opencv人脸识别-海康网络摄像头

本文介绍了如何配置OpenCV环境,通过网络摄像头采集人脸图片,并进行人脸模型训练。强调了在人脸对比实时显示时可能出现的卡顿问题,并推荐通过Netty优化。最后列举了关键方法和参考资料。
摘要由CSDN通过智能技术生成

配置环境

管网下载地址:https://opencv.org/releases/
ps:高版本的管网jar中只有基础方法,功能比较全的jar包可以通过管网源码编译生成或者网上下载
在这里插入图片描述
根据环境下载匹配的包,执行下载文件自动解压出环境文件,在path环境变量中添加路径:D:\workSoft\opencv\build\x64\vc14\bin

文件说明

环境中包含基础方法jar包及人脸模型文件
在这里插入图片描述
在这里插入图片描述

人脸采集

采集10张人脸图片,保存到指定目录,命名规则:path\name\img\name-count.jpg
网络摄像头可以通过直接访问或者流媒体推流地址进行访问,可以先用VLC测试视频是否正常

ps:识别的人脸图片可能受环境影响,有可能会将环境误识别为人脸
例如:下面的图片测试的时候会标识出了2个人脸标记
在这里插入图片描述

先将jar包引入到工程中

JFrame测试代码

public class FaceCollect {
   
	//模型文件路径
    public static String modelXml = "D:\\workSoft\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
    public static String name = "";
    //摄像头直接访问地址
    public static String url = "rtsp://name:password@ip:554/Streaming/Channels/1";
    //推流访问地址
//    public static String url = "http://xxx/live?port=1935&app=myapp&stream=34020000002000000001_34020000001320000001_Play";
    public static boolean flag = true;
    //图片保存路径
    public static String path = "C:\\Users\\yf\\Desktop\\faceTest\\";
    public static int count = 0;
    public static long startTime;

    public static void main(String[] args) {
   

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println("Please input person name: ");
        Scanner scanner = new Scanner(System.in);
        name = scanner.next();
        System.out.println("collecting images for : " + name);
        //新建窗口
        JFrame cameraFrame = new JFrame("camera");
        cameraFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        cameraFrame.setSize(1280, 720);
        cameraFrame.setBounds(0, 0, cameraFrame.getWidth(), cameraFrame.getHeight());
        VideoPanel videoPanel = new VideoPanel();
        cameraFrame.setContentPane(videoPanel);
        cameraFrame.setVisible(true);

        CascadeClassifier faceCascade = new CascadeClassifier();
        //获取模型文件
        faceCascade.load(modelXml);
        //调用摄像头
        VideoCapture capture = new VideoCapture(url);

        try {
   
//            capture.open(0);
            if (capture.isOpened()) {
   
                Mat image = new Mat();
                while(flag) {
   
                    capture.read(image);
                    if (!image.empty()) {
   
                        detectAndDisplay(image, faceCascade);
                        videoPanel.setImageWithMat(image);
                        cameraFrame.repaint();
                    } else {
   
                        break;
                    }
                }
            }
        }catch (Exception e) {
   
            System.out.println(e.getMessage());
        }finally {
   
            capture.release();
        }
        cameraFrame.repaint();
        cameraFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        cameraFrame.dispose();
    }

    /**
     * 框出人脸
     * @param frame
     * @param faceCascade
     */
    public static void detectAndDisplay(Mat frame, CascadeClassifier faceCascade){
   
        MatOfRect faces = new MatOfRect();
        Mat grayFrame = new Mat();

        //convert the frame in gray scale
        Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
        //equalize the frame histogram to improve the result
        Imgproc.equalizeHist(grayFrame, grayFrame);

        //compute minimum face size (20% of the frame height, in our case)
        int absoluteFaceSize = 0;
        int height = grayFrame.rows();
        if (Math.round(height * 0.2f) > 0) {
   
            absoluteFaceSize = Math.round(height * 0.2f);
        }

        //detect faces
        faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, Objdetect.CASCADE_SCALE_IMAGE, new Size(absoluteFaceSize, absoluteFaceSize), new Size());
        //each rectangle in faces is a face: draw them!
        Rect[] facesArray = faces.toArray();
        //保存人脸图片
        if (facesArray.length==1) {
   
            if ((count == 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值