qidao123.com技术社区-IT企服评测·应用市场

标题: 最简单的人脸检测(免费调用百度AI开放平台接口) [打印本页]

作者: 九天猎人    时间: 2023-7-4 07:40
标题: 最简单的人脸检测(免费调用百度AI开放平台接口)
远程调用百度AI开放平台的web服务,快速完成人脸识别
欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览

注册百度账号

登录百度智能云

实名认证

创建应用

拿到API Key和Secret Key

得到access_token

  1. curl -i -k 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【百度云应用的API Key】&client_secret=【百度云应用的Secret Key】'
复制代码
编码

  1. <dependency>
  2.         <groupId>org.projectlombok</groupId>
  3.     <artifactId>lombok</artifactId>
  4.     <version>1.18.18</version>
  5. </dependency>
  6. <dependency>
  7.         <groupId>com.squareup.okhttp3</groupId>
  8.         <artifactId>okhttp</artifactId>
  9.         <version>3.10.0</version>
  10. </dependency>
  11. <dependency>
  12.         <groupId>com.fasterxml.jackson.core</groupId>
  13.         <artifactId>jackson-databind</artifactId>
  14.         <version>2.11.0</version>
  15. </dependency>
复制代码
  1. package com.bolingcavalry.grabpush.bean.request;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import lombok.Data;
  4. /**
  5. * @author willzhao
  6. * @version 1.0
  7. * @description 请求对象
  8. * @date 2022/1/1 16:21
  9. */
  10. @Data
  11. public class FaceDetectRequest {
  12.     // 图片信息(总数据大小应小于10M),图片上传方式根据image_type来判断
  13.     String image;
  14.     // 图片类型
  15.     // BASE64:图片的base64值,base64编码后的图片数据,编码后的图片大小不超过2M;
  16.     // URL:图片的 URL地址( 可能由于网络等原因导致下载图片时间过长);
  17.     // FACE_TOKEN: 人脸图片的唯一标识,调用人脸检测接口时,会为每个人脸图片赋予一个唯一的FACE_TOKEN,同一张图片多次检测得到的FACE_TOKEN是同一个。
  18.     @JsonProperty("image_type")
  19.     String imageType;
  20.     // 包括age,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing信息
  21.     //逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
  22.     @JsonProperty("face_field")
  23.     String faceField;
  24.     // 最多处理人脸的数目,默认值为1,根据人脸检测排序类型检测图片中排序第一的人脸(默认为人脸面积最大的人脸),最大值120
  25.     @JsonProperty("max_face_num")
  26.     int maxFaceNum;
  27.     // 人脸的类型
  28.     // LIVE表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等
  29.     // IDCARD表示身份证芯片照:二代身份证内置芯片中的人像照片
  30.     // WATERMARK表示带水印证件照:一般为带水印的小图,如公安网小图
  31.     // CERT表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片
  32.     // 默认LIVE
  33.     @JsonProperty("face_type")
  34.     String faceType;
  35.     // 活体控制 检测结果中不符合要求的人脸会被过滤
  36.     // NONE: 不进行控制
  37.     // LOW:较低的活体要求(高通过率 低攻击拒绝率)
  38.     // NORMAL: 一般的活体要求(平衡的攻击拒绝率, 通过率)
  39.     // HIGH: 较高的活体要求(高攻击拒绝率 低通过率)
  40.     // 默认NONE
  41.     @JsonProperty("liveness_control")
  42.     String livenessControl;
  43.    
  44.     // 人脸检测排序类型
  45.     // 0:代表检测出的人脸按照人脸面积从大到小排列
  46.     // 1:代表检测出的人脸按照距离图片中心从近到远排列
  47.     // 默认为0
  48.     @JsonProperty("face_sort_type")
  49.     int faceSortType;
  50. }
复制代码
  1. package com.bolingcavalry.grabpush.bean.response;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import lombok.Data;
  4. import lombok.ToString;
  5. import java.io.Serializable;
  6. import java.util.List;
  7. /**
  8. * @author willzhao
  9. * @version 1.0
  10. * @description TODO
  11. * @date 2022/1/1 13:30
  12. */
  13. @Data
  14. @ToString
  15. public class FaceDetectResponse implements Serializable {
  16.     // 返回码
  17.     @JsonProperty("error_code")
  18.     String errorCode;
  19.     // 描述信息
  20.     @JsonProperty("error_msg")
  21.     String errorMsg;
  22.     // 返回的具体内容
  23.     Result result;
  24.     /**
  25.      * @author willzhao
  26.      * @version 1.0
  27.      * @description 返回的具体内容
  28.      * @date 2022/1/1 16:01
  29.      */
  30.     @Data
  31.     public static class Result {
  32.         // 人脸数量
  33.         @JsonProperty("face_num")
  34.         private int faceNum;
  35.         // 每个人脸的信息
  36.         @JsonProperty("face_list")
  37.         List<Face> faceList;
  38.         /**
  39.          * @author willzhao
  40.          * @version 1.0
  41.          * @description 检测出来的人脸对象
  42.          * @date 2022/1/1 16:03
  43.          */
  44.         @Data
  45.         public static class Face {
  46.             // 位置
  47.             Location location;
  48.             // 是人脸的置信度
  49.             @JsonProperty("face_probability")
  50.             double face_probability;
  51.             // 口罩
  52.             Mask mask;
  53.             /**
  54.              * @author willzhao
  55.              * @version 1.0
  56.              * @description 人脸在图片中的位置
  57.              * @date 2022/1/1 16:04
  58.              */
  59.             @Data
  60.             public static class Location {
  61.                 double left;
  62.                 double top;
  63.                 double width;
  64.                 double height;
  65.                 double rotation;
  66.             }
  67.             /**
  68.              * @author willzhao
  69.              * @version 1.0
  70.              * @description 口罩对象
  71.              * @date 2022/1/1 16:11
  72.              */
  73.             @Data
  74.             public static class Mask {
  75.                 int type;
  76.                 double probability;
  77.             }
  78.         }
  79.     }
  80. }
