Ask Your Question
1

opencv object detection tutorial not working

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

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 -> https://docs.opencv.org/master/db/d28...

my "project" -> https://github.com/ellisa4/ASL-Interp...

my CMakeLists.txt -> https://raw.githubusercontent.com/ell...

./src/main/cpp/main.cpp -> https://raw.githubusercontent.com/ell...

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

returns

/home/master/Documents/opencv/data/haarcascades/haarcascade_frontalface_alt.xml
/home/master/Documents/opencv/data/haarcascades_cuda/haarcascade_frontalface_alt.xml
/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml

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
2

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

phillity gravatar image

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

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||}"
            "{face_cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}"
            "{eyes_cascade|../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}");
    parser.about("\n this program demonstrates using face and ete detection in a video stream\n");
    parser.printMessage();

face_cascade_name=parser.get<String>("face_cascade_name");
eyes_cascade_name=parser.get<String>("eyes_cascade");

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).

//old_main.cpp
//https://github.com/ellisa4/ASL-Interpreter/tree/master/src/main/cpp
#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

    stream1.open(0);
    if (!stream1.isOpened())
    {
        cout << "can not open camera" << endl;
        return -1;
    }
    while (stream1.read(frame))
    {
        if (frame.empty())
        {
            cout << "frame is empty" << endl;
            break;
        }
        //Step 3) Apply the classifier to the frame
        detectAndDisplay(frame);
        if (waitKey(10) == 27)
        {
            break;
        }
    }
    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 ...
(more)
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 1,774 times

Last updated: Apr 02 '18