Opencv 3.0 Error loading face cascade

2

Hi i have implemented opencv library. It is working for some code like video capturing or running video from file. but when i implement the code for face detection or object detection or for motion detection program. currently i have implemented this program.

#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main(void)
{
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return -1; };

    //-- 2. Read the video stream
    capture.open(-1);
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);

        int c = waitKey(10);
        if ((char)c == 27) { break; } // escape
    }
    return 0;
}

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

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

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

    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 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 | CASCADE_SCALE_IMAGE, Size(30, 30));

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

When I try to debug it gives me error The program '[7912] ConsoleApplication1.exe' has exited with code -1 (0xffffffff).

When I try start without debugging it gives me an error Error loading face cascade.

I found also one thing,one warning message while debugging is C:\Users\rushikesh\Documents\Visual Studio 2013\Projects\ConsoleApplication1\x64\Debug\opencv_world300d.dll'. Cannot find or open the PDB file.

but i check there is world300d.dll. Some programs of opencv 3.0.0 is running, So i guess i have configured it right, but few program especially track objects or motion or detect the face is not running and giving me the same error.

Edit After trying as per the suggestion of @srslynow i got following error.

enter image description here

c++
debugging
opencv
visual-c++
visual-studio-2013
asked on Stack Overflow Jul 4, 2015 by user3471546 • edited Jul 5, 2015 by user3471546

4 Answers

1

Your program cannot find the .xml files. Note that the default working directory when running your program from the Visual Studio IDE is NOT where your .exe is. It is the root directory of your project.

Possible solutions:

  • Move the xml files to your project root
  • Change the working directory (under project > properties > debugging) to $(SolutionDir)$(Platform)\$(Configuration)\

edit:

Capturing the default video device is done by using capture.open(0); This might be the cause for exiting the program with a -1 status, I'm assuming you do actually have a webcam on your machine?

answered on Stack Overflow Jul 4, 2015 by srslynow • edited Jul 4, 2015 by srslynow
1

One suggestion related to haar cascades. The best detector file I found is this one:

haarcascade_frontalface_alt2.xml

I did thousands of tests and this was the best file.

answered on Stack Overflow Jul 8, 2015 by Cookie Monster
0

If you are using Visual Studio, the problem could be also the compiler version between your application and the opencv binary. Exaple: if you are using VS 2013 that corresponde to compiler "vc120" but your are linking the opencv binary build with Visual Studio 2010 ("vs100"), you could have that error.
In this case go to properties of the project: Project-> Properties-> Select General Section on the left under "Configuration Properties" and on the right change the property "Platform Toolset" to "Visual Studio 2010 (v100)".
This should work!

answered on Stack Overflow Jul 12, 2016 by Rudy
0

Maybe you should use absolute path containing XML file.

answered on Stack Overflow Jun 11, 2017 by TrungManucian • edited May 31, 2019 by double-beep

User contributions licensed under CC BY-SA 3.0