Skip to content

Commit fd4761b

Browse files
committed
Update face detection samples
1 parent 9533982 commit fd4761b

File tree

3 files changed

+134
-179
lines changed

3 files changed

+134
-179
lines changed

samples/cpp/facedetect.cpp

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
#include "opencv2/objdetect.hpp"
2-
#include "opencv2/imgcodecs.hpp"
3-
#include "opencv2/videoio.hpp"
42
#include "opencv2/highgui.hpp"
53
#include "opencv2/imgproc.hpp"
6-
#include "opencv2/core/utility.hpp"
7-
8-
#include "opencv2/videoio/videoio_c.h"
9-
#include "opencv2/highgui/highgui_c.h"
10-
11-
#include <cctype>
124
#include <iostream>
13-
#include <iterator>
14-
#include <stdio.h>
155

166
using namespace std;
177
using namespace cv;
@@ -28,7 +18,7 @@ static void help()
2818
" [--try-flip]\n"
2919
" [filename|camera_index]\n\n"
3020
"see facedetect.cmd for one call:\n"
31-
"./facedetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"../../data/haarcascades/haarcascade_eye.xml\" --scale=1.3\n\n"
21+
"./facedetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\" --scale=1.3\n\n"
3222
"During execution:\n\tHit any key to quit.\n"
3323
"\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
3424
}
@@ -42,8 +32,8 @@ string nestedCascadeName = "../../data/haarcascades/haarcascade_eye_tree_eyeglas
4232

4333
int main( int argc, const char** argv )
4434
{
45-
CvCapture* capture = 0;
46-
Mat frame, frameCopy, image;
35+
VideoCapture capture;
36+
Mat frame, image;
4737
const string scaleOpt = "--scale=";
4838
size_t scaleOptLen = scaleOpt.length();
4939
const string cascadeOpt = "--cascade=";
@@ -103,17 +93,17 @@ int main( int argc, const char** argv )
10393

10494
if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
10595
{
106-
capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
10796
int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ;
108-
if(!capture) cout << "Capture from CAM " << c << " didn't work" << endl;
97+
if(!capture.open(c))
98+
cout << "Capture from camera #" << c << " didn't work" << endl;
10999
}
110100
else if( inputName.size() )
111101
{
112102
image = imread( inputName, 1 );
113103
if( image.empty() )
114104
{
115-
capture = cvCaptureFromAVI( inputName.c_str() );
116-
if(!capture) cout << "Capture from AVI didn't work" << endl;
105+
if(!capture.open( inputName ))
106+
cout << "Could not read " << inputName << endl;
117107
}
118108
}
119109
else
@@ -122,36 +112,27 @@ int main( int argc, const char** argv )
122112
if(image.empty()) cout << "Couldn't read ../data/lena.jpg" << endl;
123113
}
124114

125-
cvNamedWindow( "result", 1 );
126-
127-
if( capture )
115+
if( capture.isOpened() )
128116
{
129-
cout << "In capture ..." << endl;
117+
cout << "Video capturing has been started ..." << endl;
118+
130119
for(;;)
131120
{
132-
IplImage* iplImg = cvQueryFrame( capture );
133-
frame = cv::cvarrToMat(iplImg);
121+
capture >> frame;
134122
if( frame.empty() )
135123
break;
136-
if( iplImg->origin == IPL_ORIGIN_TL )
137-
frame.copyTo( frameCopy );
138-
else
139-
flip( frame, frameCopy, 0 );
140124

141-
detectAndDraw( frameCopy, cascade, nestedCascade, scale, tryflip );
125+
Mat frame1 = frame.clone();
126+
detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );
142127

143-
if( waitKey( 10 ) >= 0 )
144-
goto _cleanup_;
128+
int c = waitKey(10);
129+
if( c == 27 || c == 'q' || c == 'Q' )
130+
break;
145131
}
146-
147-
waitKey(0);
148-
149-
_cleanup_:
150-
cvReleaseCapture( &capture );
151132
}
152133
else
153134
{
154-
cout << "In image read" << endl;
135+
cout << "Detecting face(s) in " << inputName << endl;
155136
if( !image.empty() )
156137
{
157138
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
@@ -190,39 +171,39 @@ int main( int argc, const char** argv )
190171
}
191172
}
192173

193-
cvDestroyWindow("result");
194-
195174
return 0;
196175
}
197176

