18 static CvMemStorage* storage = 0;
19 static CvHaarClassifierCascade* cascade = 0;
21 void detect_and_draw( IplImage* image );
23 const char* cascade_name =
24 "haarcascade_frontalface_alt.xml";
25 /* "haarcascade_profileface.xml";*/
27 int main( int argc, char** argv )
29 CvCapture* capture = 0;
30 IplImage *frame, *frame_copy = 0;
31 int optlen = strlen("--cascade=");
32 const char* input_name;
34 if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
36 cascade_name = argv[1] + optlen;
37 input_name = argc > 2 ? argv[2] : 0;
41 cascade_name = "../../data/haarcascades/haarcascade_frontalface_alt2.xml";
42 input_name = argc > 1 ? argv[1] : 0;
45 cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
49 fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
51 "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
54 storage = cvCreateMemStorage(0);
56 if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
57 capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
59 capture = cvCaptureFromAVI( input_name );
61 cvNamedWindow( "result", 1 );
67 if( !cvGrabFrame( capture ))
69 frame = cvRetrieveFrame( capture );
73 frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
74 IPL_DEPTH_8U, frame->nChannels );
75 if( frame->origin == IPL_ORIGIN_TL )
76 cvCopy( frame, frame_copy, 0 );
78 cvFlip( frame, frame_copy, 0 );
80 detect_and_draw( frame_copy );
82 if( cvWaitKey( 10 ) >= 0 )
86 cvReleaseImage( &frame_copy );
87 cvReleaseCapture( &capture );
91 const char* filename = input_name ? input_name : (char*)"lena.jpg";
92 IplImage* image = cvLoadImage( filename, 1 );
96 detect_and_draw( image );
98 cvReleaseImage( &image );
102 /* assume it is a text file containing the
103 list of the image filenames to be processed - one per line */
104 FILE* f = fopen( filename, "rt" );
108 while( fgets( buf, 1000, f ) )
110 int len = (int)strlen(buf);
111 while( len > 0 && isspace(buf[len-1]) )
114 image = cvLoadImage( buf, 1 );
117 detect_and_draw( image );
119 cvReleaseImage( &image );
128 cvDestroyWindow("result");
133 void detect_and_draw( IplImage* img )
135 static CvScalar colors[] =
148 IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
149 IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
150 cvRound (img->height/scale)),
154 cvCvtColor( img, gray, CV_BGR2GRAY );
155 cvResize( gray, small_img, CV_INTER_LINEAR );
156 cvEqualizeHist( small_img, small_img );
157 cvClearMemStorage( storage );
161 double t = (double)cvGetTickCount();
162 CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
163 1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
165 t = (double)cvGetTickCount() - t;
166 printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
167 for( i = 0; i < (faces ? faces->total : 0); i++ )
169 CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
172 center.x = cvRound((r->x + r->width*0.5)*scale);
173 center.y = cvRound((r->y + r->height*0.5)*scale);
174 radius = cvRound((r->width + r->height)*0.25*scale);
175 cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
179 cvShowImage( "result", img );
180 cvReleaseImage( &gray );
181 cvReleaseImage( &small_img );