JavaCV的摄像头实战之十四:口罩检测

x33g5p2x  于2022-01-09 转载在 Java  
字(13.8k)|赞(0)|评价(0)|浏览(447)

欢迎访问我的GitHub

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

本篇概览

  • 本文是《JavaCV的摄像头实战》系列的第十四篇,如标题所说,今天的功能是检测摄像头内的人是否带了口罩,把检测结果实时标注在预览窗口,如下图所示:

  • 整个处理流程如下,实现口罩检测的关键是将图片提交到百度AI开放平台,然后根据平台返回的结果在本地预览窗口标识出人脸位置,以及此人是否带了口罩:

问题提前告知

  • 依赖云平台处理业务的一个典型问题,就是处理速度受限
  • 首先,如果您在百度AI开放平台注册的账号是个人类型,那么免费的接口调用会被限制到一秒钟两次,如果是企业类型账号,该限制是十次
  • 其次,经过实测,一次人脸检测接口耗时300ms以上
  • 最终,实际上一秒钟只能处理两帧,这样的效果在预览窗口展现出来,就只能是幻灯片效果了(低于每秒十五帧就能感受到明显的卡顿)
  • 因此,本文只适合基本功能展示,无法作为实际场景的解决方案

关于百度AI开放平台

编码:添加依赖库

  • 本文继续使用《JavaCV的摄像头实战之一:基础》创建的simple-grab-push工程
  • 首先是在pom.xml中增加okhttp和jackson依赖,分别用于网络请求和JSON解析:
  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version>3.10.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-databind</artifactId>
  9. <version>2.11.0</version>
  10. </dependency>

编码:封装请求和响应百度AI开放平台的代码

  • 接下来要开发一个服务类,这个服务类封装了所有和百度AI开放平台相关的代码
  • 首先,定义web请求的request对象FaceDetectRequest.java:
  1. package com.bolingcavalry.grabpush.bean.request;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import lombok.Data;
  4. /** * @author willzhao * @version 1.0 * @description 请求对象 * @date 2022/1/1 16:21 */
  5. @Data
  6. public class FaceDetectRequest {
  7. // 图片信息(总数据大小应小于10M),图片上传方式根据image_type来判断
  8. String image;
  9. // 图片类型
  10. // BASE64:图片的base64值,base64编码后的图片数据,编码后的图片大小不超过2M;
  11. // URL:图片的 URL地址( 可能由于网络等原因导致下载图片时间过长);
  12. // FACE_TOKEN: 人脸图片的唯一标识,调用人脸检测接口时,会为每个人脸图片赋予一个唯一的FACE_TOKEN,同一张图片多次检测得到的FACE_TOKEN是同一个。
  13. @JsonProperty("image_type")
  14. String imageType;
  15. // 包括age,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing信息
  16. //逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
  17. @JsonProperty("face_field")
  18. String faceField;
  19. // 最多处理人脸的数目,默认值为1,根据人脸检测排序类型检测图片中排序第一的人脸(默认为人脸面积最大的人脸),最大值120
  20. @JsonProperty("max_face_num")
  21. int maxFaceNum;
  22. // 人脸的类型
  23. // LIVE表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等
  24. // IDCARD表示身份证芯片照:二代身份证内置芯片中的人像照片
  25. // WATERMARK表示带水印证件照:一般为带水印的小图,如公安网小图
  26. // CERT表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片
  27. // 默认LIVE
  28. @JsonProperty("face_type")
  29. String faceType;
  30. // 活体控制 检测结果中不符合要求的人脸会被过滤
  31. // NONE: 不进行控制
  32. // LOW:较低的活体要求(高通过率 低攻击拒绝率)
  33. // NORMAL: 一般的活体要求(平衡的攻击拒绝率, 通过率)
  34. // HIGH: 较高的活体要求(高攻击拒绝率 低通过率)
  35. // 默认NONE
  36. @JsonProperty("liveness_control")
  37. String livenessControl;
  38. // 人脸检测排序类型
  39. // 0:代表检测出的人脸按照人脸面积从大到小排列
  40. // 1:代表检测出的人脸按照距离图片中心从近到远排列
  41. // 默认为0
  42. @JsonProperty("face_sort_type")
  43. int faceSortType;
  44. }
  • 其次,定义web响应对象FaceDetectResponse.java:
  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. @Data
  8. @ToString
  9. public class FaceDetectResponse implements Serializable {
  10. // 返回码
  11. @JsonProperty("error_code")
  12. String errorCode;
  13. // 描述信息
  14. @JsonProperty("error_msg")
  15. String errorMsg;
  16. // 返回的具体内容
  17. Result result;
  18. @Data
  19. public static class Result {
  20. // 人脸数量
  21. @JsonProperty("face_num")
  22. private int faceNum;
  23. // 每个人脸的信息
  24. @JsonProperty("face_list")
  25. List<Face> faceList;
  26. /** * @author willzhao * @version 1.0 * @description 检测出来的人脸对象 * @date 2022/1/1 16:03 */
  27. @Data
  28. public static class Face {
  29. // 位置
  30. Location location;
  31. // 是人脸的置信度
  32. @JsonProperty("face_probability")
  33. double face_probability;
  34. // 口罩
  35. Mask mask;
  36. /** * @author willzhao * @version 1.0 * @description 人脸在图片中的位置 * @date 2022/1/1 16:04 */
  37. @Data
  38. public static class Location {
  39. double left;
  40. double top;
  41. double width;
  42. double height;
  43. double rotation;
  44. }
  45. /** * @author willzhao * @version 1.0 * @description 口罩对象 * @date 2022/1/1 16:11 */
  46. @Data
  47. public static class Mask {
  48. int type;
  49. double probability;
  50. }
  51. }
  52. }
  53. }
  • 然后是服务类BaiduCloudService.java,把请求和响应百度AI开放平台的逻辑全部集中在这里,可见其实很简单:根据图片的base64字符串构造请求对象、发POST请求(path是人脸检测服务)、收到响应后用Jackson反序列化成FaceDetectResponse对象:
  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 java.io.IOException;
  8. /** * @author willzhao * @version 1.0 * @description 百度云服务的调用 * @date 2022/1/1 11:06 */
  9. public class BaiduCloudService {
  10. OkHttpClient client = new OkHttpClient();
  11. static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  12. static final String URL_TEMPLATE = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%s";
  13. String token;
  14. ObjectMapper mapper = new ObjectMapper();
  15. public BaiduCloudService(String token) {
  16. this.token = token;
  17. // 重要:反序列化的时候,字符的字段如果比类的字段多,下面这个设置可以确保反序列化成功
  18. mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  19. }
  20. /** * 检测指定的图片 * @param imageBase64 * @return */
  21. public FaceDetectResponse detect(String imageBase64) {
  22. // 请求对象
  23. FaceDetectRequest faceDetectRequest = new FaceDetectRequest();
  24. faceDetectRequest.setImageType("BASE64");
  25. faceDetectRequest.setFaceField("mask");
  26. faceDetectRequest.setMaxFaceNum(6);
  27. faceDetectRequest.setFaceType("LIVE");
  28. faceDetectRequest.setLivenessControl("NONE");
  29. faceDetectRequest.setFaceSortType(0);
  30. faceDetectRequest.setImage(imageBase64);
  31. FaceDetectResponse faceDetectResponse = null;
  32. try {
  33. // 用Jackson将请求对象序列化成字符串
  34. String jsonContent = mapper.writeValueAsString(faceDetectRequest);
  35. //
  36. RequestBody requestBody = RequestBody.create(JSON, jsonContent);
  37. Request request = new Request .Builder()
  38. .url(String.format(URL_TEMPLATE, token))
  39. .post(requestBody)
  40. .build();
  41. Response response = client.newCall(request).execute();
  42. String rawRlt = response.body().string();
  43. faceDetectResponse = mapper.readValue(rawRlt, FaceDetectResponse.class);
  44. } catch (IOException ioException) {
  45. ioException.printStackTrace();
  46. }
  47. return faceDetectResponse;
  48. }
  49. }
  • 服务类写完了,接下来是主程序把整个逻辑串起来

