Ask Your Question
0

Trained Haar classifier is not working

asked 2015-04-13 09:18:15 -0500

pulp_fiction gravatar image

updated 2015-04-13 09:45:04 -0500

I am exhausted, am about to give up! I have been trying all day for past 3 days now but no success.

I set up OpenCV, ran the test example and asked many questions here and StackOverflow and currently I am able to test the frontal_face_default.xml and others xmls provided with OpenCV successfully except one classifier. I found it here:
http://coding-robin.de/2013/07/22/tra...

The name is banana_classifier.xml(Download link: https://github.com/mrnugget/opencv-ha...). The guy there says it worked for him. It is not working for me. I tried it with both newer train_cascade(C++ API) and older haar_training_cascade(C API). It crashes in both the APIs. Here is the source code file for your reference. Though I debugged and am sure that, it is something with the classifier file and not the source code.(I am using MS VC 2013 C/C++ compilers and libraries). Everything has been configured properly in IDE. Believe me, I have had my share of headache setting up everything and failing.

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

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay(Mat frame);

/** Global variables */
String banana_cascade_name = "banana_classifier.xml";
CascadeClassifier banana_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main(void)
{
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascades
    if (!banana_cascade.load(banana_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };

    //-- 2. Read the video stream
    capture.open(0);
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }

    while (capture.read(frame))
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);

        int c = waitKey(10);
        if ((char)c == 27) { break; } // escape
    }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> bananas;
    Mat frame_gray;

    //Conversion of frame to grayscale
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    //Contrast enhance(Spread out intensity distribution)
    equalizeHist(frame_gray, frame_gray);
    //-- Detect Banana
    banana_cascade.detectMultiScale(frame_gray, bananas, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
    cout << bananas.size();

    for (size_t i = 0; i < bananas.size(); i++)
    {
        Point center(bananas[i].x + bananas[i].width / 2, bananas[i].y + bananas[i].height / 2);
        ellipse(frame, center, Size(bananas[i].width / 2, bananas[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

        //Mat faceROI = frame_gray(bananas[i]);
        //std::vector<Rect> eyes;
        //-- In each face, detect eyes
        //eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        /*
        for (size_t j = 0; j < eyes.size(); j++)
        {
        Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
        int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
        circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }*/
    }
    //-- Show what you got
    imshow(window_name, frame);
}

And here is the C source code(older one):

// OpenCV Sample Application: facedetect.c

// Include header files
#include "cv.h"
#include "highgui.h"

#include ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-07-04 20:59:28 -0500

updated 2015-07-04 21:06:43 -0500

There is no problem with both your code and the classifier. I compiled the cpp source code smoothly. Here are screenshots of the result:

C:\fakepath\BananaDetect.png

image description

Surely, there are false detection:

image description

image description

I tuned detectMultiScale() slightly:

    //banana_cascade.detectMultiScale(frame_gray, bananas, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
banana_cascade.detectMultiScale(frame_gray, bananas, 1.05, 12, 0 |CASCADE_FIND_BIGGEST_OBJECT , Size(100, 100));
cout << "bananas.size() = "<< bananas.size() << endl;

Full source code here:

BananaDetect.cpp

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-04-13 09:18:15 -0500

Seen: 649 times

Last updated: Jul 04 '15