SpringBoot集成百度人脸识别

亦凉 2023-10-12 18:18 144阅读 0赞

SpringBoot集成百度人脸识别

      • 1、概述
      • 2、账号申请
        • 账号登录注册
        • 创建应用
      • 3、抽取模板工具
        • AipFaceProperties
        • AipFaceTemplate
        • application.yml
      • 4、测试

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

1、概述

地址:https://ai.baidu.com/tech/face

image-20201030184449505

2、账号申请

账号登录注册

百度云AI支持百度账号登录,按需注册即可

image-20201030184841463

创建应用

按需创建应用

image-20230620094348630

image-20230620094443292

3、抽取模板工具

AipFaceProperties
  1. @Data
  2. @ConfigurationProperties("facedemo")
  3. public class AipFaceProperties {
  4. private String appId;
  5. private String apiKey;
  6. private String secretKey;
  7. @Bean
  8. public AipFace aipFace() {
  9. AipFace client = new AipFace(appId, apiKey, secretKey);
  10. // 可选:设置网络连接参数
  11. client.setConnectionTimeoutInMillis(2000);
  12. client.setSocketTimeoutInMillis(60000);
  13. return client;
  14. }
  15. }
AipFaceTemplate
  1. import com.baidu.aip.face.AipFace;
  2. import org.json.JSONObject;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import java.util.HashMap;
  5. public class AipFaceTemplate {
  6. @Autowired
  7. private AipFace client;
  8. /**
  9. * 检测图片中是否包含人脸
  10. * true:包含
  11. * false:不包含
  12. */
  13. public boolean detect(String imageUrl) {
  14. // 调用接口
  15. String imageType = "URL";
  16. HashMap<String, String> options = new HashMap<String, String>();
  17. options.put("face_field", "age");
  18. options.put("max_face_num", "2");
  19. options.put("face_type", "LIVE");
  20. options.put("liveness_control", "LOW");
  21. // 人脸检测
  22. JSONObject res = client.detect(imageUrl, imageType, options);
  23. System.out.println(res.toString(2));
  24. Integer error_code = (Integer) res.get("error_code");
  25. return error_code == 0;
  26. }
  27. }
application.yml

编写百度AI的配置信息

  1. facedemo:
  2. appId: 24021388
  3. apiKey: ZnMTwoETXnu4OPIGwGAO2H4G
  4. secretKey: D4jXShyinv5q26bUS78xRKgNLnB9IfZh

4、测试

编写单元测试类

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes = AppServerApplication.class)
  3. public class FaceTest {
  4. @Autowired
  5. private AipFaceTemplate template;
  6. @Test
  7. public void detectFace() {
  8. String image = "图片路径";
  9. boolean detect = template.detect(image); //是否存在人脸
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读