Ask Your Question

opencv object detection tutorial not working

asked 2018-04-02 14:25:49 -0500

ellisa4 gravatar image

So I have followed the object detection tutorial to a "T" and it doesn't work. Just to double check for any spelling or syntax errors, I copied and pasted the website's code into my project and changed my code to "old_main.cpp." The actual problem, is that "cmake ." and "make" return's no errors and when I run the program, "./ASL-Interpreter," I see my video but no cascade. Thanks in advance

tutorial I'm using ->

my "project" ->

my CMakeLists.txt ->

./src/main/cpp/main.cpp ->

[root@Ellis master]# find / -name "haarcascade_frontalface_alt.xml" -print



that leads me to believe that it is there. Anything else I can try to trouble shoot?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-02 16:47:40 -0500

phillity gravatar image

updated 2018-04-02 17:58:51 -0500

One way I suggest to solve your issue loading the cascades is downloading them, putting them in the same directory as your cpp program and hardcoding their filenames into your program.

You can find and downlaod the cascades used in the tutorial here.

Download these and move them into the directory containing your cpp file. Then delete these lines:

CommandLineParser parser( argx, arg,c,
            "{help h||}"
    parser.about("\n this program demonstrates using face and ete detection in a video stream\n");


And add these lines in their place:

face_cascade_name = "haarcascade_frontalface_default.xml";
eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";

You now will no longer need to use command line arguments. You can add the CommandLineParser and need for arguments back (if you want) once you have got your program working with the hard coded filenames.

Your code "old_main.cpp" had several errors. I fixed them, so please carefully look and see the changes. Below is code that should work, provided you download the files and add them to the directory which contains the cpp file.

(P.S. If you are curious how the haar cascades work, you can read the seminal Viola-Jones paper here or a wiki page summarizing/explaining it here. If you are curious about the meaning of (and how to set) the detectMultiScale parameters, there is a nice explanation here).

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

using namespace cv;
using namespace std;

void detectAndDisplay(Mat frame);
String face_cascade_name, eyes_cascade_name;
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

int main(int argc, const char** argv)
    face_cascade_name = "haarcascade_frontalface_default.xml";
    eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";

    VideoCapture stream1;
    Mat frame;

    //Step 1) Load the cascades
    if (!face_cascade.load(face_cascade_name))
        cout << "error loading face cascade" << endl;
        return -1;

    if (!eyes_cascade.load(eyes_cascade_name))
        cout << "error loading eyes cascade" << endl;
        return -1;

    //Step 2) read in video stream;
    if (!stream1.isOpened())
        cout << "can not open camera" << endl;
        return -1;
    while (
        if (frame.empty())
            cout << "frame is empty" << endl;
        //Step 3) Apply the classifier to the frame
        if (waitKey(10) == 27)
    return 0;
void detectAndDisplay(Mat frame)
    std::vector<Rect> faces;
    Mat frame_gray;
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

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

    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, find eye
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30 ...
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-04-02 14:25:49 -0500

Seen: 1,161 times

Last updated: Apr 02 '18