1 #include <opencv2/objdetect/objdetect.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3 #include <opencv2/imgproc/imgproc.hpp>
11 void detectAndDraw( Mat& img,
12 CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
16 "../../data/haarcascades/haarcascade_frontalface_alt.xml";
17 String nestedCascadeName =
18 "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
20 int main( int argc, const char** argv )
22 CvCapture* capture = 0;
23 Mat frame, frameCopy, image;
24 const String scaleOpt = "--scale=";
25 size_t scaleOptLen = scaleOpt.length();
26 const String cascadeOpt = "--cascade=";
27 size_t cascadeOptLen = cascadeOpt.length();
28 const String nestedCascadeOpt = "--nested-cascade";
29 size_t nestedCascadeOptLen = nestedCascadeOpt.length();
32 CascadeClassifier cascade, nestedCascade;
35 for( int i = 1; i < argc; i++ )
37 if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
38 cascadeName.assign( argv[i] + cascadeOptLen );
39 else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
41 if( argv[i][nestedCascadeOpt.length()] == '=' )
42 nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
43 if( !nestedCascade.load( nestedCascadeName ) )
44 cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
46 else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
48 if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
51 else if( argv[i][0] == '-' )
53 cerr << "WARNING: Unknown option %s" << argv[i] << endl;
56 inputName.assign( argv[i] );
59 if( !cascade.load( cascadeName ) )
61 cerr << "ERROR: Could not load classifier cascade" << endl;
62 cerr << "Usage: facedetect [--cascade=\"<cascade_path>\"]\n"
63 " [--nested-cascade[=\"nested_cascade_path\"]]\n"
64 " [--scale[=<image scale>\n"
65 " [filename|camera_index]\n" ;
69 if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
70 capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
71 else if( inputName.size() )
73 image = imread( inputName, 1 );
75 capture = cvCaptureFromAVI( inputName.c_str() );
78 image = imread( "lena.jpg", 1 );
80 cvNamedWindow( "result", 1 );
86 IplImage* iplImg = cvQueryFrame( capture );
90 if( iplImg->origin == IPL_ORIGIN_TL )
91 frame.copyTo( frameCopy );
93 flip( frame, frameCopy, 0 );
95 detectAndDraw( frameCopy, cascade, nestedCascade, scale );
97 if( waitKey( 10 ) >= 0 )
103 cvReleaseCapture( &capture );
109 detectAndDraw( image, cascade, nestedCascade, scale );
112 else if( !inputName.empty() )
114 /* assume it is a text file containing the
115 list of the image filenames to be processed - one per line */
116 FILE* f = fopen( inputName.c_str(), "rt" );
120 while( fgets( buf, 1000, f ) )
122 int len = (int)strlen(buf), c;
123 while( len > 0 && isspace(buf[len-1]) )
126 cout << "file " << buf << endl;
127 image = imread( buf, 1 );
130 detectAndDraw( image, cascade, nestedCascade, scale );
132 if( c == 27 || c == 'q' || c == 'Q' )
141 cvDestroyWindow("result");
146 void detectAndDraw( Mat& img,
147 CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
153 const static Scalar colors[] = { CV_RGB(0,0,255),
161 Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
163 cvtColor( img, gray, CV_BGR2GRAY );
164 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
165 equalizeHist( smallImg, smallImg );
167 t = (double)cvGetTickCount();
168 cascade.detectMultiScale( smallImg, faces,
170 //|CV_HAAR_FIND_BIGGEST_OBJECT
171 //|CV_HAAR_DO_ROUGH_SEARCH
175 t = (double)cvGetTickCount() - t;
176 printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
177 for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
180 vector<Rect> nestedObjects;
182 Scalar color = colors[i%8];
184 center.x = cvRound((r->x + r->width*0.5)*scale);
185 center.y = cvRound((r->y + r->height*0.5)*scale);
186 radius = cvRound((r->width + r->height)*0.25*scale);
187 circle( img, center, radius, color, 3, 8, 0 );
188 if( nestedCascade.empty() )
190 smallImgROI = smallImg(*r);
191 nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
193 //|CV_HAAR_FIND_BIGGEST_OBJECT
194 //|CV_HAAR_DO_ROUGH_SEARCH
195 //|CV_HAAR_DO_CANNY_PRUNING
199 for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
201 center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
202 center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
203 radius = cvRound((nr->width + nr->height)*0.25*scale);
204 circle( img, center, radius, color, 3, 8, 0 );
207 cv::imshow( "result", img );