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.

3 comments:

Unknown said...

great work..can i use this code in C language and then connect it to Arduino pic

Dileep said...

oh yes, you can

Pritesh said...

I want to implement this codes on dsPIC or any other PIC how can it be done?

Post a Comment