复制代码
  1. package com.bolingcavalry.grabpush.extend;
  2. import com.bolingcavalry.grabpush.bean.request.FaceDetectRequest;
  3. import com.bolingcavalry.grabpush.bean.response.FaceDetectResponse;
  4. import com.fasterxml.jackson.databind.DeserializationFeature;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import okhttp3.*;
  7. import sun.misc.BASE64Encoder;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. /**
  12. * @author willzhao
  13. * @version 1.0
  14. * @description 百度云服务的调用
  15. * @date 2022/1/1 11:06
  16. */
  17. public class BaiduCloudService {
  18.     // 转换
  19.     BASE64Encoder encoder = new BASE64Encoder();
  20.     OkHttpClient client = new OkHttpClient();
  21.     static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  22.     static final String URL_TEMPLATE = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%s";
  23.     String token;
  24.     ObjectMapper mapper = new ObjectMapper();
  25.     public BaiduCloudService(String token) {
  26.         this.token = token;
  27.         
  28.         // 重要:反序列化的时候,字符的字段如果比类的字段多,下面这个设置可以确保反序列化成功
  29.         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  30.     }
  31.     /**
  32.      * 将指定位置的图片转为base64字符串
  33.      * @param imagePath
  34.      * @return
  35.      */
  36.     private String img2Base64(String imagePath) {
  37.         InputStream inputStream = null;
  38.         byte[] data = null;
  39.         try {
  40.             inputStream = new FileInputStream(imagePath);
  41.             data = new byte[inputStream.available()];
  42.             inputStream.read(data);
  43.             inputStream.close();
  44.         } catch (IOException ioException) {
  45.             ioException.printStackTrace();
  46.         }
  47.         return null==data ? null :encoder.encode(data);
  48.     }
  49.     /**
  50.      * 检测指定的图片
  51.      * @param imageBase64
  52.      * @return
  53.      */
  54.     public FaceDetectResponse detect(String imageBase64) {
  55.         // 请求对象
  56.         FaceDetectRequest faceDetectRequest = new FaceDetectRequest();
  57.         faceDetectRequest.setImageType("BASE64");
  58.         faceDetectRequest.setFaceField("mask");
  59.         faceDetectRequest.setMaxFaceNum(6);
  60.         faceDetectRequest.setFaceType("LIVE");
  61.         faceDetectRequest.setLivenessControl("NONE");
  62.         faceDetectRequest.setFaceSortType(0);
  63.         faceDetectRequest.setImage(imageBase64);
  64.         FaceDetectResponse faceDetectResponse = null;
  65.         try {
  66.             // 用Jackson将请求对象序列化成字符串
  67.             String jsonContent = mapper.writeValueAsString(faceDetectRequest);
  68.             //
  69.             RequestBody requestBody = RequestBody.create(JSON, jsonContent);
  70.             Request request = new Request
  71.                     .Builder()
  72.                     .url(String.format(URL_TEMPLATE, token))
  73.                     .post(requestBody)
  74.                     .build();
  75.             Response response = client.newCall(request).execute();
  76.             String rawRlt = response.body().string();
  77.             faceDetectResponse = mapper.readValue(rawRlt, FaceDetectResponse.class);
  78.         } catch (IOException ioException) {
  79.             ioException.printStackTrace();
  80.         }
  81.         return faceDetectResponse;
  82.     }
  83.     public static void main(String[] args) {
  84.         // 图片在本地的位置
  85.         String imagePath = "E:\\temp\\202201\\01\\pic\\1.jpeg";
  86.         // 百度云的token,是通过此接口得到的:https://aip.baidubce.com/oauth/2.0/token
  87.         String token = "24.95xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxx.xxxxxx-xxxxxxxx";
  88.         // 实例化服务对象
  89.         BaiduCloudService service = new BaiduCloudService(token);
  90.         // 将图片转为base64字符串
  91.         String imageBase64 = service.img2Base64(imagePath);
  92.         // 向百度服务发请求,检测人脸
  93.         FaceDetectResponse faceDetectResponse = service.detect(imageBase64);
  94.         // 输出检测结果
  95.         System.out.println(faceDetectResponse);
  96.     }
  97. }
复制代码
使用限制

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4