Showing posts with label face detector. Show all posts
Showing posts with label face detector. Show all posts

Friday, June 17, 2011

Face + Eye detector

In my previous post, I showed you how to use your webcam to do face detection.In this post, I will explain you of how to use your webcam for eye and face detection.

Before we start, for those of you who couldn't use your webcam for face detection, I have ripped the code from previous post to make a code that takes an image as input and returns the same image with a rectangle drawn over the face as output.Here's the link for it

http://www.mediafire.com/?cs8yw8c4nfaif

Let's go straight into our eye +face detector.Before you go inside the code, first check the face detector , because most of the code here is hacked from it.Check it out here

http://www.opencvuser.co.cc/2011/06/face-detector.html

Here's the code for eye+face detector using your inbuilt webcam.Also note that, there is no robust eye detector related cascase file (XML file) till date.I have tested and included the most powerful one.But neverthless, if you have spectacles, remove them during testing.



//Programme to detect faces through the webcam using the haar cascade
//Links Used:http://www.opencvuser.co.cc/2011/06/face-detector.html

#include
#include
#include

CvHaarClassifierCascade *cascadeface,*cascadeeyes,*cascadenose,*cascademouth;
CvMemStorage *Membuffer;

void detectfaces(IplImage *frame)
{
int i;
CvSeq *faces=cvHaarDetectObjects(frame,cascadeface,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);
//cvSetImageROI(frame,*r);
//Draws a rectangle with two points given with color of red and thickness as 8 and line type - 1
}

}

void detecteyes(IplImage *frame)
{
int i;
CvSeq *eyes=cvHaarDetectObjects(frame,cascadeeyes,Membuffer,1.1,3,0,cvSize(10,10));//Maximum 3 neighbourhood images and the last cvsize gives minimum size of eyes
for(i=0;i<(eyes?eyes->total:0);i++)
{CvRect *r=(CvRect *)cvGetSeqElem(eyes,i);
cvRectangle(frame,cvPoint(r->x,r->y),cvPoint(r->x+r->width,r->y+r->height),CV_RGB(0,255,0),1,8,0);

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


}



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

{IplImage *frame;
CvCapture *capture;
int key,i;
cascadeface=(CvHaarClassifierCascade *)cvLoad("haarcascade_frontalface_alt.xml",0,0,0);
cascadeeyes=(CvHaarClassifierCascade *)cvLoad("haarcascade_eye_tree_eyeglasses.xml",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

assert(cascadeface && cascadeeyes && Membuffer && capture);
cvNamedWindow("Video",1);

while(key!='q')

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

//cvFlip(frame,frame,-1);//Uncomment this if you are getting inverted video
frame->origin=0;



detectfaces(frame);
detecteyes(frame);

cvShowImage("Video",frame);//For showing the video
key=cvWaitKey(10);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Video");
//cvReleaseHaarClassifierCascade(&cascade);
cvReleaseMemStorage(&Membuffer);
}



The downloadable Linux executable and the two XML files I  used, along with the original program are in the below link


http://www.mediafire.com/?u167ycmi1jz3w

For any doubts, use the comments section.

Don't have the time for making video, will upload soon.

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