1 #include "opencv2/objdetect.hpp"
2 #include "opencv2/highgui.hpp"
3 #include "opencv2/imgproc.hpp"
4 #include "opencv2/core/utility.hpp"
5
6 #include "opencv2/videoio/videoio_c.h"
7 #include "opencv2/highgui/highgui_c.h"
8
9 #include <cctype>
10 #include <iostream>
11 #include <iterator>
12 #include <stdio.h>
13
14 using namespace std;
15 using namespace cv;
16
help()17 static void help()
18 {
19 cout << "\nThis program demonstrates the smile detector.\n"
20 "Usage:\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"
24 " [--try-flip]\n"
25 " [video_filename|camera_index]\n\n"
26 "Example:\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;
30 }
31
32 void detectAndDraw( Mat& img, CascadeClassifier& cascade,
33 CascadeClassifier& nestedCascade,
34 double scale, bool tryflip );
35
36 string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
37 string nestedCascadeName = "../../data/haarcascades/haarcascade_smile.xml";
38
39
main(int argc,const char ** argv)40 int main( int argc, const char** argv )
41 {
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();
52 string inputName;
53 bool tryflip = false;
54
55 help();
56
57 CascadeClassifier cascade, nestedCascade;
58 double scale = 1;
59
60 for( int i = 1; i < argc; i++ )
61 {
62 cout << "Processing " << i << " " << argv[i] << endl;
63 if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
64 {
65 cascadeName.assign( argv[i] + cascadeOptLen );
66 cout << " from which we have cascadeName= " << cascadeName << endl;
67 }
68 else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
69 {
70 if( argv[i][nestedCascadeOpt.length()] == '=' )
71 nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
72 }
73 else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
74 {
75 if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
76 scale = 1;
77 cout << " from which we read scale = " << scale << endl;
78 }
79 else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 )
80 {
81 tryflip = true;
82 cout << " will try to flip image horizontally to detect assymetric objects\n";
83 }
84 else if( argv[i][0] == '-' )
85 {
86 cerr << "WARNING: Unknown option " << argv[i] << endl;
87 }
88 else
89 inputName.assign( argv[i] );
90 }
91
92 if( !cascade.load( cascadeName ) )
93 {
94 cerr << "ERROR: Could not load face cascade" << endl;
95 help();
96 return -1;
97 }
98 if( !nestedCascade.load( nestedCascadeName ) )
99 {
100 cerr << "ERROR: Could not load smile cascade" << endl;
101 help();
102 return -1;
103 }
104
105 if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
106 {
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;
110 }
111 else if( inputName.size() )
112 {
113 capture = cvCaptureFromAVI( inputName.c_str() );
114 if(!capture) cout << "Capture from AVI didn't work" << endl;
115 }
116
117 cvNamedWindow( "result", 1 );
118
119 if( capture )
120 {
121 cout << "In capture ..." << endl;
122 cout << endl << "NOTE: Smile intensity will only be valid after a first smile has been detected" << endl;
123
124 for(;;)
125 {
126 IplImage* iplImg = cvQueryFrame( capture );
127 frame = cv::cvarrToMat(iplImg);
128 if( frame.empty() )
129 break;
130 if( iplImg->origin == IPL_ORIGIN_TL )
131 frame.copyTo( frameCopy );
132 else
133 flip( frame, frameCopy, 0 );
134
135 detectAndDraw( frameCopy, cascade, nestedCascade, scale, tryflip );
136
137 if( waitKey( 10 ) >= 0 )
138 goto _cleanup_;
139 }
140
141 waitKey(0);
142
143 _cleanup_:
144 cvReleaseCapture( &capture );
145 }
146 else
147 {
148 cerr << "ERROR: Could not initiate capture" << endl;
149 help();
150 return -1;
151 }
152
153 cvDestroyWindow("result");
154 return 0;
155 }
156
detectAndDraw(Mat & img,CascadeClassifier & cascade,CascadeClassifier & nestedCascade,double scale,bool tryflip)157 void detectAndDraw( Mat& img, CascadeClassifier& cascade,
158 CascadeClassifier& nestedCascade,
159 double scale, bool tryflip)
160 {
161 int i = 0;
162 vector<Rect> faces, faces2;
163 const static Scalar colors[] = { CV_RGB(0,0,255),
164 CV_RGB(0,128,255),
165 CV_RGB(0,255,255),
166 CV_RGB(0,255,0),
167 CV_RGB(255,128,0),
168 CV_RGB(255,255,0),
169 CV_RGB(255,0,0),
170 CV_RGB(255,0,255)} ;
171 Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
172
173 cvtColor( img, gray, COLOR_BGR2GRAY );
174 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
175 equalizeHist( smallImg, smallImg );
176
177 cascade.detectMultiScale( smallImg, faces,
178 1.1, 2, 0
179 //|CASCADE_FIND_BIGGEST_OBJECT
180 //|CASCADE_DO_ROUGH_SEARCH
181 |CASCADE_SCALE_IMAGE
182 ,
183 Size(30, 30) );
184 if( tryflip )
185 {
186 flip(smallImg, smallImg, 1);
187 cascade.detectMultiScale( smallImg, faces2,
188 1.1, 2, 0
189 //|CASCADE_FIND_BIGGEST_OBJECT
190 //|CASCADE_DO_ROUGH_SEARCH
191 |CASCADE_SCALE_IMAGE
192 ,
193 Size(30, 30) );
194 for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
195 {
196 faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
197 }
198 }
199
200 for( vector<Rect>::iterator r = faces.begin(); r != faces.end(); r++, i++ )
201 {
202 Mat smallImgROI;
203 vector<Rect> nestedObjects;
204 Point center;
205 Scalar color = colors[i%8];
206 int radius;
207
208 double aspect_ratio = (double)r->width/r->height;
209 if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
210 {
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 );
215 }
216 else
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)),
219 color, 3, 8, 0);
220
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,
226 1.1, 0, 0
227 //|CASCADE_FIND_BIGGEST_OBJECT
228 //|CASCADE_DO_ROUGH_SEARCH
229 //|CASCADE_DO_CANNY_PRUNING
230 |CASCADE_SCALE_IMAGE
231 ,
232 Size(30, 30) );
233
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);
242
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);
248 }
249
250 cv::imshow( "result", img );
251 }
252