DetectService接口的实现

  • 熟悉《JavaCV的摄像头实战》系列的读者应该对DetectService接口不陌生了,为了在整个系列的诸多实战中以统一的风格实现抓取帧–>处理帧–>输出处理结果这样的流程,咱们定义了一个DetectService接口,每种不同帧处理业务按照自己的特点来实现此接口即可(例如人脸检测、年龄检测、性别检测等)
  • 先来回顾DetectService接口:
  1. package com.bolingcavalry.grabpush.extend;
  2. import org.bytedeco.javacv.Frame;
  3. import org.bytedeco.javacv.OpenCVFrameConverter;
  4. import org.bytedeco.opencv.opencv_core.*;
  5. import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
  6. import static org.bytedeco.opencv.global.opencv_core.CV_8UC1;
  7. import static org.bytedeco.opencv.global.opencv_imgproc.*;
  8. /** * @author willzhao * @version 1.0 * @description 检测工具的通用接口 * @date 2021/12/5 10:57 */
  9. public interface DetectService {
  10. /** * 根据传入的MAT构造相同尺寸的MAT,存放灰度图片用于以后的检测 * @param src 原始图片的MAT对象 * @return 相同尺寸的灰度图片的MAT对象 */
  11. static Mat buildGrayImage(Mat src) {
  12. return new Mat(src.rows(), src.cols(), CV_8UC1);
  13. }
  14. /** * 检测图片,将检测结果用矩形标注在原始图片上 * @param classifier 分类器 * @param converter Frame和mat的转换器 * @param rawFrame 原始视频帧 * @param grabbedImage 原始视频帧对应的mat * @param grayImage 存放灰度图片的mat * @return 标注了识别结果的视频帧 */
  15. static Frame detect(CascadeClassifier classifier,
  16. OpenCVFrameConverter.ToMat converter,
  17. Frame rawFrame,
  18. Mat grabbedImage,
  19. Mat grayImage) {
  20. // 当前图片转为灰度图片
  21. cvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
  22. // 存放检测结果的容器
  23. RectVector objects = new RectVector();
  24. // 开始检测
  25. classifier.detectMultiScale(grayImage, objects);
  26. // 检测结果总数
  27. long total = objects.size();
  28. // 如果没有检测到结果,就用原始帧返回
  29. if (total<1) {
  30. return rawFrame;
  31. }
  32. // 如果有检测结果,就根据结果的数据构造矩形框,画在原图上
  33. for (long i = 0; i < total; i++) {
  34. Rect r = objects.get(i);
  35. int x = r.x(), y = r.y(), w = r.width(), h = r.height();
  36. rectangle(grabbedImage, new Point(x, y), new Point(x + w, y + h), Scalar.RED, 1, CV_AA, 0);
  37. }
  38. // 释放检测结果资源
  39. objects.close();
  40. // 将标注过的图片转为帧,返回
  41. return converter.convert(grabbedImage);
  42. }
  43. /** * 初始化操作,例如模型下载 * @throws Exception */
  44. void init() throws Exception;
  45. /** * 得到原始帧,做识别,添加框选 * @param frame * @return */
  46. Frame convert(Frame frame);
  47. /** * 释放资源 */
  48. void releaseOutputResource();
  49. }
  • 再来看看本次实战中DetectService接口的实现类BaiduCloudDetectService.java,有几处要注意的地方稍后会提到:
  1. package com.bolingcavalry.grabpush.extend;
  2. import com.bolingcavalry.grabpush.bean.response.FaceDetectResponse;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.bytedeco.javacpp.Loader;
  5. import org.bytedeco.javacv.Frame;
  6. import org.bytedeco.javacv.Java2DFrameConverter;
  7. import org.bytedeco.javacv.OpenCVFrameConverter;
  8. import org.bytedeco.opencv.opencv_core.Mat;
  9. import org.bytedeco.opencv.opencv_core.Point;
  10. import org.bytedeco.opencv.opencv_core.Rect;
  11. import org.bytedeco.opencv.opencv_core.Scalar;
  12. import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
  13. import org.opencv.face.Face;
  14. import sun.misc.BASE64Encoder;
  15. import javax.imageio.ImageIO;
  16. import java.awt.image.BufferedImage;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.net.URL;
  21. import java.util.List;
  22. import static org.bytedeco.opencv.global.opencv_imgproc.*;
  23. import static org.bytedeco.opencv.global.opencv_imgproc.CV_AA;
  24. /** * @author willzhao * @version 1.0 * @description 音频相关的服务 * @date 2021/12/3 8:09 */
  25. @Slf4j
  26. public class BaiduCloudDetectService implements DetectService {
  27. /** * 每一帧原始图片的对象 */
  28. private Mat grabbedImage = null;
  29. /** * 百度云的token */
  30. private String token;
  31. /** * 图片的base64字符串 */
  32. private String base64Str;
  33. /** * 百度云服务 */
  34. private BaiduCloudService baiduCloudService;
  35. private OpenCVFrameConverter.ToMat openCVConverter = new OpenCVFrameConverter.ToMat();
  36. private Java2DFrameConverter java2DConverter = new Java2DFrameConverter();
  37. private OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
  38. private BASE64Encoder encoder = new BASE64Encoder();
  39. /** * 构造方法,在此指定模型文件的下载地址 * @param token */
  40. public BaiduCloudDetectService(String token) {
  41. this.token = token;
  42. }
  43. /** * 百度云服务对象的初始化 * @throws Exception */
  44. @Override
  45. public void init() throws Exception {
  46. baiduCloudService = new BaiduCloudService(token);
  47. }
  48. @Override
  49. public Frame convert(Frame frame) {
  50. // 将原始帧转成base64字符串
  51. base64Str = frame2Base64(frame);
  52. // 记录请求开始的时间
  53. long startTime = System.currentTimeMillis();
  54. // 交给百度云进行人脸和口罩检测
  55. FaceDetectResponse faceDetectResponse = baiduCloudService.detect(base64Str);
  56. // 如果检测失败,就提前返回了
  57. if (null==faceDetectResponse
  58. || null==faceDetectResponse.getErrorCode()
  59. || !"0".equals(faceDetectResponse.getErrorCode())) {
  60. String desc = "";
  61. if (null!=faceDetectResponse) {
  62. desc = String.format(",错误码[%s],错误信息[%s]", faceDetectResponse.getErrorCode(), faceDetectResponse.getErrorMsg());
  63. }
  64. log.error("检测人脸失败", desc);
  65. // 提前返回
  66. return frame;
  67. }
  68. log.info("检测耗时[{}]ms,结果:{}", (System.currentTimeMillis()-startTime), faceDetectResponse);
  69. // 如果拿不到检测结果,就返回原始帧
  70. if (null==faceDetectResponse.getResult()
  71. || null==faceDetectResponse.getResult().getFaceList()) {
  72. log.info("未检测到人脸");
  73. return frame;
  74. }
  75. // 取出百度云的检测结果,后面会逐个处理
  76. List<FaceDetectResponse.Result.Face> list = faceDetectResponse.getResult().getFaceList();
  77. FaceDetectResponse.Result.Face face;
  78. FaceDetectResponse.Result.Face.Location location;
  79. String desc;
  80. Scalar color;
  81. int pos_x;
  82. int pos_y;
  83. // 如果有检测结果,就根据结果的数据构造矩形框,画在原图上
  84. for (int i = 0; i < list.size(); i++) {
  85. face = list.get(i);
  86. // 每张人脸的位置
  87. location = face.getLocation();
  88. int x = (int)location.getLeft();
  89. int y = (int)location.getHeight();
  90. int w = (int)location.getWidth();
  91. int h = (int)location.getHeight();
  92. // 口罩字段的type等于1表示带口罩,0表示未带口罩
  93. if (1==face.getMask().getType()) {
  94. desc = "Mask";
  95. color = Scalar.GREEN;
  96. } else {
  97. desc = "No mask";
  98. color = Scalar.RED;
  99. }
  100. // 在图片上框出人脸
  101. rectangle(grabbedImage, new Point(x, y), new Point(x + w, y + h), color, 1, CV_AA, 0);
  102. // 人脸标注的横坐标
  103. pos_x = Math.max(x-10, 0);
  104. // 人脸标注的纵坐标
  105. pos_y = Math.max(y-10, 0);
  106. // 给人脸做标注,标注是否佩戴口罩
  107. putText(grabbedImage, desc, new Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.5, color);
  108. }
  109. // 将标注过的图片转为帧,返回
  110. return converter.convert(grabbedImage);
  111. }
  112. /** * 程序结束前,释放人脸识别的资源 */
  113. @Override
  114. public void releaseOutputResource() {
  115. if (null!=grabbedImage) {
  116. grabbedImage.release();
  117. }
  118. }
  119. private String frame2Base64(Frame frame) {
  120. grabbedImage = converter.convert(frame);
  121. BufferedImage bufferedImage = java2DConverter.convert(openCVConverter.convert(grabbedImage));
  122. ByteArrayOutputStream bStream = new ByteArrayOutputStream();
  123. try {
  124. ImageIO.write(bufferedImage, "png", bStream);
  125. } catch (IOException e) {
  126. throw new RuntimeException("bugImg读取失败:"+e.getMessage(),e);
  127. }
  128. return encoder.encode(bStream.toByteArray());
  129. }
  130. }
  • 上述代码有以下几点要注意:
  1. 整个BaiduCloudDetectService类,主要是对前面BaiduCloudService类的使用
  2. convert方法中,拿到frame实例后会转为base64字符串,用于提交到百度AI开放平台做人脸检测
  3. 百度AI开放平台的检测结果中有多个人脸检测结果,这里要逐个处理:取出每个人脸的位置,以此位置在原图画矩形框,然后根据是否戴口罩在人脸上做标记,戴口罩的是绿色标记(包括矩形框),不戴口罩的是红色矩形框

