0

I'm writing a software for eye detection in a webcam stream. I'm using OpenCV in Visual Studio but when I load the haarcascade file, I get an unhandled exception and in the output:

OpenCV error: NULL pointer <NULL or empty buffer> in unknow function,
file ....persistence.cpp

Here is the code:

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

using namespace std;
using namespace cv;

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

/** Global variables */
String face_cascade_name = "C:/opencv/data/haarcascades/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "C:/opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);

/** @function main */
int main( int argc, const char** argv )
{
    CvCapture* capture;
    Mat frame;
    int a;
    //-- 1. Load the cascades
    if (!face_cascade.load( face_cascade_name) ) {
        cout << "Couldn't load face_cascade" << endl;
        exit(-1);
    }
    if (!eyes_cascade.load( eyes_cascade_name) ) {
        cout << "Couldn't load face_cascade" << endl;
        exit(-1);
    }

    cout << "Loaded cascade" << endl;

    //-- 2. Read the video stream
    capture = cvCaptureFromCAM( -1 );
    if( capture )
    {
        while( true )
        {
            frame = cvQueryFrame( capture );

            //-- 3. Apply the classifier to the frame
            if( !frame.empty() )
            { detectAndDisplay( frame ); }
            else
            { printf(" --(!) No captured frame -- Break!"); break; }

            int c = waitKey(10);
            if( (char)c == 'c' ) { break; }
        }
    }
    return 0;
}

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

    cvtColor( frame, frame_gray, CV_BGR2GRAY );
    equalizeHist( frame_gray, frame_gray );

    //-- Detect faces
    face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

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

        Mat faceROI = frame_gray( faces[i] );
        std::vector<Rect> eyes;

        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30,30) );

        for( int j = 0; j < eyes.size(); j++ )
        {
            Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
            int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
            circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
        }
    }
    //-- Show what you got
    imshow( window_name, frame );
}

I've done all the inclusion to make OpenCV working with Visual Studio and the cascade classifier are in the correct path so I don't know why this code don't work.

Ah, I've exception too when I try the sample facerecognition.cpp. Any help is appreciated.


EDIT:

I've tried with the help of Barnabas but the exception is the same.

But maybe I found out something. If I delete the code

if (!eyes_cascade.load( eyes_cascade_name) ) { 
    cout << "Couldn't load face_cascade" << endl;
    exit(-1); 
}

The exception comes after the opening of webcam's software and if I continue the result is a frame (only one) where I can see my face recognized. So if I delete the control on the loading of the eye cascade classifier, the problem delay. New ideas?

Amro
  • 123,847
  • 25
  • 243
  • 454
user1872319
  • 11
  • 1
  • 4

2 Answers2

2

Basically, the problem is that you are mixing up the C++ and the C interface.

The camera capture is not CvCapture*. Here is how to capture from webcam (or from video-stream) correctly:

using namespace cv;
VideoCapture cap(0); // 0 for webcam input
if(cap.isOpened())   // use this instead of if( capture )

cap >> frame;        // instead of cvQueryFrame
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
0

This issue was driving me insane when I realised it was possibly something to do with the program using the wrong dlls for some reason; I haven't figured out why yet. My hunch was that my OPENCV_DIR enviroment variable was set to on older version of OpenCV but having checked I have set it to the newer version.

Any how, to get working code in VS 2012 (vc11) I did the following:

open the properties page and go to Configuration Properties > Debugging and add to the Environement variable "path=C:/OpenCV2_4_5/build/x86/vc11/bin;%PATH%" without the quotation marks. Obviously, your path to that bin folder may be different.

Hope this helps.