栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

人工智能Java SDK:读取本地摄像头,实时检测人脸

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

人工智能Java SDK:读取本地摄像头,实时检测人脸

人脸检测

人脸识别技术目前已经广泛应用于包括人脸门禁系统、刷脸支付等各行各业。随着人脸识别技术的提升,应用越来越广泛。目前中国的人脸识
别技术已经在世界水平上处于领先地位,在安防行业,国内主流安防厂家也都推出了各自的人脸识别产品和解决方案,泛安防行业是人脸识别技术主要应用领域。
人脸识别是一项热门的计算机技术研究领域,它属于生物特征识别技术,是对生物体(一般特指人)本身的生物特征来区分生物体个体。
生物特征识别技术所研究的生物特征包括脸、指纹、手掌纹、虹膜、视网膜、声音(语音)、体形、个人习惯(例如敲击键盘的力度和频率、签字)等,
相应的识别技术就有人脸识别、指纹识别、掌纹识别、虹膜识别、视网膜识别、语音识别(用语音识别可以进行身份识别,也可以进行语音内容的识别,
只有前者属于生物特征识别技术)、体形识别、键盘敲击识别、签字识别等。

SDK功能

读取本地摄像头,实时检测人脸。

运行人脸检测的例子
  1. 首先下载例子代码
git clone https://github.com/mymagicpower/AIAS.git
  1. 导入examples项目到IDE中:
cd camera_face_sdk
  1. 运行例子代码:CameraFaceDetectionExample

public class CameraFaceDetectionExample {
  public static void main(String[] args) throws IOException, ModelException, TranslateException {
    faceDetection();
  }

  
  public static void faceDetection()
      throws IOException, ModelException, TranslateException {
    float shrink = 0.5f;
    float threshold = 0.7f;

    Criteria criteria = new FaceDetection().criteria(shrink, threshold);

    // 开启摄像头,获取图像(得到的图像为frame类型,需要转换为mat类型进行检测和识别)
    OpenCVframeGrabber grabber = new OpenCVframeGrabber(0);
    
    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 predictor = model.newPredictor()) {
      // 获取图像帧
      for (; canvas.isVisible() && (frame = grabber.grab()) != 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 items = detections.items();

        // 遍历人脸
        for (DetectedObjects.DetectedObject item : items) {
          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,
              "Face",
              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();
  }
}

效果如下:

目录:

http://www.aias.top/

Git地址:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号