如何检测网络摄像头并比较本地文件以匹配人脸 OpenCV

how can i detect webcam and compare the local file to match face OpenCV

本文关键字:文件 OpenCV 比较 何检测 检测 摄像头 网络      更新时间:2023-10-16

突出显示的代码表明openCV框架已加载到我的C代码中,并且它呈现了警察监视。这只是为了证明它非常流畅且

编写的代码非常干净。

目标:我的网络CAM已连接到USB端口。我想捕获实时网络摄像头图像并从本地文件(/tmp/myface.png)匹配,如果实时网络摄像头与本地文件 myface.png 匹配,它将显示文本"警察观看"

我现在如何在以下代码上捕获我的 webCAM? 2)捕获 webCAM 时,我如何加载文件并查找它是否匹配,在匹配时它仅显示该文本。

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

#include "opencv/cv.h"
void detectAndDisplay(Mat frame);
//*************

 // Set Region of Interest
    cv::Rect roi_b;
    cv::Rect roi_c;
    size_t ic = 0; // ic is index of current element
    int ac = 0; // ac is area of current element
    size_t ib = 0; // ib is index of biggest element
    int ab = 0; // ab is area of biggest element
    stringstream ssfn;
//*************
 CascadeClassifier face_cascade;
string window_name = "Capture - Face detection";
int filenumber; // Number of file to be saved
string filename;
Mat frameread = imread("test.jpg");

int main(int argc, const char *argv[]){
     if (argc != 4) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
        cout << "t <device id> -- The webcam device id to grab frames from." << endl;
      //  exit(1);
    }
    CascadeClassifier face_cascade;
    CascadeClassifier face_cascade1;
    String fn="C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml";
    String fn1="C:\opencv\sources\data\haarcascades\haarcascade_eye.xml";
    face_cascade.load(fn);
    face_cascade1.load(fn1);
    VideoCapture input(0);
    if(!input.isOpened()){return -1;}
    namedWindow("Mezo",1);
    Mat f2;
    Mat frame;
            std::vector<Rect> faces,faces1;
            CvCapture* capture1;
            IplImage* f1;
            Mat crop;
            cv::Rect r;
    //  detectAndDisplay(frameread);
    while(1)
    {
        ic=0;
        ib=0;
        ab=0;
        ac=0;
        input >> frame;
        waitKey(10);
        //cvtColor(frame, frame, CV_BGR2GRAY);
        //cv::equalizeHist(frame,frame);
        face_cascade.detectMultiScale(frame, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));
        for(int i=0; i < faces.size();i++)
        {
            Point pt1(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
            Point pt2(faces[i].x,faces[i].y);
            Mat faceROI = frame(faces[i]);
            face_cascade1.detectMultiScale(faceROI, faces1, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30,30));
            for(size_t j=0; j< faces1.size(); j++)
            {
                Point center(faces[i].x+faces1[j].x+faces1[j].width*0.5, faces[i].y+faces1[j].y+faces1[j].height*0.5);
                int radius = cvRound((faces1[j].width+faces1[j].height)*0.25);
                circle(frame, center, radius, Scalar(255,0,0), 2, 8, 0);
            }
            rectangle(frame, pt1, pt2, cvScalar(0,255,0), 2, 8, 0);

        }
        imshow("Result", frame);
        waitKey(3);
        char c = waitKey(3);
        if(c == 27)
            break;
    }
    return 0;
}

你问的可能是人脸识别。你应该在你的问题中更清楚。

Opencv有一个完美识别的类,而不是像你想象的那样。

这项技术有很多方法可用,Opencv 有三种算法。此外,您还需要准备图像数据库(标记的人脸)

所有这些步骤都在opencv文档中进行了描述 http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html,并提供了一些示例:只是您需要阅读并申请。

在这里,您还可以找到适合初学者的好教程。