Ask Your Question

OpenCv and Visual C++ Face detection

asked 2017-07-18 22:58:40 -0500

blink gravatar image

updated 2017-07-18 23:49:07 -0500

I recently came across this video which shows how to write a face recognition program and i tried using his codes to study how the program works. He indicated to change one of the lines and i followed as he said. I tried changing the line but it sill showed this error.

Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale and
cv::Exception at memory location 0x000000000029EE10

Unhandled exception at 0x000007FEFD5CA06D in opencvTry.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000029EE10. occurred

My program - try.cpp:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
    // capture from web camera init

    VideoCapture cap(0);;

    Mat img;

    // Initialize the inbuilt Harr Cascade frontal face detection
    // Below mention the path of where your haarcascade_frontalface_alt2.xml file is located

    CascadeClassifier face_cascade;
    // i tried changing this line to match my folder in C Drive

    for (;;)

        // Image from camera to Mat

        cap >> img;

        // obtain input image from source
        cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);

        // Just resize input image if you want
        resize(img, img, Size(1000, 640));

        // Container of faces
        vector<Rect> faces;

        // Detect faces
        face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));
        // error message appears here

        //Show the results
        // 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);
            ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

        // To draw rectangles around detected faces
        /* for (unsigned i = 0; i<faces.size(); i++)
        rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/

        imshow("wooohooo", img);
        int key2 = waitKey(20);

    return 0;
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-07-19 00:11:51 -0500

berak gravatar image

updated 2017-07-19 01:26:10 -0500

you have to avoid single backslashes in ALL kind of filenames, this is not even an opencv specific problem. so load your cascade like this:


or like this:


next, please do not blindly copy & paste random code lines, e.g. this:

 cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);

will have to be removed.

and really, look at the docs , not at youtube videos ...

edit flag offensive delete link more


Thanks for the solution and also, i will heed your advice and look through docs. I actually did try and use codes from there before but there was error and so i kept on looking for alternatives, which is my biggest problem. I just don't understand how OpenCV works even though i read up the study materials.

blink gravatar imageblink ( 2017-07-19 01:38:55 -0500 )edit

answered 2018-03-23 16:43:04 -0500

I think there is an error on your Vistual Studio configuration. You problably put two libs on "Addticional Dependencies" configuration (opencv_world320d.lib and opencv_world320.lib). Try to use only one. The first (opencv_world320d.lib) is used for debug configuration.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-07-18 22:58:40 -0500

Seen: 1,266 times

Last updated: Jul 19 '17