Opencv c++ face detection code not working

702 views Asked by At

I'm trying to run the following code for face detection but there is a problem in

circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );

Full code:

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include<highgui.h>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "1.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);

    Mat image=cvarrToMat(img);  
    CascadeClassifier face_cascade;
    face_cascade.load( "haarcascade_frontalface_alt2.xml" );

    std::vector<Rect> faces;
    face_cascade.detectMultiScale( image, faces, 1.1, 2,         0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    // Draw circles on the detected faces
    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 );
        circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
    }


    imshow("Detected Face",image);
    waitKey(0);                   
    return 0;
}
2

There are 2 answers

0
francis On

Please check the return value of face_cascade.load( "haarcascade_frontalface_alt2.xml"). Otherwise, nothing happens if the file haarcascade_frontalface_alt2.xml is not found.

I may not have the same version of opencv as yours, but here is a code based on yours that produces an acceptable output. The path to the include files and haarcascade_frontalface_alt2.xml may need to be changed.

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include<opencv/highgui.h>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
    IplImage* img = cvLoadImage( "2.jpg" );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);

    Mat image=cvarrToMat(img);  
    CascadeClassifier face_cascade;
    // face_cascade.load( "haarcascade_frontalface_alt2.xml" );
    String face_cascade_name = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml";
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
    std::vector<Rect> faces;
    face_cascade.detectMultiScale( image, faces, 1.1, 2,         0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    // Draw circles on the detected faces
    for( int i = 0; i < faces.size(); i++ )
    {
        Point center=Point( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        circle( image, center,faces[i].width/2,Scalar( 255, 0, 255 ), 4, 8, 0 );
    }


    imshow("Detected Face",image);

    try {
        imwrite("alpha.jpg", image);
    }
    catch (runtime_error& ex) {
        fprintf(stderr, "Exception converting image to jpg format: %s\n", ex.what());
        return 1;
    }

    waitKey(0);                   
    return 0;
}

The code above it compiled by :

gcc -fPIC main.cpp -o main3 -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_objdetect -I /usr/local/include

Based on this image in the public domain, i got this : enter image description here

4
herohuyongtao On

That's because you didn't call circle() correctly, which doesn't accept 10 input parameters:

//! draws the circle outline or a solid circle in the image
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
                       const Scalar& color, int thickness=1,
                       int lineType=8, int shift=0);

For your case, you should call it as follows:

circle(image, center, faces[i].width*0.5, Scalar(255, 0, 255), 4, 8, 0);