SpringBoot integrates Baidu face recognition
Face Recognition (Face Recognition) is based on face detection, analysis and comparison technology in images or videos, providing face detection and attribute analysis, face comparison and face search for your private data under the premise of authorization , Liveness detection and other capabilities. It can be flexibly applied to financial, pan-security, retail and other industry scenarios to meet business needs such as identity verification, facial recognition attendance, and gate access
1 Overview
Address: https://ai.baidu.com/tech/face
2. Account application
account login registration
Baidu Cloud AI supports Baidu account login, just register on demand
create application
Create apps on demand
3. Extract template tool
AipFaceProperties
@Data
@ConfigurationProperties("facedemo")
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;
}
}
AipFaceTemplate
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;
/**
* 检测图片中是否包含人脸
* true:包含
* false:不包含
*/
public boolean detect(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;
}
}
application.yml
Write the configuration information of Baidu AI
facedemo:
appId: 24021388
apiKey: ZnMTwoETXnu4OPIGwGAO2H4G
secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh
4. Test
Write a unit test class
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class FaceTest {
@Autowired
private AipFaceTemplate template;
@Test
public void detectFace() {
String image = "图片路径";
boolean detect = template.detect(image); //是否存在人脸
}
}