Java之opencv人脸识别

来源:互联网 发布:linux 打开文件夹 编辑:程序博客网 时间:2024/05/22 03:41

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

原创粉丝点击