Created
December 7, 2011 06:37
-
-
Save codebrainz/1441735 to your computer and use it in GitHub Desktop.
Detect faces using OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Matthew Brush <mbrush(at)codebrainz(dot)ca> | |
* Feel free to use this code however you want to. | |
* | |
* This is a VERY minimal example of detecting faces using OpenCV. It | |
* doesn't do any error handling or anything in an attempt to keep the | |
* topic code more readable. | |
* | |
* To compile (at least on Linux): | |
* gcc -o facedetect facedetect.c `pkg-config --cflags --libs opencv` | |
* | |
* To run: | |
* ./facedetect CASCADE_FILE [CAMERA_INDEX] | |
* | |
* The CASECADE_FILEs come from the OpenCV source distribution but you | |
* could also download them from the trunk's "data/haarcascades" | |
* directory in the source repository: | |
* https://code.ros.org/gf/project/opencv/scmsvn | |
* | |
* The CAMERA_INDEX is the index of the actual capture device, | |
* according to the platform. Use -1 or leave blank for the "default" | |
* device. | |
*/ | |
#include <cv.h> | |
#include <highgui.h> | |
#define WIN_NAME "Face Detector" | |
int main (int argc, char *argv[]) | |
{ | |
int i; | |
int device = -1; | |
const char *name; | |
void *handle; | |
CvCapture *capture; | |
IplImage *frame; | |
IplImage *frame_out; | |
CvMemStorage *storage; | |
CvSeq *faces; | |
CvRect *rect; | |
CvHaarClassifierCascade *cascade; | |
if (argc > 2) | |
device = atoi (argv[2]); | |
storage = cvCreateMemStorage (0); | |
cascade = (CvHaarClassifierCascade *) cvLoad (argv[1], 0, 0, 0); | |
capture = cvCaptureFromCAM (device); | |
cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_WIDTH, 320.0); | |
cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_HEIGHT, 240.0); | |
cvNamedWindow (WIN_NAME, 1); | |
handle = cvGetWindowHandle (WIN_NAME); | |
while (1) | |
{ | |
frame = cvQueryFrame (capture); | |
if (!frame) | |
break; | |
cvClearMemStorage (storage); | |
faces = cvHaarDetectObjects (frame, | |
cascade, | |
storage, | |
1.1, | |
2, | |
CV_HAAR_DO_CANNY_PRUNING, | |
cvSize (40, 40), | |
cvSize (40, 40)); | |
frame_out = cvCloneImage (frame); | |
for (i = 0; i < (faces ? faces->total : 0); i++) | |
{ | |
rect = (CvRect*) cvGetSeqElem (faces, i); | |
cvRectangle (frame_out, | |
cvPoint (rect->x, rect->y), | |
cvPoint (rect->x + rect->width, | |
rect->y + rect->height), | |
CV_RGB (0xff,0x00,0x0), | |
1, CV_AA, 0); | |
} | |
cvShowImage (WIN_NAME, frame_out); | |
cvReleaseImage (&frame_out); | |
if (cvWaitKey (100) >= 0) | |
break; | |
/* Hack (at least for the GTK+ backend) to exit when the | |
* window manager's close button is clicked */ | |
name = cvGetWindowName (handle); | |
if (!name || !name[0]) | |
break; | |
} | |
cvReleaseCapture (&capture); | |
cvDestroyWindow (WIN_NAME); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment