官方定义:
人脸识别(Face Recognition)基于图片或视频中的人脸检测、分析和对比技术,提供对您已获授权前提下的私有数据的人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求。
相关依赖:
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.8.0</version>
</dependency>
配置类:
@Data
@ConfigurationPropertion(prefix="tanhua.aip")
public class AipFaceProperties{
private String appId;
private String apiKey;
private String secretKey;
@Bean
public AipFace aipFace(){
AipFace client = new AipFace(appId,apiKey,secretKey);
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
}
模板类:
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 AipFaceTemplate{
@AutoWired
private AipFace client;
public boolean detect(String imageUrl){
HashMap<String,String> options = new HashMap<>();
options.put("face_field","age");
options.put("max_face_num","2");
options.put("face_type","LIVE");
option.put("liveness_control","LOW");
JSONObject res = client.detect(imageUrl,"URL",options);
Integer errorCode = (Integer) res.get("error_code");
return errorCode == 0;
}
}
注册到Spring IoC容器中:
@EnableConfigugrationProperties({AipFaceProperties.class})
public class TanhuaAutoConfiguration{
@Bean
public AipFaceTemplate aipFaceTemplate(){
return new AipFaceTemplate();
}
}
在application.yml中添加人脸识别配置:
aip:
appId: 24021388
apiKey: ZnMTwoETXnu4OPIGwGAO2H4G
secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh