当前位置:   article > 正文

百度人脸识别接口java使用_baiduface/config

baiduface/config

 

 

我:

(1)maven包:

  1. <!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
  2. <dependency>
  3. <groupId>com.baidu.aip</groupId>
  4. <artifactId>java-sdk</artifactId>
  5. <version>4.15.1</version>
  6. </dependency>

(1)配置类(需要去百度人脸识别官网注册账号获取appid和密匙对):BaiduFaceConfig

  1. /**
  2. * @qq 1393180819
  3. * @author wuguixin
  4. */
  5. package utils.baidu;
  6. import com.baidu.aip.face.AipFace;
  7. import com.baidu.aip.face.FaceVerifyRequest;
  8. import com.baidu.aip.face.MatchRequest;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. public class BaiduFaceConfig {
  14. //设置APPID/AK/SK
  15. public static String APP_ID = null;
  16. public static String API_KEY = null;
  17. public static String SECRET_KEY = null;
  18. private static AipFace client = null;
  19. public static final String IMG_TYPE_BASE64 = "BASE64";
  20. public static final String IMG_TYPE_URL = "URL";
  21. public static final String IMG_TYPE_FACE_TOKEN = "FACE_TOKEN";
  22. public static void createClient(){
  23. if(null==client){
  24. client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
  25. }
  26. }
  27. // 人脸检测
  28. public static JSONObject detectFace(String image, String imageType){
  29. createClient();
  30. // 可选:设置网络连接参数
  31. client.setConnectionTimeoutInMillis(2000);
  32. client.setSocketTimeoutInMillis(60000);
  33. // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
  34. //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
  35. //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
  36. // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
  37. // 也可以直接通过jvm启动参数设置此环境变量
  38. //System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
  39. // 传入可选参数调用接口
  40. HashMap<String, String> options = new HashMap<>();
  41. options.put("face_field", "quality"); // 选择可返回字段
  42. //options.put("max_face_num", "2");
  43. //options.put("face_type", "LIVE");
  44. // 图片大小应小于10M
  45. // 取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串
  46. JSONObject res = client.detect(image, imageType, options);
  47. System.out.println(res);
  48. return res;
  49. }
  50. /*Base64.encode(BaiduFaceUtils.File2byte(file))*/
  51. // 在线活体检测
  52. public static JSONObject detectLivingFace(String image,String imageType) {
  53. createClient();
  54. FaceVerifyRequest req = new FaceVerifyRequest(image, imageType);
  55. ArrayList<FaceVerifyRequest> list = new ArrayList<FaceVerifyRequest>();
  56. list.add(req);
  57. JSONObject res = client.faceverify(list);
  58. System.out.println(res.toString(2));
  59. return res;
  60. }
  61. // 人脸对比
  62. public static JSONObject matchFace(String image1,String image2,String imageType){
  63. createClient();
  64. MatchRequest req1 = new MatchRequest(image1, imageType);
  65. MatchRequest req2 = new MatchRequest(image2, imageType);
  66. ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
  67. requests.add(req1);
  68. requests.add(req2);
  69. JSONObject res = client.match(requests);
  70. System
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/123532
推荐阅读
相关标签
  

闽ICP备14008679号