Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Trained Haar classifier is not working

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/train-your-own-opencv-haar-classifier.html

The name is banana_classifier.xml. 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>

// Create memory for calculations
static CvMemStorage* storage = 0;

// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;

// Function prototype for detecting and drawing an object from an image
void detect_and_draw(IplImage* image);

// Create a string that contains the cascade name
const char* cascade_name = "banana_classifier.xml";
/*    "haarcascade_profileface.xml";*/

// Main function, defines the entry point for the program.
int main(int argc, char** argv)
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Used for calculations
    int optlen = strlen("--cascade=");

    // Input file name for avi or image file.
    //const char* input_name="banana.jpg";
    const char* input_name = "0";

    // Check for the correct usage of the command line
    /*
    if (argc > 1 && strncmp(argv[1], "--cascade=", optlen) == 0)
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        fprintf(stderr,
            "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n");
        return -1;
    }
    */
    //Chek if file actually exists
    struct stat buf;
    int statResult = stat(cascade_name, &buf);
    if (statResult || buf.st_ino < 0) {
        printf("File not found\n");
        exit(-2);
    }

    // Load the HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad(cascade_name, 0, 0, 0);

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if (!cascade)
    {
        fprintf(stderr, "ERROR: Could not load classifier cascade\n");
        return -1;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Find whether to detect the object from file or from camera.
    if (!input_name || (isdigit(input_name[0]) && input_name[1] == '\0'))
        capture = cvCaptureFromCAM(!input_name ? 0 : input_name[0] - '0');
    else
        capture = cvCaptureFromAVI(input_name);

    // Create a new named window with title: result
    cvNamedWindow("result", 1);

    // Find if the capture is loaded successfully or not.

    // If loaded succesfully, then:
    if (capture)
    {
        // Capture from the camera.
        for (;;)
        {
            // Capture the frame and load it in IplImage
            if (!cvGrabFrame(capture))
                break;
            frame = cvRetrieveFrame(capture, 0);

            // If the frame does not exist, quit the loop
            if (!frame)
                break;

            // Allocate framecopy as the same size of the frame
            if (!frame_copy)
                frame_copy = cvCreateImage(cvSize(frame->width, frame->height),
                IPL_DEPTH_8U, frame->nChannels);

            // Check the origin of image. If top left, copy the image frame to frame_copy. 
            if (frame->origin == IPL_ORIGIN_TL)
                cvCopy(frame, frame_copy, 0);
            // Else flip and copy the image
            else
                cvFlip(frame, frame_copy, 0);

            // Call the function to detect and draw the face
            detect_and_draw(frame_copy);

            // Wait for a while before proceeding to the next frame
            if (cvWaitKey(10) >= 0)
                break;
        }

        // Release the images, and capture memory
        cvReleaseImage(&frame_copy);
        cvReleaseCapture(&capture);
    }

    // If the capture is not loaded succesfully, then:
    else
    {
        // Assume the image to be lena.jpg, or the input_name specified
        const char* filename = input_name ? input_name : (char*)"lena.jpg";

        // Load the image from that filename
        IplImage* image = cvLoadImage(filename, 1);

        // If Image is loaded succesfully, then:
        if (image)
        {
            // Detect and draw the face
            detect_and_draw(image);

            // Wait for user input
            cvWaitKey(0);

            // Release the image memory
            cvReleaseImage(&image);
        }
        else
        {
            /* assume it is a text file containing the
            list of the image filenames to be processed - one per line */
            FILE* f = fopen(filename, "rt");
            if (f)
            {
                char buf[1000 + 1];

                // Get the line from the file
                while (fgets(buf, 1000, f))
                {

                    // Remove the spaces if any, and clean up the name
                    int len = (int)strlen(buf);
                    while (len > 0 && isspace(buf[len - 1]))
                        len--;
                    buf[len] = '\0';

                    // Load the image from the filename present in the buffer
                    image = cvLoadImage(buf, 1);

                    // If the image was loaded succesfully, then:
                    if (image)
                    {
                        // Detect and draw the face from the image
                        detect_and_draw(image);

                        // Wait for the user input, and release the memory
                        cvWaitKey(0);
                        cvReleaseImage(&image);
                    }
                }
                // Close the file
                fclose(f);
            }
        }

    }

    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}

