Ask Your Question
0

Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale thrown at runtime

asked 2017-03-29 10:16:35 -0600

meg gravatar image

I am trying to do real-time face, nose and eyes detection the following is my code. The problem is that nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30)); statement generates run-time exception and I don't know the reason.

The exception is: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale

What is the problem can someone please tell me. I have the xml files in the right directories and the rest of the code is working fine except that particular line.

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

#include <iostream>

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";
String nose_cascade_name = "haarcascade_mcs_nose.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier nose_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);

/** @function main */
int main(int argc, const char** argv)
{
    VideoCapture vcap(0);
    if (!vcap.isOpened()) {
        cout << "Error opening video stream" << endl;
        return -1;
    }
    Mat frame;

    //-- 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

        while (true)
        {
            vcap >> frame;
            //-- 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 (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
        rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(255, 0, 0), 3, 8);
        Mat faceROI = frame_gray(faces[i]);
        vector<Rect> eyes;
        vector<Rect> noses;
        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        for (size_t 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);
        }
        nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        for (size_t j = 0; j < noses.size(); j++) {
            Point center(faces[i].x + noses[j].x + noses[j].width*0.5, faces[i].y + noses[j].y + noses[j].height*0.5);
            int radius = cvRound((noses[j].width + noses[j].height)*0.25);
            circle(frame, center, radius, Scalar(0, 0, 255), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}
edit retag flag offensive close merge delete

Comments

1

Looks like it cannot load the classifier file. Try to put the cascade files in the same folder than the executable or use absolute pathname.

Eduardo gravatar imageEduardo ( 2017-03-29 10:31:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-29 10:33:52 -0600

berak gravatar image

updated 2017-03-29 10:37:57 -0600

look at your code, again, you only load the face & eye cascades, but never your nose one. there should be a line like:

    if (!nose_cascade.load(nose_cascade_name)) {
         printf("--(!)Error loading the nose cascade\n"); return -1; 
    };
edit flag offensive delete link more

Comments

Thanks a lot that saved my life

meg gravatar imagemeg ( 2017-03-29 10:53:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-29 10:16:35 -0600

Seen: 5,597 times

Last updated: Mar 29 '17