主程序

  • 最后是主程序了,还是《JavaCV的摄像头实战》系列的套路,咱们来看看主程序的服务类定义好的框架
  • 《JavaCV的摄像头实战之一:基础》创建的simple-grab-push工程中已经准备好了父类AbstractCameraApplication,所以本篇继续使用该工程,创建子类实现那些抽象方法即可
  • 编码前先回顾父类的基础结构,如下图,粗体是父类定义的各个方法,红色块都是需要子类来实现抽象方法,所以接下来,咱们以本地窗口预览为目标实现这三个红色方法即可:

  • 新建文件PreviewCameraWithBaiduCloud.java,这是AbstractCameraApplication的子类,其代码很简单,接下来按上图顺序依次说明
  • 先定义CanvasFrame类型的成员变量previewCanvas,这是展示视频帧的本地窗口:
  1. protected CanvasFrame previewCanvas
  • 把前面创建的DetectService作为成员变量,后面检测的时候会用到:
  1. /** * 检测工具接口 */
  2. private DetectService detectService;
  • PreviewCameraWithBaiduCloud的构造方法,接受DetectService的实例:
  1. /** * 不同的检测工具,可以通过构造方法传入 * @param detectService */
  2. public PreviewCameraWithBaiduCloud(DetectService detectService) {
  3. this.detectService = detectService;
  4. }
  • 然后是初始化操作,可见是previewCanvas的实例化和参数设置,还有检测、识别的初始化操作:
  1. @Override
  2. protected void initOutput() throws Exception {
  3. previewCanvas = new CanvasFrame("摄像头预览", CanvasFrame.getDefaultGamma() / grabber.getGamma());
  4. previewCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. previewCanvas.setAlwaysOnTop(true);
  6. // 检测服务的初始化操作
  7. detectService.init();
  8. }
  • 接下来是output方法,定义了拿到每一帧视频数据后做什么事情,这里调用了detectService.convert检测人脸并识别性别,然后在本地窗口显示:
  1. @Override
  2. protected void output(Frame frame) {
  3. // 原始帧先交给检测服务处理,这个处理包括物体检测,再将检测结果标注在原始图片上,
  4. // 然后转换为帧返回
  5. Frame detectedFrame = detectService.convert(frame);
  6. // 预览窗口上显示的帧是标注了检测结果的帧
  7. previewCanvas.showImage(detectedFrame);
  8. }
  • 最后是处理视频的循环结束后,程序退出前要做的事情,先关闭本地窗口,再释放检测服务的资源:
  1. @Override
  2. protected void releaseOutputResource() {
  3. if (null!= previewCanvas) {
  4. previewCanvas.dispose();
  5. }
  6. // 检测工具也要释放资源
  7. detectService.releaseOutputResource();
  8. }
  • 每一帧耗时太多,所以两帧之间就不再额外间隔了:
  1. @Override
  2. protected int getInterval() {
  3. return 0;
  4. }
  • 至此,功能已开发完成,再写上main方法,代码如下,请注意token的值是前面在百度AI开放平台取得的access_token:
  1. public static void main(String[] args) {
  2. String token = "21.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxx.xxxxxx-xxxxxxxx";
  3. new PreviewCameraWithBaiduCloud(new BaiduCloudDetectService(token)).action(1000);
  4. }
  • 至此,代码写完了,准备好摄像头开始验证,群众演员为了免费盒饭已经在寒风中等了很久啦

验证

  • 运行PreviewCameraWithBaiduCloud的main方法,请群众演员出现在摄像头前面,此时不戴口罩,可见人脸上是红色字体和矩形框:

  • 让群众演员戴上口罩,再次出现在摄像头前面,这次检测到了口罩,显示了绿色标注和矩形框:

  • 实际体验中,由于一秒钟最多只有两帧,在预览窗口展示时完全是幻灯片效果,惨不忍睹…
  • 本篇博客使用了群众演员两张照片,所以被他领走了两份盒饭,欣宸很心疼…
  • 至此,基于JavaCV和百度AI开放平台实现的口罩检测功能已完成,希望您继续关注《JavaCV的摄像头实战》系列,之后的实战更精彩

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

相关文章

最新文章

更多