8. 百度人脸识别

百度人脸识别

人脸识别(Face Recognition)基于图像或视频中的人脸检测、分析和比对技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求

一、官网示例代码

public class Sample {
    //设置APPID/AK/SK
    public static final String APP_ID = "你的 App ID";
    public static final String API_KEY = "你的 Api Key";
    public static final String SECRET_KEY = "你的 Secret Key";

    public static void main(String[] args) {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 调用接口
        String image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
        String imageType = "BASE64";
        
         // 传入可选参数调用接口
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("face_field", "age");
    options.put("max_face_num", "2");
    options.put("face_type", "LIVE");
    options.put("liveness_control", "LOW");
        
    
        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));
        
    }
}

二、项目代码

2.1先测试,FaceApiTest

检测一张图片中是否包含有人脸

package com.itheima.test;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

import java.util.HashMap;

public class FaceApiTest {

    //设置APPID/AK/SK
    public static final String APP_ID = "27915129";
    public static final String API_KEY = "iYbOuyOqi9BsVw4SbAEQifgR";
    public static final String SECRET_KEY = "Z5kpiL2Fnb1ujQIlcPLjZ9fg0dcAWjaO";

    public static void main(String[] args) {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 调用接口
        String image = "https://project-of-tanhua.oss-cn-hangzhou.aliyuncs.com/2022/10/14/f716e89e-1364-435a-b3f3-4c9b42d563a2.jpg";
        String imageType = "URL";


        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));

    }
}

三、抽取模板工具

3.1在tanhua-app-server的applicatin.yaml配置信息

tanhua:
  faceapi:
    appId: 27915129
    apiKey: iYbOuyOqi9BsVw4SbAEQifgR
    secret: Z5kpiL2Fnb1ujQIlcPLjZ9fg0dcAWjaO

3.2FaceApiProperties配置类,读取配置

package com.tanhua.autoconfig.properties;

import com.baidu.aip.face.AipFace;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

@Data
@ConfigurationProperties("tanhua.faceapi")
public class FaceApiProperties {


    private String appId;
    private String apiKey;
    private String secret;
    
    
    @Bean
    public AipFace client(){
     AipFace client = new AipFace(appId, apiKey, secret);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }
}

3.3FaceApiTemplate

package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

public class FaceApiTemplate {

    @Autowired
    private AipFace client;



    /**
     * 检测图片中是否有人脸
     * 需要的参数:
     * 本项目可以使用在阿里云oss中上传上去的照片URL
     * @param imageUrl
     * @return
     */
    public boolean checkFace(String imageUrl){

        String imageType = "URL";


        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        System.out.println(res.toString(2));



        Integer error_code = (Integer) res.get("error_code");
        return error_code == 0;
    }

}

3.4测试

package com.itheima.test;

import com.tanhua.autoconfig.template.FaceApiTemplate;
import com.tanhua.server.AppServerApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class FaceApiTest2 {


    @Autowired
    private FaceApiTemplate faceApiTemplate;

    @Test
    public void test (){

        String imageUrl = "https://project-of-tanhua.oss-cn-hangzhou.aliyuncs.com/2022/10/14/f716e89e-1364-435a-b3f3-4c9b42d563a2.jpg";
        boolean result = faceApiTemplate.checkFace(imageUrl);
        System.out.println("图片是否含有人脸?"+result);
    }

}

posted @ 2022-10-30 12:09  给我手牵你走  阅读(253)  评论(0编辑  收藏  举报