Thanks for more than two lakh views. All about openCV, Image Processing converging towards Biometric face recognition. Use the Easy Navigation button on the top bar to view all the posts at a glance related to openCV. I kept this blog small so that anyone can complete going through all posts and acquaint himself with openCV.

Wednesday, June 15, 2011

Face detector

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.

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