im using this code to detect face features,it works properly but after sometime of its run,video frame pauses and stops responding.

im running it on microsoft visual cpp 2008

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <conio.h>

// A Simple Camera Capture Framework 
const char* cascade_name_f ="D:\\study\\xml\\haarcascade_frontalface_alt2.xml";
const char* cascade_name_e ="D:\\study\\xml\\parojos.xml";
const char* cascade_name_n ="D:\\study\\xml\\Nose.xml";
const char* cascade_name_m ="D:\\study\\xml\\haarcascade_mcs_mouth.xml";


void detect_and_draw( IplImage* img ); 


int main() {

    IplImage* grayImg; // gray image for the conversion of the original image
    int threshold = 120, maxValue = 255;
    int thresholdType = CV_THRESH_BINARY;

    CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
    if ( !capture ) 
    {
        fprintf( stderr, "ERROR: capture is NULL \n" );
        getchar();
        return -1;
    }


    while ( 1 ) 
    {
        // Get one frame
        IplImage* frame = cvQueryFrame( capture );
        if ( !frame ) 
        {
            fprintf( stderr, "ERROR: frame is null...\n" );
            getchar();
            break;
        }
        grayImg = cvCreateImage( cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1 );
        cvCvtColor( frame, grayImg, CV_BGR2GRAY );
        detect_and_draw(grayImg); 



        if ( (cvWaitKey(10) & 255) == 27 ) break;
    }
    // Release the capture device housekeeping
    cvReleaseCapture( &capture );

    return 0;
}

void detect_and_draw( IplImage* img )
{

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

    // Create a new Haar classifier
    static CvHaarClassifierCascade* cascade_f = 0;
    static CvHaarClassifierCascade* cascade_e = 0;
    static CvHaarClassifierCascade* cascade_n = 0;
    static CvHaarClassifierCascade* cascade_m = 0;
    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;

    // Load the HaarClassifierCascade
    cascade_f = (CvHaarClassifierCascade*)cvLoad( cascade_name_f, 0, 0, 0 );
    cascade_e = (CvHaarClassifierCascade*)cvLoad( cascade_name_e, 0, 0, 0 );
    cascade_n = (CvHaarClassifierCascade*)cvLoad( cascade_name_n, 0, 0, 0 );
    cascade_m = (CvHaarClassifierCascade*)cvLoad( cascade_name_m, 0, 0, 0 );
    // Check whether the cascade has loaded successfully. Else report and error and quit
    if( !cascade_f )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return;
    }

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

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

    // 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_f )
    {

        // 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_f, storage,
            1.1, 3, CV_HAAR_DO_CANNY_PRUNING,
            cvSize(40, 40) );

        // 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 );

            if(cascade_e)
            {    
                /* Set the Region of Interest: estimate the eyes' position */
                cvSetImageROI(img, cvRect(r->x, r->y + (r->height/5.5), r->width, r->height/3.0));

                CvSeq* eyes = cvHaarDetectObjects( img, cascade_e, storage,
                    1.15, 3, 0,
                    cvSize(25, 15) );

                for( i = 0; i < (eyes ? eyes->total : 0); i++ ) 
                {
                    CvRect* e = (CvRect*)cvGetSeqElem( eyes, i );
                    cvRectangle(img, 
                        cvPoint(e->x, e->y), 
                        cvPoint(e->x + e->width, e->y + e->height),
                        CV_RGB(255, 0, 0), 1, 8, 0);
                }
                cvResetImageROI(img);
            }
            if(cascade_n)
            {
                cvSetImageROI(img, cvRect(r->x, r->y , r->width, r->height));

                CvSeq* nose = cvHaarDetectObjects( img, cascade_n, storage,
                    1.15, 3, 0,
                    cvSize(25, 15) );

                for( i = 0; i < (nose ? nose->total : 0); i++ ) 
                {
                    CvRect* n = (CvRect*)cvGetSeqElem( nose, i );
                    cvRectangle(img, 
                        cvPoint(n->x, n->y), 
                        cvPoint(n->x + n->width, n->y + n->height),
                        CV_RGB(255, 0, 0), 1, 8, 0);

                }
                cvResetImageROI(img);
            }
            if(cascade_m)
            {
                cvSetImageROI(img, cvRect(r->x, r->y + (r->height*2/3), r->width, r->height/3.0));

                CvSeq* mouth = cvHaarDetectObjects( img, cascade_m, storage,
                    1.15, 3, 0,
                    cvSize(25, 15) );

                for( i = 0; i < (mouth ? mouth->total : 0); i++ ) 
                {
                    CvRect* m = (CvRect*)cvGetSeqElem( mouth, i );
                    cvRectangle(img, 
                        cvPoint(m->x, m->y), 
                        cvPoint(m->x + m->width, m->y + m->height),
                        CV_RGB(255, 0, 0), 1, 8, 0);
                }
                cvResetImageROI(img);
            }

        }
    cvReleaseHaarClassifierCascade( &cascade_e );
    cvReleaseHaarClassifierCascade( &cascade_n );
    cvReleaseHaarClassifierCascade( &cascade_m );
    }

    // Show the image in the window named "result"
    cvShowImage( "result", img );
    cvReleaseHaarClassifierCascade( &cascade_f );
    cvReleaseMemStorage( &storage );

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

Edited 4 Years Ago by anuran

It's not at all clear what the issue might be, you are clearly using a very specialised library. I would suggest some techniques used in a general approcah to any problem:

  1. Try running the software in a symbolic debugger, when it gets to the point of not responding (assuming it doesn't crash and halt) pause the program and see where it is stuck.

  2. Use the process explorer (procexp.exe) to monitor how much memory the application is actually using and if it is leaking memory (indicated by every increasing memory usage).

This article has been dead for over six months. Start a new discussion instead.