198177
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
199178
CascadeClassifier& nestedCascade,
200179
double scale, bool tryflip )
201180
{
202-
int i = 0;
203181
double t = 0;
204182
vector<Rect> faces, faces2;
205-
const static Scalar colors[] = { CV_RGB(0,0,255),
206-
CV_RGB(0,128,255),
207-
CV_RGB(0,255,255),
208-
CV_RGB(0,255,0),
209-
CV_RGB(255,128,0),
210-
CV_RGB(255,255,0),
211-
CV_RGB(255,0,0),
212-
CV_RGB(255,0,255)} ;
213-
Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
183+
const static Scalar colors[] =
184+
{
185+
Scalar(255,0,0),
186+
Scalar(255,128,0),
187+
Scalar(255,255,0),
188+
Scalar(0,255,0),
189+
Scalar(0,128,255),
190+
Scalar(0,255,255),
191+
Scalar(0,0,255),
192+
Scalar(255,0,255)
193+
};
194+
Mat gray, smallImg;
214195

215196
cvtColor( img, gray, COLOR_BGR2GRAY );
216-
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
197+
double fx = 1 / scale;
198+
resize( gray, smallImg, Size(), fx, fx, INTER_LINEAR );
217199
equalizeHist( smallImg, smallImg );
218200

219201
t = (double)cvGetTickCount();
220202
cascade.detectMultiScale( smallImg, faces,
221203
1.1, 2, 0
222204
//|CASCADE_FIND_BIGGEST_OBJECT
223205
//|CASCADE_DO_ROUGH_SEARCH
224-
|CASCADE_SCALE_IMAGE
225-
,
206+
|CASCADE_SCALE_IMAGE,
226207
Size(30, 30) );
227208
if( tryflip )
228209
{
@@ -231,8 +212,7 @@ void detectAndDraw( Mat& img, CascadeClassifier& cascade,
231212
1.1, 2, 0
232213
//|CASCADE_FIND_BIGGEST_OBJECT
233214
//|CASCADE_DO_ROUGH_SEARCH
234-
|CASCADE_SCALE_IMAGE
235-
,
215+
|CASCADE_SCALE_IMAGE,
236216
Size(30, 30) );
237217
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
238218
{
@@ -241,44 +221,45 @@ void detectAndDraw( Mat& img, CascadeClassifier& cascade,
241221
}
242222
t = (double)cvGetTickCount() - t;
243223
printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
244-
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
224+
for ( size_t i = 0; i < faces.size(); i++ )
245225
{
226+
Rect r = faces[i];
246227
Mat smallImgROI;
247228
vector<Rect> nestedObjects;
248229
Point center;
249230
Scalar color = colors[i%8];
250231
int radius;
251232

252-
double aspect_ratio = (double)r->width/r->height;
233+
double aspect_ratio = (double)r.width/r.height;
253234
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
254235
{
255-
center.x = cvRound((r->x + r->width*0.5)*scale);
256-
center.y = cvRound((r->y + r->height*0.5)*scale);
257-
radius = cvRound((r->width + r->height)*0.25*scale);
236+
center.x = cvRound((r.x + r.width*0.5)*scale);
237+
center.y = cvRound((r.y + r.height*0.5)*scale);
238+
radius = cvRound((r.width + r.height)*0.25*scale);
258239
circle( img, center, radius, color, 3, 8, 0 );
259240
}
260241
else
261-
rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
262-
cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
242+
rectangle( img, cvPoint(cvRound(r.x*scale), cvRound(r.y*scale)),
243+
cvPoint(cvRound((r.x + r.width-1)*scale), cvRound((r.y + r.height-1)*scale)),
263244
color, 3, 8, 0);
264245
if( nestedCascade.empty() )
265246
continue;
266-
smallImgROI = smallImg(*r);
247+
smallImgROI = smallImg( r );
267248
nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
268249
1.1, 2, 0
269250
//|CASCADE_FIND_BIGGEST_OBJECT
270251
//|CASCADE_DO_ROUGH_SEARCH
271252
//|CASCADE_DO_CANNY_PRUNING
272-
|CASCADE_SCALE_IMAGE
273-
,
253+
|CASCADE_SCALE_IMAGE,
274254
Size(30, 30) );
275-
for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
255+
for ( size_t j = 0; j < nestedObjects.size(); j++ )
276256
{
277-
center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
278-
center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
279-
radius = cvRound((nr->width + nr->height)*0.25*scale);
257+
Rect nr = nestedObjects[j];
258+
center.x = cvRound((r.x + nr.x + nr.width*0.5)*scale);
259+
center.y = cvRound((r.y + nr.y + nr.height*0.5)*scale);
260+
radius = cvRound((nr.width + nr.height)*0.25*scale);
280261
circle( img, center, radius, color, 3, 8, 0 );
281262
}
282263
}
283-
cv::imshow( "result", img );
264+
imshow( "result", img );
284265
}

0 commit comments

Comments
 (0)