Question

I am trying to make a face detection software using OpenCV 2.3.0. While OpenCV 2.4 has the face recognizer class 2.3.0 is devoid of this feature. I checked the documentation and is specifies that the detectMultiScale function has the following declaration

void CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, double      
scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size())

Now I am giving the image, that is the camera feed, but don't know what to fill in vector block. Here is the code that I have written.

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include<iostream>

using namespace cv;
using namespace std;

int main()
{
    std::vector<Rect> faces;
    VideoCapture cap(0);

    if(!cap.isOpened())
        cout<<"Camera is not connected"<<endl;
    cv::CascadeClassifier* cascade=0;
    if(cascade.empty())
        return -1;
    Mat edges;
    namedWindow("Camera Feed",1);
    for(;;)
    {
        Mat frame;
        cap >> frame;
        imshow("Camera Feed", frame);
        if(waitKey(10)==27)
            break;
        cascade.detectMultiScale(frame,faces);
    }
    return 0;
}

Question: How to proceed further?

Was it helpful?

Solution

go through Docs OpenCV. check here for detail example

void detectAndDisplay( Mat frame )
{
  std::vector<Rect> faces;
  Mat frame_gray;

  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );

  //-- Detect faces
  face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

  for( int i = 0; i < faces.size(); i++ )
  {
    Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
    ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

    Mat faceROI = frame_gray( faces[i] );
    std::vector<Rect> eyes;

    //-- In each face, detect eyes
    eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( int j = 0; j < eyes.size(); j++ )
     {
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
       int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
       circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
     }
  }
  //-- Show what you got
  imshow( window_name, frame );
 }

hope this will help you

OTHER TIPS

Here is the 2.3 documentation http://www.opencv.org.cn/opencvdoc/2.3.2/html/index.html

Use the circle or rectangle function to draw the rectangles contained in the faces vector. Then use imshow to display the result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top