user16449543
user16449543

Reputation: 1

Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale

I have been trying to get to understand how OpenCV and facial recognition works, but I keep getting an error message.

I get the following error Message: OpenCV: terminate handler is called! The last OpenCV error is:

OpenCV(4.5.3) Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\build\master_winpack-build-win64-vc15\opencv\modules\objdetect\src\cascadedetect.cpp, line 1689

can someone please tell me what this error message means?

The program crashes after running for a few seconds and I don't get to see the video feed. After failing a few times with my own code, I ended up directly copying code from Tutorials. I used code from "https://www.youtube.com/watch?v=RY6fPxpN10E" and still get an error message.

(Main.cpp)

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv )
{
    double scale = 1.0;

    cv::CascadeClassifier faceCascade;
    faceCascade.load("C:/ComputerVision/opencv/build/etc/haarcascades_frontalface_alt.xml");

    cv::VideoCapture video(0);

    if(video.isOpened())
    {
      std::cout<<"Video funktioniert"<<std::endl;
    }

    //Frame speichern
    cv::Mat frame;

    //Loop yur Ausgabe von Video
    while (video.read(frame))
    {
        video >> frame;

        cv::Mat grayscale;
        cvtColor(frame, grayscale, cv::COLOR_BGR2GRAY);

        resize(grayscale, grayscale, Size(grayscale.size().width / scale, grayscale.size().height / scale));

        vector<Rect> faces;
        faceCascade.detectMultiScale(grayscale, faces, 1.1, 3.0, 0, Size(30, 30));

        for (Rect area : faces)
        {
            Scalar drawColour = Scalar(255, 0, 0);
            rectangle(
                frame,
                Point(
                    cvRound(area.x * scale),
                    cvRound(area.y * scale)),
                Point(
                    cvRound((area.x + area.width - 1) * scale),
                    cvRound((area.y + area.height - 1) * scale)),
                drawColour);

        }

        cv::imshow("video feed", frame);

        if (cv::waitKey(25) >= 0)
        {
            break;
        }
    }


    return 0;
}

(.pro)

    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += \
            main.cpp
    
    INCLUDEPATH += C:\ComputerVision\opencv\build\include\
    
    LIBS += C:\ComputerVision\opencv\build\x64\vc15\lib\opencv_world453.lib

Upvotes: -1

Views: 1722

Answers (2)

Son H. Nguyen
Son H. Nguyen

Reputation: 416

The error occurred because the haarcascades_frontalface_alt.xml wasn't found.

You don't need hardcoding the file path in your code because it's already included in opencv lib. Try this instead:

faceDetector.load (cv::samples::findFile ("haarcascades/haarcascade_frontalface_alt.xml"));

Upvotes: 0

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15518

OpenCV uses assertions for error checking. That may be considered lazy because failed assertions aren't exceptions that can be caught (in C++ anyway, AFAIK).

This specific assertion says that you created a Cascade Classifier but the xml file wasn't found or there was some other issue in loading the classifier data. To arrive at this conclusion, you would read the OpenCV source at the specified location. You'd find the assertion, and you'd dive into empty(), which checks precisely that situation.

You can check this situation right after the .load() call yourself:

assert(!faceCascade.empty());

Which is the same test as in the assertion, just in your own code, and before you try to use the cascade classifier.

Upvotes: 0

Related Questions