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

时间:2014-12-17 09:40:57

标签: c++ c opencv

突出显示的代码演示了openCV框架在我的C代码中加载并呈现警察观看。这只是为了证明它可以非常流畅且非常干净地编写代码。

目标:我的webCAM已连接到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;
}

1 个答案:

答案 0 :(得分:2)

您所询问的可能是面部识别。你的问题应该更清楚。

Opencv有一个完美的识别课程,而不是你想的那样。

这项技术有很多种方法,Opencv有三种算法。您还需要准备图像数据库(标记为面部)

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

Here您还可以为初学者找到一个很好的教程。