1 #include "opencv2/objdetect.hpp"
2 #include "opencv2/highgui.hpp"
3 #include "opencv2/imgproc.hpp"
4 #include "opencv2/core/utility.hpp"
6 #include "opencv2/videoio/videoio_c.h"
7 #include "opencv2/highgui/highgui_c.h"
19 cout << "\nThis program demonstrates the smile detector.\n"
21 "./smiledetect [--cascade=<cascade_path> this is the frontal face classifier]\n"
22 " [--smile-cascade=[<smile_cascade_path>]]\n"
23 " [--scale=<image scale greater or equal to 1, try 2.0 for example. The larger the faster the processing>]\n"
25 " [video_filename|camera_index]\n\n"
27 "./smiledetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --smile-cascade=\"../../data/haarcascades/haarcascade_smile.xml\" --scale=2.0\n\n"
28 "During execution:\n\tHit any key to quit.\n"
29 "\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
32 void detectAndDraw( Mat& img, CascadeClassifier& cascade,
33 CascadeClassifier& nestedCascade,
34 double scale, bool tryflip );
36 string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
37 string nestedCascadeName = "../../data/haarcascades/haarcascade_smile.xml";
40 int main( int argc, const char** argv )
42 CvCapture* capture = 0;
43 Mat frame, frameCopy, image;
44 const string scaleOpt = "--scale=";
45 size_t scaleOptLen = scaleOpt.length();
46 const string cascadeOpt = "--cascade=";
47 size_t cascadeOptLen = cascadeOpt.length();
48 const string nestedCascadeOpt = "--smile-cascade";
49 size_t nestedCascadeOptLen = nestedCascadeOpt.length();
50 const string tryFlipOpt = "--try-flip";
51 size_t tryFlipOptLen = tryFlipOpt.length();
57 CascadeClassifier cascade, nestedCascade;
60 for( int i = 1; i < argc; i++ )
62 cout << "Processing " << i << " " << argv[i] << endl;
63 if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
65 cascadeName.assign( argv[i] + cascadeOptLen );
66 cout << " from which we have cascadeName= " << cascadeName << endl;
68 else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
70 if( argv[i][nestedCascadeOpt.length()] == '=' )
71 nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
73 else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
75 if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
77 cout << " from which we read scale = " << scale << endl;
79 else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 )
82 cout << " will try to flip image horizontally to detect assymetric objects\n";
84 else if( argv[i][0] == '-' )
86 cerr << "WARNING: Unknown option " << argv[i] << endl;
89 inputName.assign( argv[i] );
92 if( !cascade.load( cascadeName ) )
94 cerr << "ERROR: Could not load face cascade" << endl;
98 if( !nestedCascade.load( nestedCascadeName ) )
100 cerr << "ERROR: Could not load smile cascade" << endl;
105 if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
107 capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
108 int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ;
109 if(!capture) cout << "Capture from CAM " << c << " didn't work" << endl;
111 else if( inputName.size() )
113 capture = cvCaptureFromAVI( inputName.c_str() );
114 if(!capture) cout << "Capture from AVI didn't work" << endl;
117 cvNamedWindow( "result", 1 );
121 cout << "In capture ..." << endl;
122 cout << endl << "NOTE: Smile intensity will only be valid after a first smile has been detected" << endl;
126 IplImage* iplImg = cvQueryFrame( capture );
127 frame = cv::cvarrToMat(iplImg);
130 if( iplImg->origin == IPL_ORIGIN_TL )
131 frame.copyTo( frameCopy );
133 flip( frame, frameCopy, 0 );
135 detectAndDraw( frameCopy, cascade, nestedCascade, scale, tryflip );
137 if( waitKey( 10 ) >= 0 )
144 cvReleaseCapture( &capture );
148 cerr << "ERROR: Could not initiate capture" << endl;
153 cvDestroyWindow("result");
157 void detectAndDraw( Mat& img, CascadeClassifier& cascade,
158 CascadeClassifier& nestedCascade,
159 double scale, bool tryflip)
162 vector<Rect> faces, faces2;
163 const static Scalar colors[] = { CV_RGB(0,0,255),
171 Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
173 cvtColor( img, gray, COLOR_BGR2GRAY );
174 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
175 equalizeHist( smallImg, smallImg );
177 cascade.detectMultiScale( smallImg, faces,
179 //|CASCADE_FIND_BIGGEST_OBJECT
180 //|CASCADE_DO_ROUGH_SEARCH
186 flip(smallImg, smallImg, 1);
187 cascade.detectMultiScale( smallImg, faces2,
189 //|CASCADE_FIND_BIGGEST_OBJECT
190 //|CASCADE_DO_ROUGH_SEARCH
194 for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
196 faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
200 for( vector<Rect>::iterator r = faces.begin(); r != faces.end(); r++, i++ )
203 vector<Rect> nestedObjects;
205 Scalar color = colors[i%8];
208 double aspect_ratio = (double)r->width/r->height;
209 if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
211 center.x = cvRound((r->x + r->width*0.5)*scale);
212 center.y = cvRound((r->y + r->height*0.5)*scale);
213 radius = cvRound((r->width + r->height)*0.25*scale);
214 circle( img, center, radius, color, 3, 8, 0 );
217 rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
218 cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
221 const int half_height=cvRound((float)r->height/2);
222 r->y=r->y + half_height;
223 r->height = half_height;
224 smallImgROI = smallImg(*r);
225 nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
227 //|CASCADE_FIND_BIGGEST_OBJECT
228 //|CASCADE_DO_ROUGH_SEARCH
229 //|CASCADE_DO_CANNY_PRUNING
234 // The number of detected neighbors depends on image size (and also illumination, etc.). The
235 // following steps use a floating minimum and maximum of neighbors. Intensity thus estimated will be
236 //accurate only after a first smile has been displayed by the user.
237 const int smile_neighbors = (int)nestedObjects.size();
238 static int max_neighbors=-1;
239 static int min_neighbors=-1;
240 if (min_neighbors == -1) min_neighbors = smile_neighbors;
241 max_neighbors = MAX(max_neighbors, smile_neighbors);
243 // Draw rectangle on the left side of the image reflecting smile intensity
244 float intensityZeroOne = ((float)smile_neighbors - min_neighbors) / (max_neighbors - min_neighbors + 1);
245 int rect_height = cvRound((float)img.rows * intensityZeroOne);
246 CvScalar col = CV_RGB((float)255 * intensityZeroOne, 0, 0);
247 rectangle(img, cvPoint(0, img.rows), cvPoint(img.cols/10, img.rows - rect_height), col, -1);
250 cv::imshow( "result", img );