Java之opencv人脸识别

OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类 构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了 图像处理和计算机视觉方面的很多通用算法。
OpenCV用C++语言编写,它的主要接口也是C++语言,但是依然保留了大量的C语言 接口。该库也有大量的Python, Java and MATLAB/OCTAVE (版本2.5)的接口。这些语言的API接口函数可以通过在线 文档获得。如今也提供对于C#,Ch, Ruby的支持。

1.加载dll资源库

static {
    System.loadLibrary("libs/x64/opencv_java246");
}

2.获取图片人脸范围

/**
 * 获取人脸范围
 * @param fileName
 * @return
 */
public static MatOfRect detectFace(String fileName) {
    CascadeClassifier faceDetector = new CascadeClassifier("libs/lbpcascade_frontalface.xml");
    Mat image = Highgui.imread(fileName);
    MatOfRect faceDetections = new MatOfRect();
    // 指定人脸识别的最大和最小像素范围
    Size minSize = new Size(120, 120);
    Size maxSize = new Size(250, 250);
    // 参数设置为scaleFactor=1.1f, minNeighbors=4, flags=0 以此来增加识别人脸的正确率
    faceDetector.detectMultiScale(image, faceDetections, 1.1f, 4, 0,
            minSize, maxSize);
    return faceDetections;
}

3.获取人脸范围并在图片人脸处画矩形框

/**
 * Detects faces in an image, draws boxes around them, and writes the results
 * @param fileName
 * @param destName
 */
public static void drawRect(String fileName, String destName){
    Mat image = Highgui.imread(fileName);
    // Create a face detector from the cascade file in the resources
    // directory.
    CascadeClassifier faceDetector = new CascadeClassifier("libs/lbpcascade_frontalface.xml");
    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
    }

    Highgui.imwrite(destName, image);

}
同理:身份证号提取也是先判断人脸处,然后判断身份证号位置,再利用tess4j提取图片中的身份证号
具体代码请移步github  https://github.com/followwwind/javautils

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值