Wednesday, June 15, 2011

Face detector

Today, let's take an important step towards face recognition - face detection

First, let me tell you the difference between both.Face detection is just telling whether there is a face or not in the given image, on the other hand, face recognition is telling to which person that face belongs.

This is the code for face detection using your webcam.Actually, this is not exclusively my code, I hacked it from the code given by Nashruddin which inturn was hacked from the openCV samples.Any way, that's what makes the open source software better and better forever :)

Here's the code snippet
//Programme to detect faces through the webcam using the haar cascade
//Links Used:http://nashruddin.com/OpenCV_Face_Detection

#include<stdio.h>
#include<cv.h>
#include<highgui.h>

CvHaarClassifierCascade *cascade;
CvMemStorage *Membuffer;

void detectfaces(IplImage *frame)
{
int i;
CvSeq *faces=cvHaarDetectObjects(frame,cascade,Membuffer,1.1,3,0,cvSize(30,30));//Maximum 3 neighbourhood images and the last cvsize gives minimum size of face
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);

//Draws a rectangle with two points given with color of red and thickness as 8 and line type - 1
}

cvShowImage("Video",frame);


}


int main(int argc,char *argv[])

{IplImage *frame;
CvCapture *capture;
int key;
char filename[]="haarcascade_frontalface_alt.xml";//This is the cascading file that is needed for face detector it is generally present in if openCV is your installation folder, it is present in openCV/data/haarcascades folder, don't forget to paste the haarcascade file in the same folder as your program before compiling



cascade=(CvHaarClassifierCascade *)cvLoad(filename,0,0,0);

//Loading the haar... file

Membuffer=cvCreateMemStorage(0);//Allocating the default memory for image (64K at Present)

capture=cvCaptureFromCAM(0);//Capture the image from cam with index 0
//Note that if you are using a Laptop with an external webcam it has an index 1 since the Lappie cam's index is 0

assert(cascade && Membuffer && capture);//To make sure that everything needed is always available
cvNamedWindow("Video",1);

while(key!='q')

{frame=cvQueryFrame(capture);
if(!frame)
break;

//cvFlip(frame,frame,-1);//If you are using openCV on windows, uncomment this line
frame->origin=0;



detectfaces(frame);
key=cvWaitKey(10);//Wait for 10 milli seconds, the user may press 'q'
}
cvReleaseCapture(&capture);
cvDestroyWindow("Video");
cvReleaseHaarClassifierCascade(&cascade);
cvReleaseMemStorage(&Membuffer);
}


For your convenience, I have added the comments in the code above.Don't bother if you feel lazy to copy the content.I kept the above program and the haar cascade xml file that you need to use in the link below.


http://www.free-car-magazines.com/images/download-button-animated.gif

I know, some of you are even lazy to, compile the code, so I have included a Linux executable in the above download link.

And this is a bonus, a youtube video of me with my face detector.I shot the video with poor lighting conditions and a VGA cam, that's why not a good one (I look 10 times more handsome), but serves the purpose



4 comments:

Dilip said...

tel me how this file works... what exactly wil it do and how... "haarcascade_frontalface_alt.xml"

Dileep said...

This is an xml file, that contains the structure or kind of blue print of how to recognize a face

Dilip said...

dude mail me back, dilip_489@yahoo.co.in, have to talk to u... its about my final year project. may be u can help.

Unknown said...

I'm getting this error:

"facedetect.c: In function ‘detectfaces’:
facedetect.c:14:1: error: too few arguments to function ‘cvHaarDetectObjects’
CvSeq *faces=cvHaarDetectObjects(frame,cascade,Membuffer,1.1,3,0,cvSize(30,30));//Maximum 3 neighbourhood images and the last cvsize gives minimum size of face
^
In file included from /usr/include/opencv/cv.h:71:0,
from facedetect.c:5:
/usr/include/opencv2/objdetect/objdetect.hpp:139:15: note: declared here
CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,"

Any idea what could be the problem ?

Post a Comment