Cheeta
Cheeta

Reputation: 437

XML files for Haar Cascades not loading in Visual Studio 2010 using OpenCV 2.4.3

I am trying to run this simple face detection program that uses haar cascades in Visual Studio 2010 using OpenCV 2.4.3.Now every thing compiles fine but it seems like that the xml files for haar cascades are not loading.I did a lot of finding on Google but couldn't find any solution that works.I have tried every possible way to make them load.I gave absolute paths,I copied them into projects working directory,I used double backslashes as separators in absolute paths but they don't just load.I even downloaded them form other sources thinking may be the ones included with the library may be invalid or corrupt but no success.I know I am making some very basic mistake here.Here is the code I am using.Please help me out.Thank you.

<pre><code>
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


using namespace cv;
void detectAndDisplay( Mat frame );

/** Global variables */
String face_cascade_name = "E:\Downloads\IDM Downloads\opencv\data\haarcascades\haarcascade_frontalface_alt.xml";

String eyes_cascade_name = "E:\Downloads\IDM Downloads\opencv\data\haarcascades\haarcascade_eye_tree_eyeglasses.xml";

CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
CascadeClassifier face_cascade;
/** @function main */
int main( int argc, const char** argv )
{

    // printf();
    CvCapture* capture;
    Mat frame;
    // face_cascade
    //-- 1. Load the cascades
    if(!face_cascade.load( face_cascade_name ) ){
        printf("--(!)Error loading\n");
        return -1;
    }

    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; }

    //-- 2. Read the video stream
    capture = cvCaptureFromCAM( 0 );
    if( capture )
    {
        while( true )
        {
            frame = cvQueryFrame( capture );

            //-- 3. Apply the classifier to the frame
            if( !frame.empty() )
            { detectAndDisplay( frame ); }
            else
            { printf(" --(!) No captured frame -- Break!"); break; }

            int c = waitKey(10);
            if( (char)c == 'c' ) { break; }
        }
    }
    return 0;
}

/** @function detectAndDisplay */
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 );
}
</code></pre>

The ouput command line window just prints
<pre><code>

 --(!)Error loading
 Press any key to continue . . .

Which means the xml files are not loading. Here is my project directory listing.I have put the xml files in every folder but still no success.


OpenCVTest_2
    |___Debug
    |___ipch
         |___opencvtest_2-365b6930
    |___OpenCVTest_2
         |___Debug
         |___Release
    |___Release

Upvotes: 2

Views: 4410

Answers (3)

Chandan Sharma
Chandan Sharma

Reputation: 2943

Probably try this

/** Global variables */

String face_cascade_name ="E:/Downloads/IDMDownloads/opencv/data/haarcascades/haarcascade_frontalface_alt.xml";
String eyes_cascade_name ="E:/Downloads/IDMDownloads/opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";

Upvotes: 0

Marco Maisto
Marco Maisto

Reputation: 101

Probably it is a little bit late for the answer, but maybe someone will search for it and will find it (as i have just done...).

I was having same problem as Cheeta. Trying to load with

String xmlFaceFilename = "haarcascade_frontalface_alt2.xml";
//or giving the exact path
String xmlFaceFilename = "C:\\...\\haarcascade_frontalface_alt2.xml";

doesn't work even for me.

I could make it work with:

String xmlFaceFilename = "..\\Debug\\haarcascade_frontalface_alt2.xml";

with the xml file copied into the Debug folder (where it can be found the .exe file).

Hope it could help someone! :)

Upvotes: 0

Roger Rowland
Roger Rowland

Reputation: 26279

It looks like you forgot to escape your backslashes:

/** Global variables */
String face_cascade_name = "E:\\Downloads\\IDM Downloads\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";

String eyes_cascade_name = "E:\\Downloads\\IDM Downloads\\opencv\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";

EDIT - sorry, I missed that in your original question - in any case the backslahes do need to be escaped, but it may be that the path is too long or must not contain spaces. So copy the xml files to the same folder as your .exe (and maybe your project folder too) and then use this

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";

String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";

Upvotes: 2

Related Questions