人工智能Java SDK:读取本地MP4文件,实时检测口罩

目录:

http://aias.top/

口罩检测

口罩检测助力抗击肺炎,人工智能技术正被应用到疫情防控中来。 抗疫切断传播途径中,
佩戴口罩已经几乎成为了最重要的举措之一。但是在实际场景中,仍然有不重视、不注意、
侥幸心理的人员不戴口罩,尤其在公众场合,给个人和公众造成极大的风险隐患。
而基于人工智能的口罩检测功能可以基于摄像头视频流进行实时检测。

SDK功能

读取本地MP4文件,实时检测口罩。

运行例子

  1. 首先下载例子代码
git clone https://github.com/mymagicpower/AIAS.git
  1. 运行例子代码:MP4FaceMaskDetectionExample
/**
 * 本地视频人脸检测
 *
 * @author Calvin
 */
public class MP4FaceMaskDetectionExample {
  public static void main(String[] args) throws IOException, ModelException, TranslateException {
    faceMaskDetection("src/test/resources/mask.mp4");
  }

  /**
   * 人脸检测
   *
   * @param input 视频源
   */
  public static void faceMaskDetection(String input)
      throws IOException, ModelException, TranslateException {
    float shrink = 0.5f;
    float threshold = 0.85f;
    Criteria<Image, DetectedObjects> criteria = new FaceDetection().criteria(shrink, threshold);
    Criteria<Image, Classifications> maskCriteria = new FaceMaskDetect().criteria();

    // 读取视频文件或者视频流获取图像(得到的图像为frame类型,需要转换为mat类型进行检测和识别)
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(input);
    grabber.start();

    // Frame与Mat转换
    OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();

    CanvasFrame canvas = new CanvasFrame("人脸检测"); // 新建一个预览窗口
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setVisible(true);
    canvas.setFocusable(true);
    // 窗口置顶
    if (canvas.isAlwaysOnTopSupported()) {
      canvas.setAlwaysOnTop(true);
    }
    Frame frame = null;

    try (ZooModel model = ModelZoo.loadModel(criteria);
        Predictor<Image, DetectedObjects> predictor = model.newPredictor();
        ZooModel classifyModel = ModelZoo.loadModel(maskCriteria);
        Predictor<Image, Classifications> classifier = classifyModel.newPredictor()) {
      // 获取图像帧
      for (; canvas.isVisible() && (frame = grabber.grabImage()) != null; ) {

        // 将获取的frame转化成mat数据类型
        Mat img = converter.convert(frame);
        BufferedImage buffImg = OpenCVImageUtil.mat2BufferedImage(img);

        Image image = ImageFactory.getInstance().fromImage(buffImg);
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        DetectedObjects detections = predictor.predict(image);
        List<DetectedObjects.DetectedObject> items = detections.items();

        // 遍历人脸
        for (DetectedObjects.DetectedObject item : items) {
          Image subImg = getSubImage(image, item.getBoundingBox());
          Classifications classifications = classifier.predict(subImg);
          String className = classifications.best().getClassName();

          BoundingBox box = item.getBoundingBox();
          Rectangle rectangle = box.getBounds();
          int x = (int) (rectangle.getX() * imageWidth);
          int y = (int) (rectangle.getY() * imageHeight);
          Rect face =
              new Rect(
                  x,
                  y,
                  (int) (rectangle.getWidth() * imageWidth),
                  (int) (rectangle.getHeight() * imageHeight));

          // 绘制人脸矩形区域,scalar色彩顺序:BGR(蓝绿红)
          rectangle(img, face, new Scalar(0, 0, 255, 1));

          int pos_x = Math.max(face.tl().x() - 10, 0);
          int pos_y = Math.max(face.tl().y() - 10, 0);
          // 在人脸矩形上面绘制文字
          putText(
              img,
              className,
              new Point(pos_x, pos_y),
              FONT_HERSHEY_COMPLEX,
              1.0,
              new Scalar(0, 0, 255, 2.0));
        }

        // 显示视频图像
        canvas.showImage(frame);
      }
    }

    canvas.dispose();
    grabber.close();
  }

  private static int[] extendSquare(
      double xmin, double ymin, double width, double height, double percentage) {
    double centerx = xmin + width / 2;
    double centery = ymin + height / 2;
    double maxDist = Math.max(width / 2, height / 2) * (1 + percentage);
    return new int[] {(int) (centerx - maxDist), (int) (centery - maxDist), (int) (2 * maxDist)};
    //    return new int[] {(int) xmin, (int) ymin, (int) width, (int) height};
  }

  private static Image getSubImage(Image img, BoundingBox box) {
    Rectangle rect = box.getBounds();
    int width = img.getWidth();
    int height = img.getHeight();
    int[] squareBox =
        extendSquare(
            rect.getX() * width,
            rect.getY() * height,
            rect.getWidth() * width,
            rect.getHeight() * height,
            0); // 0.18

    if (squareBox[0] < 0) squareBox[0] = 0;
    if (squareBox[1] < 0) squareBox[1] = 0;
    if (squareBox[0] > width) squareBox[0] = width;
    if (squareBox[1] > height) squareBox[1] = height;
    if ((squareBox[0] + squareBox[2]) > width) squareBox[2] = width - squareBox[0];
    if ((squareBox[1] + squareBox[2]) > height) squareBox[2] = height - squareBox[1];
    return img.getSubimage(squareBox[0], squareBox[1], squareBox[2], squareBox[2]);
    //    return img.getSubimage(squareBox[0], squareBox[1], squareBox[2], squareBox[3]);
  }
}

效果如下:

result

目录:

http://www.aias.top/

Git地址:

https://github.com/mymagicpower/AIAS
https://gitee.com/mymagicpower/AIAS

125分50秒---162.81M---LabView01---第10章数据的显示.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView01-第10章数据的显示.wmv 92分2秒---143.46M---LabView02---第11章文件的IO及保存.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView02-第11章文件的IO及保存.wmv 37分22秒---58.09M---LabView03---第12章LabVIEW的通信.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView03-第12章LabVIEW的通信.wmv 9分58秒---21.2M---LabView04---第13章LabVIEW与其它软件的连接.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView04-第13章LabVIEW与其它软件的连接.wmv 4分45秒---9.99M---LabView05---第14章LabVIEW中子VI的设置及调用.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView05-第14章LabVIEW中子VI的设置及调用.wmv 34分30秒---61.03M---LabView06---第15章其它高级技巧.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView06-第15章其它高级技巧.wmv 9分41秒---11.37M---LabView07---第2章第一个LabVIEW例子.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView07-第2章第一个LabVIEW例子.wmv 86分58秒---140.25M---LabView08---第3章基本元素.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView08-第3章基本元素.wmv 76分48秒---143.72M---LabView09---第4章程序结构.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView09-第4章程序结构.wmv 5分46秒---14.67M---LabView10---第5章数据采集.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView10-第5章数据采集.wmv 12分50秒---18.35M---LabView11---第6章数据传输及电脑接口.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView11-第6章数据传输及电脑接口.wmv 59分57秒---87.83M---LabView12---第7章数据的分析及处理.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView12-第7章数据的分析及处理.wmv 42分4秒---61.02M---LabView13---第8章LabVIEW的信号调理.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView13-第8章LabVIEW的信号调理.wmv 132分19秒---178.86M---LabView14---第9章分析处理中数学计算.wmv---F:/Resource/video/硬件/AltiumDesigner/LabView14-第9章分析处理中数学计算.wmv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值