欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
- 本文是《JavaCV的摄像头实战》系列的第十四篇,如标题所说,今天的功能是检测摄像头内的人是否带了口罩,把检测结果实时标注在预览窗口,如下图所示:
- 整个处理流程如下,实现口罩检测的关键是将图片提交到百度AI开放平台,然后根据平台返回的结果在本地预览窗口标识出人脸位置,以及此人是否带了口罩:
问题提前告知
- 依赖云平台处理业务的一个典型问题,就是处理速度受限
- 首先,如果您在百度AI开放平台注册的账号是个人类型,那么免费的接口调用会被限制到一秒钟两次,如果是企业类型账号,该限制是十次
- 其次,经过实测,一次人脸检测接口耗时300ms以上
- 最终,实际上一秒钟只能处理两帧,这样的效果在预览窗口展现出来,就只能是幻灯片效果了(低于每秒十五帧就能感受到明显的卡顿)
- 因此,本文只适合基本功能展示,无法作为实际场景的解决方案
关于百度AI开放平台
编码:添加依赖库
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
编码:封装请求和响应百度AI开放平台的代码
- 接下来要开发一个服务类,这个服务类封装了所有和百度AI开放平台相关的代码
- 首先,定义web请求的request对象FaceDetectRequest.java:
package com.bolingcavalry.grabpush.bean.request;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class FaceDetectRequest {
String image;
@JsonProperty("image_type")
String imageType;
@JsonProperty("face_field")
String faceField;
@JsonProperty("max_face_num")
int maxFaceNum;
@JsonProperty("face_type")
String faceType;
@JsonProperty("liveness_control")
String livenessControl;
@JsonProperty("face_sort_type")
int faceSortType;
}
- 其次,定义web响应对象FaceDetectResponse.java:
package com.bolingcavalry.grabpush.bean.response;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
import java.util.List;
@Data
@ToString
public class FaceDetectResponse implements Serializable {
@JsonProperty("error_code")
String errorCode;
@JsonProperty("error_msg")
String errorMsg;
Result result;
@Data
public static class Result {
@JsonProperty("face_num")
private int faceNum;
@JsonProperty("face_list")
List<Face> faceList;
@Data
public static class Face {
Location location;
@JsonProperty("face_probability")
double face_probability;
Mask mask;
@Data
public static class Location {
double left;
double top;
double width;
double height;
double rotation;
}
@Data
public static class Mask {
int type;
double probability;
}
}
}
}
- 然后是服务类BaiduCloudService.java,把请求和响应百度AI开放平台的逻辑全部集中在这里,可见其实很简单:根据图片的base64字符串构造请求对象、发POST请求(path是人脸检测服务)、收到响应后用Jackson反序列化成FaceDetectResponse对象:
package com.bolingcavalry.grabpush.extend;
import com.bolingcavalry.grabpush.bean.request.FaceDetectRequest;
import com.bolingcavalry.grabpush.bean.response.FaceDetectResponse;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import java.io.IOException;
public class BaiduCloudService {
OkHttpClient client = new OkHttpClient();
static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
static final String URL_TEMPLATE = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%s";
String token;
ObjectMapper mapper = new ObjectMapper();
public BaiduCloudService(String token) {
this.token = token;
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public FaceDetectResponse detect(String imageBase64) {
FaceDetectRequest faceDetectRequest = new FaceDetectRequest();
faceDetectRequest.setImageType("BASE64");
faceDetectRequest.setFaceField("mask");
faceDetectRequest.setMaxFaceNum(6);
faceDetectRequest.setFaceType("LIVE");
faceDetectRequest.setLivenessControl("NONE");
faceDetectRequest.setFaceSortType(0);
faceDetectRequest.setImage(imageBase64);
FaceDetectResponse faceDetectResponse = null;
try {
String jsonContent = mapper.writeValueAsString(faceDetectRequest);
RequestBody requestBody = RequestBody.create(JSON, jsonContent);
Request request = new Request
.Builder()
.url(String.format(URL_TEMPLATE, token))
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
String rawRlt = response.body().string();
faceDetectResponse = mapper.readValue(rawRlt, FaceDetectResponse.class);
} catch (IOException ioException) {
ioException.printStackTrace();
}
return faceDetectResponse;
}
}
DetectService接口的实现
- 熟悉《JavaCV的摄像头实战》系列的读者应该对DetectService接口不陌生了,为了在整个系列的诸多实战中以统一的风格实现抓取帧–>处理帧–>输出处理结果这样的流程,咱们定义了一个DetectService接口,每种不同帧处理业务按照自己的特点来实现此接口即可(例如人脸检测、年龄检测、性别检测等)
- 先来回顾DetectService接口:
package com.bolingcavalry.grabpush.extend;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
import static org.bytedeco.opencv.global.opencv_core.CV_8UC1;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
public interface DetectService {
static Mat buildGrayImage(Mat src) {
return new Mat(src.rows(), src.cols(), CV_8UC1);
}
static Frame detect(CascadeClassifier classifier,
OpenCVFrameConverter.ToMat converter,
Frame rawFrame,
Mat grabbedImage,
Mat grayImage) {
cvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
RectVector objects = new RectVector();
classifier.detectMultiScale(grayImage, objects);
long total = objects.size();
if (total<1) {
return rawFrame;
}
for (long i = 0; i < total; i++) {
Rect r = objects.get(i);
int x = r.x(), y = r.y(), w = r.width(), h = r.height();
rectangle(grabbedImage, new Point(x, y), new Point(x + w, y + h), Scalar.RED, 1, CV_AA, 0);
}
objects.close();
return converter.convert(grabbedImage);
}
void init() throws Exception;
Frame convert(Frame frame);
void releaseOutputResource();
}
- 再来看看本次实战中DetectService接口的实现类BaiduCloudDetectService.java,有几处要注意的地方稍后会提到:
package com.bolingcavalry.grabpush.extend;
import com.bolingcavalry.grabpush.bean.response.FaceDetectResponse;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Rect;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
import org.opencv.face.Face;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_imgproc.CV_AA;
@Slf4j
public class BaiduCloudDetectService implements DetectService {
private Mat grabbedImage = null;
private String token;
private String base64Str;
private BaiduCloudService baiduCloudService;
private OpenCVFrameConverter.ToMat openCVConverter = new OpenCVFrameConverter.ToMat();
private Java2DFrameConverter java2DConverter = new Java2DFrameConverter();
private OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
private BASE64Encoder encoder = new BASE64Encoder();
public BaiduCloudDetectService(String token) {
this.token = token;
}
@Override
public void init() throws Exception {
baiduCloudService = new BaiduCloudService(token);
}
@Override
public Frame convert(Frame frame) {
base64Str = frame2Base64(frame);
long startTime = System.currentTimeMillis();
FaceDetectResponse faceDetectResponse = baiduCloudService.detect(base64Str);
if (null==faceDetectResponse
|| null==faceDetectResponse.getErrorCode()
|| !"0".equals(faceDetectResponse.getErrorCode())) {
String desc = "";
if (null!=faceDetectResponse) {
desc = String.format(",错误码[%s],错误信息[%s]", faceDetectResponse.getErrorCode(), faceDetectResponse.getErrorMsg());
}
log.error("检测人脸失败", desc);
return frame;
}
log.info("检测耗时[{}]ms,结果:{}", (System.currentTimeMillis()-startTime), faceDetectResponse);
if (null==faceDetectResponse.getResult()
|| null==faceDetectResponse.getResult().getFaceList()) {
log.info("未检测到人脸");
return frame;
}
List<FaceDetectResponse.Result.Face> list = faceDetectResponse.getResult().getFaceList();
FaceDetectResponse.Result.Face face;
FaceDetectResponse.Result.Face.Location location;
String desc;
Scalar color;
int pos_x;
int pos_y;
for (int i = 0; i < list.size(); i++) {
face = list.get(i);
location = face.getLocation();
int x = (int)location.getLeft();
int y = (int)location.getHeight();
int w = (int)location.getWidth();
int h = (int)location.getHeight();
if (1==face.getMask().getType()) {
desc = "Mask";
color = Scalar.GREEN;
} else {
desc = "No mask";
color = Scalar.RED;
}
rectangle(grabbedImage, new Point(x, y), new Point(x + w, y + h), color, 1, CV_AA, 0);
pos_x = Math.max(x-10, 0);
pos_y = Math.max(y-10, 0);
putText(grabbedImage, desc, new Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.5, color);
}
return converter.convert(grabbedImage);
}
@Override
public void releaseOutputResource() {
if (null!=grabbedImage) {
grabbedImage.release();
}
}
private String frame2Base64(Frame frame) {
grabbedImage = converter.convert(frame);
BufferedImage bufferedImage = java2DConverter.convert(openCVConverter.convert(grabbedImage));
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, "png", bStream);
} catch (IOException e) {
throw new RuntimeException("bugImg读取失败:"+e.getMessage(),e);
}
return encoder.encode(bStream.toByteArray());
}
}
- 整个BaiduCloudDetectService类,主要是对前面BaiduCloudService类的使用
- convert方法中,拿到frame实例后会转为base64字符串,用于提交到百度AI开放平台做人脸检测
- 百度AI开放平台的检测结果中有多个人脸检测结果,这里要逐个处理:取出每个人脸的位置,以此位置在原图画矩形框,然后根据是否戴口罩在人脸上做标记,戴口罩的是绿色标记(包括矩形框),不戴口罩的是红色矩形框
主程序
- 最后是主程序了,还是《JavaCV的摄像头实战》系列的套路,咱们来看看主程序的服务类定义好的框架
- 《JavaCV的摄像头实战之一:基础》创建的simple-grab-push工程中已经准备好了父类AbstractCameraApplication,所以本篇继续使用该工程,创建子类实现那些抽象方法即可
- 编码前先回顾父类的基础结构,如下图,粗体是父类定义的各个方法,红色块都是需要子类来实现抽象方法,所以接下来,咱们以本地窗口预览为目标实现这三个红色方法即可:
- 新建文件PreviewCameraWithBaiduCloud.java,这是AbstractCameraApplication的子类,其代码很简单,接下来按上图顺序依次说明
- 先定义CanvasFrame类型的成员变量previewCanvas,这是展示视频帧的本地窗口:
protected CanvasFrame previewCanvas
- 把前面创建的DetectService作为成员变量,后面检测的时候会用到:
private DetectService detectService;
- PreviewCameraWithBaiduCloud的构造方法,接受DetectService的实例:
public PreviewCameraWithBaiduCloud(DetectService detectService) {
this.detectService = detectService;
}
- 然后是初始化操作,可见是previewCanvas的实例化和参数设置,还有检测、识别的初始化操作:
@Override
protected void initOutput() throws Exception {
previewCanvas = new CanvasFrame("摄像头预览", CanvasFrame.getDefaultGamma() / grabber.getGamma());
previewCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
previewCanvas.setAlwaysOnTop(true);
detectService.init();
}
- 接下来是output方法,定义了拿到每一帧视频数据后做什么事情,这里调用了detectService.convert检测人脸并识别性别,然后在本地窗口显示:
@Override
protected void output(Frame frame) {
Frame detectedFrame = detectService.convert(frame);
previewCanvas.showImage(detectedFrame);
}
- 最后是处理视频的循环结束后,程序退出前要做的事情,先关闭本地窗口,再释放检测服务的资源:
@Override
protected void releaseOutputResource() {
if (null!= previewCanvas) {
previewCanvas.dispose();
}
detectService.releaseOutputResource();
}
@Override
protected int getInterval() {
return 0;
}
- 至此,功能已开发完成,再写上main方法,代码如下,请注意token的值是前面在百度AI开放平台取得的access_token:
public static void main(String[] args) {
String token = "21.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx.xxxxxxxxxx.xxxxxx-xxxxxxxx";
new PreviewCameraWithBaiduCloud(new BaiduCloudDetectService(token)).action(1000);
}
- 至此,代码写完了,准备好摄像头开始验证,群众演员为了免费盒饭已经在寒风中等了很久啦
验证
-
运行PreviewCameraWithBaiduCloud的main方法,请群众演员出现在摄像头前面,此时不戴口罩,可见人脸上是红色字体和矩形框: -
让群众演员戴上口罩,再次出现在摄像头前面,这次检测到了口罩,显示了绿色标注和矩形框: -
实际体验中,由于一秒钟最多只有两帧,在预览窗口展示时完全是幻灯片效果,惨不忍睹… -
本篇博客使用了群众演员两张照片,所以被他领走了两份盒饭,欣宸很心疼… -
至此,基于JavaCV和百度AI开放平台实现的口罩检测功能已完成,希望您继续关注《JavaCV的摄像头实战》系列,之后的实战更精彩
你不孤单,欣宸原创一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 数据库+中间件系列
- DevOps系列
|