blog/posts/2013-10-03-face-detection-with-opencv.markdown
Sanchayan Maity 01d0d50249 Initial commit for blog migration to Hakyll
Never gave a damn about jekyll/ruby except to use it for github pages.
Now use Hakyll/Haskell and migrate from jekyll/ruby.
2017-02-14 02:00:46 +05:30

3.1 KiB

author title tags
Sanchayan Maity Face detection with OpenCV face detection, opencv

Below is the code for face detection in OpenCV.


#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"
#include "stdio.h"

int main()
{
IplImage* frame = NULL;
const char* cascade_path = "C:\\OpenCV\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
CvHaarClassifierCascade* hc = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSize minSize = cvSize(100, 100);
CvSize maxSize = cvSize(640, 480);
CvSeq* faces = NULL;
CvCapture* input_camera = NULL;

int key = 0;
int loopCounter = 0;

hc = (CvHaarClassifierCascade*)cvLoad(cascade_path, NULL, NULL, NULL);
if (hc == NULL)
{
printf("\nLoading of classifier failed\n");
return -1;
}

input_camera = cvCaptureFromCAM(-1);
if (input_camera == NULL)
{
printf("\nCould not open camera\n");
return -1;
}

//Grabs and returns a frame from camera
frame = cvQueryFrame(input_camera);
if (frame == NULL)
{
printf("\nCould not capture frame\n");
return -1;
}

cvNamedWindow("Capturing Image ...", 0);

cvResizeWindow("Capturing Image ...",
(int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_HEIGHT),
(int) cvGetCaptureProperty(input_camera, CV_CAP_PROP_FRAME_WIDTH));

while(frame != NULL)
{
faces = cvHaarDetectObjects(frame, hc, storage, 1.2, 3, CV_HAAR_DO_CANNY_PRUNING, minSize, maxSize);

for (loopCounter = 0; loopCounter < (faces ? faces->total : 0); loopCounter++)
{
CvRect *r = (CvRect*)cvGetSeqElem(faces, loopCounter);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
cvRectangle(frame, pt1, pt2, CV_RGB(0, 255, 0), 3, 4, 0);
}

//Shows a frame
cvShowImage("Capturing Image ...", frame);

// Checks if ESC is pressed and gives a delay
// so that the frame can be displayed properly
key = cvWaitKey(1);
if (key == 27)        // ESC key
{
break;
}
//Grabs and returns the next frame
frame = cvQueryFrame(input_camera);
}

//cvReleaseImage( &frame );
cvReleaseCapture(&input_camera);
cvReleaseMemStorage( &storage );
cvReleaseHaarClassifierCascade( &hc );

//Destroy the window
cvDestroyWindow("Capturing Image ...");

return 0;
}

I have tested the above code on a NVidia Tegra2 with Toradex Colibri T20 module and also on my laptop. Works, but there is a bug it seems. The program crashes if I enable the cvReleaseImage() line. Not an OpenCV expert, will have to really study the functions properly before playing around I guess. Will update this as soon as i found out.

I have not explained the OpenCV installation procedure for windows as it is already documented on the OpenCV website.

For using with Beagleboard you can refer to a previous article of mine

http://coherentmusings.wordpress.com/2012/06/24/getting-started-with-opencv-on-beagleboard-xm/.

You can also use OpenCV with embedded Linux if you use Buildroot or OpenEmbedded for building your image.