Showing posts with label code snippets. Show all posts
Showing posts with label code snippets. Show all posts

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



Tuesday, June 14, 2011

Code snippets

Before you use the snippets, I hope you installed openCV, compiled and understood the helloworld program given in the following website.

http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html

If not go back to my previous posts

All the code in the snippets will be similar to that of hello world program as shown below

//Program to create the negative of a given image

//Links used:http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/



#include<stdio.h>
#include<cv.h>
#include<highgui.h>
int main(int argc,char *argv[])
{int i,j,k;
IplImage *img=0;
int height,width,step,components;
uchar *data;
if(argc==0)
{printf("Could not load image");
return 0;
}
//Loading image
img=cvLoadImage(argv[1]);
if(img==0)
{printf("Image loading error");
return 0;
}
//Assigning data
height=img->height;
width=img->width;
step=img->widthStep;
components=img->nChannels;
data=(uchar *)(img->imageData);
printf("The picture's size is %d X %dnThe image has %d componentsnEach step length is %dn",height,width,components,step);
//To get the negative of image
for(i=0;i<height;i++)
for(j=0;j<width;j++)
for(k=0;k<components;k++)
data[i*step+j*components+k]=255-data[i*step+j*components+k];
cvNamedWindow("Lena",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Lena",50,50);
//Showing the image
cvShowImage("Lena",img);
cvSaveImage("Lenanegative.jpg",img);
//Waiting for a key press
cvWaitKey(0);
//Releasing the image
cvReleaseImage(&img);
return 0;
}

The code snippets contain three additional problems and their folder names are as follows :


  1. MnMdVr - This folder contains the finding of mean, median and standard deviation of image, using openCV inbuilt functions and some hacking of the helloworld.cpp code, which you wrote previously
  2. Block-Processing - This is  same as above, except, the finding of mean, median and variance are found block wise, like for every 20x20 pixels or so
  3. Sobel - This one contains the famous sobel edge detection technique, very simple one


This is the link for code snippets

Code Snippets

If the link goes down, tell me in the comments section, I will correct it.Also if you have any doubts understanding the code snippets, do comment them.

So keep trying to understand the code.If you already did the home work yourself, then very good.

If you have come this far, it is just a few steps away.On thursday, we will discuss about face detection and then saturday and sunday, we will drill down the most important topic -- "Haar Training"

And one more thing, from next post on, I will be personally posting and analyze the code snippets for you, step by step.