欢迎来到我的博客小站。  交流请加我微信好友: studyjava。  也欢迎关注同名公众号:Java学习之道

JavaCV开发详解之6:基于 OpenCV 的人脸识别

  |   0 评论   |   0 浏览

引入依赖

使用这个程序之前必须先安装配置 OpenCV,并将其 jar 包引入项目 opencv-440.jar

也可下载如下文件,解压后放在项目中和 src 同级的 lib 文件夹下(如没有 lib 文件夹,创建一个即可)

haarcascades.zip

opencv440.jar

运行程序所需 dll 文件,下载 opencv 并安装,就能获取。

代码实现

package com.java.puwang.opencv;
 
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
 
import java.io.File;
import java.io.InputStream;
 
import static org.opencv.highgui.HighGui.imshow;
 
/**
 * @author :created by mmzsblog.cn
 * @description:基于openc的人脸识别
 * @date :created at 2020/08/07 16:02
 */
@Slf4j(topic = "【基于openc的人脸识别】")
public class FaceID {
    // mac 地址
//    public static String LOCAL_PATH = "/Users/pw/gitRepository/springboot-rtmp/lib/libjli.dylib/haarcascades/";
    // windows 地址
    public static String LOCAL_PATH = "D:\\softwareIT\\opencv4.3.0\\opencv\\sources\\data\\haarcascades\\";
 
    /**
     * OpenCV人脸识别
     *
     * @param rgb
     * @param gray
     */
    public static void faceRecognition(Mat rgb, Mat gray) {
 
        log.info("加载的人脸特征识别文件是:" + LOCAL_PATH + "haarcascade_frontalface_alt.xml");
        // 读取OpenCV的人脸特征识别文件
        CascadeClassifier cascade = new CascadeClassifier(LOCAL_PATH + "haarcascade_frontalface_alt.xml");
        //在路径上没有找到相关文件返回1
        if (cascade.empty()) {
            log.info("人脸识别文件读取失败");
            //return;
        }
 
        MatOfRect rect = new MatOfRect();
        //检测人脸
        cascade.detectMultiScale(gray, rect);
        // 为每张识别到的人脸画一个圈
        for (Rect re : rect.toArray()) {
 
            Imgproc.rectangle(rgb, new Point(re.x, re.y), new Point(re.x + re.width, re.y + re.height), new Scalar(0, 0, 255));
 
        }
        //图形界面显示
        imshow("人脸识别", rgb);
 
    }
 
    /**
     * OpenCV人眼识别
     *
     * @param rgb
     * @param gray
     */
    public static void eyeRecognition(Mat rgb, Mat gray) {
 
        // 读取OpenCV的人眼特征识别文件
        CascadeClassifier cascade = new CascadeClassifier(LOCAL_PATH + "haarcascade_eye_tree_eyeglasses.xml");
        if (cascade.empty()) {
            log.info("人眼识别文件读取失败");
            return;
        }
 
        MatOfRect rect = new MatOfRect();
 
        cascade.detectMultiScale(gray, rect);
 
        for (Rect re : rect.toArray()) {
            Imgproc.rectangle(rgb, new Point(re.x, re.y), new Point(re.x + re.width, re.y + re.height), new Scalar(255, 0, 0));
 
        }
 
//         HighGui.imshow("人脸识别", rgb);
 
    }
 
    private static void loadLibraries() {
 
        try {
            InputStream in = null;
            File fileOut = null;
            String osName = System.getProperty("os.name");
            String opencvpath = System.getProperty("user.dir");
 
            //windows
            if (osName.startsWith("Windows")) {
                int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
                //32位系统
                if (bitness == 32) {
                    opencvpath = opencvpath + "\\lib\\opencv\\x86\\opencv_java440.dll";
                } else if (bitness == 64) {
                    //64位系统
                    opencvpath = opencvpath + "\\lib\\opencv\\x64\\opencv_videoio_ffmpeg440_64.dll";
                } else {
                    opencvpath = opencvpath + "\\lib\\opencv\\x86\\opencv_java440.dll";
                }
            } else if (osName.equals("Mac OS X")) {
                // mac os
                opencvpath = opencvpath + "\\lib\\libjli.dylib";
            }
            log.info("加载的 opencv 支持文件路径是:" + opencvpath);
            System.load(opencvpath);
        } catch (Exception e) {
            throw new RuntimeException("Failed to load opencv native library", e);
        }
    }
 
    public static void main(String[] args) {
        // no opencv_java430 in java.library.path
        // 把..opencv\build\java\x64 下的文件 opencv_videoio_ffmpeg440_64.dll 复制到 Java JDK 安装的 bin 目录下就解决
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//        loadLibraries();
 
 
        // 实例化相机
        VideoCapture videoCapture = new VideoCapture();
        // 如果要从摄像头获取视频 则要在 VideoCapture 的构造方法写 0
        if (!videoCapture.open(0,700)) {
            log.info("相机打开失败");
            return;
        }
 
        while (true) {
            Mat img = new Mat();
 
            if (!videoCapture.read(img)) {
                return;
            }
 
            // 灰度化
            Mat rgb = new Mat();
            Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
 
            Mat gray = new Mat();
            Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
 
            faceRecognition(img, gray);
            eyeRecognition(img, gray);
 
            HighGui.waitKey(10);
        }

    }
 
}

标题:JavaCV开发详解之6:基于 OpenCV 的人脸识别
作者:mmzsblog
地址:https://www.mmzsblog.cn/articles/2020/09/13/1600003352356.html
-----------------------------
如未加特殊说明,此网站文章均为原创。
网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
公众号转载请联系网站首页的微信号申请白名单!

个人微信公众号 ↓↓↓                 

微信搜一搜 Java 学习之道