Skip to content

Instantly share code, notes, and snippets.

@cfr
Created October 18, 2011 20:16
Show Gist options
  • Save cfr/1296580 to your computer and use it in GitHub Desktop.
Save cfr/1296580 to your computer and use it in GitHub Desktop.
#import <stdio.h>
#import <highgui.h>
#import <cv.h>
IplImage *renderFaces(IplImage *, CvSeq *, double);
int main(int argc, char** argv)
{
double scale = 2; // primary speed factor
char *haarcascade = "eye/haarcascade_mcs_lefteye.xml";
CvHaarClassifierCascade* cascade =
(CvHaarClassifierCascade*) cvLoad( haarcascade
, 0, 0, 0);
CvMemStorage* storage = cvCreateMemStorage(0);
CvCapture *capture = cvCreateCameraCapture(0);
IplImage *frame = cvQueryFrame(capture);
IplImage *frameCopy = cvCreateImage( cvGetSize(frame)
, IPL_DEPTH_8U
, frame->nChannels);
IplImage *gray = cvCreateImage(cvGetSize(frameCopy), 8, 1);
IplImage *smallImg = cvCreateImage(cvSize( cvRound(frameCopy->width/scale)
, cvRound(frameCopy->height/scale)
), 8, 1);
cvNamedWindow("result", 1);
while(1) {
frame = cvQueryFrame(capture);
cvCopy(frame, frameCopy, 0);
cvCvtColor(frameCopy, gray, CV_BGR2GRAY);
cvResize(gray, smallImg, CV_INTER_LINEAR);
cvEqualizeHist(smallImg, smallImg);
cvClearMemStorage(storage);
double t = (double)cvGetTickCount();
CvSeq* faces = cvHaarDetectObjects( smallImg, cascade, storage,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_DO_CANNY_PRUNING
//|CV_HAAR_SCALE_IMAGE
,
cvSize(30, 30), cvSize(0, 0));
t = (double)cvGetTickCount() - t;
printf("detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.));
frameCopy = renderFaces(frameCopy, faces, scale);
cvShowImage("result", frameCopy);
if(cvWaitKey(5) == 27) {
cvReleaseImage(&frameCopy);
cvReleaseImage(&gray);
cvReleaseImage(&smallImg);
cvReleaseCapture(&capture);
cvDestroyWindow("result");
return 0;
}
}
}
static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{255,0,0}},
{{255,0,255}}
};
IplImage *renderFaces(IplImage *img, CvSeq *faces, double scale)
{
int i; for(i = 0; i < (faces ? faces->total : 0); i++) {
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
CvMat smallImg_roi;
CvPoint center;
int radius;
center.x = cvRound((r->x + r->width * 0.5) * scale);
center.y = cvRound((r->y + r->height * 0.5) * scale);
radius = cvRound((r->width + r->height) * 0.25 * scale);
cvCircle(img, center, radius, colors[i%8], 3, 8, 0);
}
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment