Simple Face Detection Player

Here’s a simple video player that also performs facial detection thought the Open Computer Vision Library.

Here’s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here.

The code:

#include <highgui.h>
#include <stdio.h>
#include <cv.h>
 
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
 
int main(int argc, char *argv[]) {
    CvCapture *video = NULL;
    IplImage *frame = NULL;
    int delay = 0, key, i=0;
    char *window_name = "Video";
    char *cascadefile = "haarcascade_frontalface_alt.xml";
 
    /* check for video file passed by command line */
    if (argc>1) {
        video = cvCaptureFromFile(argv[1]);
    }
    else {
        printf("Usage: %s VIDEO_FILE\n", argv[0]);
        return 1;
    }
 
    /* check file was correctly opened */
    if (!video) {
        printf("Unable to open \"%s\"\n", argv[1]);
        return 1;
    }
 
    /* load the classifier */
    cascade = ( CvHaarClassifierCascade* )cvLoad( cascadefile, 0, 0, 0 );
    if(!cascade){
        printf("Error loading the classifier.");
	return 1;
    }
 
    /* setup the memory buffer for the face detector */
    storage = cvCreateMemStorage( 0 );
    if(!storage){
        printf("Error creating the memory storage.");
	return 1;
    }
 
    /* create a video window, auto size */
    cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);
 
    /* get a frame. Necessary for use the cvGetCaptureProperty */
    frame = cvQueryFrame(video);
 
    /* calculate the delay between each frame and display video's FPS */
    printf("%2.2f FPS\n", cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
    delay = (int) (1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
 
    while (frame) {
	/* show loaded frame */
        cvShowImage(window_name, frame);
 
	/* wait delay and check for the quit key */
        key = cvWaitKey(delay);
        if(key=='q') break;
	/* load and check next frame*/
        frame = cvQueryFrame(video);
	if(!frame) {
		printf("error loading frame.\n");
		return 1;
	}
 
        /* detect faces */
        CvSeq *faces = cvHaarDetectObjects(
            frame, /* image to detect objects in */
            cascade, /* haar classifier cascade */
            storage, /* resultant sequence of the object candidate rectangles */
            1.1, /* increse window by 10% between the subsequent scans*/
            3, /* 3 neighbors makes up an object */
            0 /* flags CV_HAAR_DO_CANNY_PRUNNING */,
            cvSize( 40, 40 ) 
        );
 
        /* for each face found, draw a red box */
        for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
             CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
             cvRectangle( frame,
                  cvPoint( r->x, r->y ),
                  cvPoint( r->x + r->width, r->y + r->height ),
                  CV_RGB( 255, 0, 0 ), 1, 8, 0 );
        }
    }
}

Yeah, I know the code needs a few adjustments. ¬¬

To compile it in a well configured OpenCV development environment:

gcc faceplayer.c -o faceplayer `pkg-config opencv ‑‑libs ‑‑cflags`

To run it you have to put in the same directory of the binary the XML classifier (haarcascade_frontalface_alt.xml) that comes with OpenCV sources at OpenCV-2.0.0/data/haarcascades/. And so:

./faceplayer video.avi

The results I got so far is that it works well for faces but sometimes its also detects more than faces. And here a video of it working live.

A example of good result:

rick roll face detection

A example of bad result:

rick roll face detection bad result

Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.

5 thoughts on “Simple Face Detection Player

  1. Silveira: don’t forget that pkg-config requires parameters with two dashes (–), not one (-). It seems your CMS shortens then to a graphic “superdash” symbol (–), so a newbie that cuts and pastes the code won’t get the program working. This is not the first time I noticed that on your blog… Please try to fix this, for me this isn’t an issue but it will simply be a showstopper for novice programmers.

    Alessandro: If you decrease the false detections, you might also decrease the current ones, isn’t it?

    I tried the program. It worked. However it uses lots and lots of processing. A simple 624×464 AVI video with 23.98 fps (an episode of X-Files with lots of faces) would take forever to play, it was really sluggish on my laptop, an Intel Core2 Duo Centrino with 2.40 GHz, using almost 100% in both processors.

    The results were not very impressive. The “E” in X-Files would show as a face, sometimes Mulder’s ears would get their red square also. And for the introduction scene where the face of Mulder gets closer and closer to the camera, it wasn’t highlighted at any time. However, after the introductory scenes, it behaved much better.

  2. Oh, and by the way, Silveira… How did you record the videos with the squares? Did you modify your program to have it save the video or did you capture from your screen?


  3. Patola:

    Silveira: don’t forget that pkg-config requires parameters with two dashes (–), not one (-). It seems your CMS shortens then to a graphic “superdash” symbol (–), so a newbie that cuts and pastes the code won’t get the program working. This is not the first time I noticed that on your blog… Please try to fix this, for me this isn’t an issue but it will simply be a showstopper for novice programmers.

    Patola, I wonder if is a problem of WordPress of from another plugin. Really annoying problem when somebody tries to use it. I corrected it here putting two times the html code for it.

    The results were not very impressive. The “E” in X-Files would show as a face, sometimes Mulder’s ears would get their red square also. And for the introduction scene where the face of Mulder gets closer and closer to the camera, it wasn’t highlighted at any time. However, after the introductory scenes, it behaved much better.

    Yeah, me too. A lot of false detection. But I guess using this can be improved somehow because for example on digital cameras face detection work pretty well. Let’s keep looking for it.

  4. hello,
    i m still able to detect the faces from live videousing cv capture and haar classifiers my code dont at all work and show a break ststement
    please reply me soon

Leave a Reply

Your email address will not be published. Required fields are marked *