// Function to detect and draw any faces that is present in an image
void detect_and_draw(IplImage* img)
{
    int scale = 1;

    // Create a new image based on the input image
    IplImage* temp = cvCreateImage(cvSize(img->width / scale, img->height / scale), 8, 3);

    // Create two points to represent the face locations
    CvPoint pt1, pt2;
    int i;

    // Clear the memory storage which was used before
    cvClearMemStorage(storage);

    // Find whether the cascade is loaded, to find the faces. If yes, then:
    if (cascade)
    {

        // There can be more than one face in an image. So create a growable sequence of faces.
        // Detect the objects and store them in the sequence
        CvSeq* faces = cvHaarDetectObjects(img, cascade, storage,
            1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
            cvSize(40, 40), cvSize(0, 0));

        // Loop the number of faces found.
        for (i = 0; i < (faces ? faces->total : 0); i++)
        {
            // Create a new rectangle for drawing the face
            CvRect* r = (CvRect*)cvGetSeqElem(faces, i);

            // Find the dimensions of the face,and scale it if necessary
            pt1.x = r->x*scale;
            pt2.x = (r->x + r->width)*scale;
            pt1.y = r->y*scale;
            pt2.y = (r->y + r->height)*scale;

            // Draw the rectangle in the input image
            cvRectangle(img, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
        }
    }

    // Show the image in the window named "result"
    cvShowImage("result", img);

    // Release the temp image created.
    cvReleaseImage(&temp);
}

Can anyone test it (if you can) and tell me what is wrong with this classifier file.

Trained Haar classifier is not working

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/train-your-own-opencv-haar-classifier.html

The name is banana_classifier.xml. (Download link: https://github.com/mrnugget/opencv-haar-classifier-training/blob/master/trained_classifiers/banana_classifier.xml). 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>

// Create memory for calculations
static CvMemStorage* storage = 0;

// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;

// Function prototype for detecting and drawing an object from an image
void detect_and_draw(IplImage* image);

// Create a string that contains the cascade name
const char* cascade_name = "banana_classifier.xml";
/*    "haarcascade_profileface.xml";*/

// Main function, defines the entry point for the program.
int main(int argc, char** argv)
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Used for calculations
    int optlen = strlen("--cascade=");

    // Input file name for avi or image file.
    //const char* input_name="banana.jpg";
    const char* input_name = "0";

    // Check for the correct usage of the command line
    /*
    if (argc > 1 && strncmp(argv[1], "--cascade=", optlen) == 0)
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        fprintf(stderr,
            "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n");
        return -1;
    }
    */
    //Chek if file actually exists
    struct stat buf;
    int statResult = stat(cascade_name, &buf);
    if (statResult || buf.st_ino < 0) {
        printf("File not found\n");
        exit(-2);
    }

    // Load the HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad(cascade_name, 0, 0, 0);

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if (!cascade)
    {
        fprintf(stderr, "ERROR: Could not load classifier cascade\n");
        return -1;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Find whether to detect the object from file or from camera.
    if (!input_name || (isdigit(input_name[0]) && input_name[1] == '\0'))
        capture = cvCaptureFromCAM(!input_name ? 0 : input_name[0] - '0');
    else
        capture = cvCaptureFromAVI(input_name);

    // Create a new named window with title: result
    cvNamedWindow("result", 1);

    // Find if the capture is loaded successfully or not.

    // If loaded succesfully, then:
    if (capture)
    {
        // Capture from the camera.
        for (;;)
        {
            // Capture the frame and load it in IplImage
            if (!cvGrabFrame(capture))
                break;
            frame = cvRetrieveFrame(capture, 0);

            // If the frame does not exist, quit the loop
            if (!frame)
                break;

            // Allocate framecopy as the same size of the frame
            if (!frame_copy)
                frame_copy = cvCreateImage(cvSize(frame->width, frame->height),
                IPL_DEPTH_8U, frame->nChannels);

            // Check the origin of image. If top left, copy the image frame to frame_copy. 
            if (frame->origin == IPL_ORIGIN_TL)
                cvCopy(frame, frame_copy, 0);
            // Else flip and copy the image
            else
                cvFlip(frame, frame_copy, 0);

            // Call the function to detect and draw the face
            detect_and_draw(frame_copy);

            // Wait for a while before proceeding to the next frame
            if (cvWaitKey(10) >= 0)
                break;
        }

        // Release the images, and capture memory
        cvReleaseImage(&frame_copy);
        cvReleaseCapture(&capture);
    }

    // If the capture is not loaded succesfully, then:
    else
    {
        // Assume the image to be lena.jpg, or the input_name specified
        const char* filename = input_name ? input_name : (char*)"lena.jpg";

        // Load the image from that filename
        IplImage* image = cvLoadImage(filename, 1);

        // If Image is loaded succesfully, then:
        if (image)
        {
            // Detect and draw the face
            detect_and_draw(image);

            // Wait for user input
            cvWaitKey(0);

            // Release the image memory
            cvReleaseImage(&image);
        }
        else
        {
            /* assume it is a text file containing the
            list of the image filenames to be processed - one per line */
            FILE* f = fopen(filename, "rt");
            if (f)
            {
                char buf[1000 + 1];

                // Get the line from the file
                while (fgets(buf, 1000, f))
                {

                    // Remove the spaces if any, and clean up the name
                    int len = (int)strlen(buf);
                    while (len > 0 && isspace(buf[len - 1]))
                        len--;
                    buf[len] = '\0';

                    // Load the image from the filename present in the buffer
                    image = cvLoadImage(buf, 1);

                    // If the image was loaded succesfully, then:
                    if (image)
                    {
                        // Detect and draw the face from the image
                        detect_and_draw(image);

                        // Wait for the user input, and release the memory
                        cvWaitKey(0);
                        cvReleaseImage(&image);
                    }
                }
                // Close the file
                fclose(f);
            }
        }

    }

    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}

// Function to detect and draw any faces that is present in an image
void detect_and_draw(IplImage* img)
{
    int scale = 1;

    // Create a new image based on the input image
    IplImage* temp = cvCreateImage(cvSize(img->width / scale, img->height / scale), 8, 3);

    // Create two points to represent the face locations
    CvPoint pt1, pt2;
    int i;

    // Clear the memory storage which was used before
    cvClearMemStorage(storage);

    // Find whether the cascade is loaded, to find the faces. If yes, then:
    if (cascade)
    {

        // There can be more than one face in an image. So create a growable sequence of faces.
        // Detect the objects and store them in the sequence
        CvSeq* faces = cvHaarDetectObjects(img, cascade, storage,
            1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
            cvSize(40, 40), cvSize(0, 0));

        // Loop the number of faces found.
        for (i = 0; i < (faces ? faces->total : 0); i++)
        {
            // Create a new rectangle for drawing the face
            CvRect* r = (CvRect*)cvGetSeqElem(faces, i);

            // Find the dimensions of the face,and scale it if necessary
            pt1.x = r->x*scale;
            pt2.x = (r->x + r->width)*scale;
            pt1.y = r->y*scale;
            pt2.y = (r->y + r->height)*scale;

            // Draw the rectangle in the input image
            cvRectangle(img, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
        }
    }

    // Show the image in the window named "result"
    cvShowImage("result", img);

    // Release the temp image created.
    cvReleaseImage(&temp);
}

Can anyone test it (if you can) and tell me what is wrong with this classifier file.