1 #include "opencv2/objdetect/objdetect.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/imgproc/imgproc.hpp"
4 #include "opencv2/ocl/ocl.hpp"
12 const static Scalar colors[] = { CV_RGB(0,0,255),
27 static void workBegin()
29 work_begin = getTickCount();
33 work_end += (getTickCount() - work_begin);
35 static double getTime()
37 return work_end /((double)cvGetTickFrequency() * 1000.);
41 void detect( Mat& img, vector<Rect>& faces,
42 ocl::OclCascadeClassifierBuf& cascade,
43 double scale, bool calTime);
46 void detectCPU( Mat& img, vector<Rect>& faces,
47 CascadeClassifier& cascade,
48 double scale, bool calTime);
51 void Draw(Mat& img, vector<Rect>& faces, double scale);
54 // This function test if gpu_rst matches cpu_rst.
55 // If the two vectors are not equal, it will return the difference in vector size
56 // Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
57 double checkRectSimilarity(Size sz, vector<Rect>& cpu_rst, vector<Rect>& gpu_rst);
60 int main( int argc, const char** argv )
63 "{ h | help | false | print help message }"
64 "{ i | input | | specify input image }"
65 "{ t | template | haarcascade_frontalface_alt.xml |"
66 " specify template file path }"
67 "{ c | scale | 1.0 | scale image }"
68 "{ s | use_cpu | false | use cpu or gpu to process the image }"
69 "{ o | output | facedetect_output.jpg |"
70 " specify output image save path(only works when input is images) }";
72 CommandLineParser cmd(argc, argv, keys);
73 if (cmd.get<bool>("help"))
75 cout << "Avaible options:" << endl;
79 CvCapture* capture = 0;
80 Mat frame, frameCopy, image;
82 bool useCPU = cmd.get<bool>("s");
83 string inputName = cmd.get<string>("i");
84 outputName = cmd.get<string>("o");
85 string cascadeName = cmd.get<string>("t");
86 double scale = cmd.get<double>("c");
87 ocl::OclCascadeClassifierBuf cascade;
88 CascadeClassifier cpu_cascade;
90 if( !cascade.load( cascadeName ) || !cpu_cascade.load(cascadeName) )
92 cerr << "ERROR: Could not load classifier cascade" << endl;
96 if( inputName.empty() )
98 capture = cvCaptureFromCAM(0);
100 cout << "Capture from CAM 0 didn't work" << endl;
102 else if( inputName.size() )
104 image = imread( inputName, 1 );
107 capture = cvCaptureFromAVI( inputName.c_str() );
109 cout << "Capture from AVI didn't work" << endl;
115 image = imread( "lena.jpg", 1 );
117 cout << "Couldn't read lena.jpg" << endl;
122 cvNamedWindow( "result", 1 );
123 vector<ocl::Info> oclinfo;
124 int devnums = ocl::getDevice(oclinfo);
127 std::cout << "no device found\n";
130 //if you want to use undefault device, set it here
131 //setDevice(oclinfo[0]);
132 ocl::setBinpath("./");
135 cout << "In capture ..." << endl;
138 IplImage* iplImg = cvQueryFrame( capture );
143 if( iplImg->origin == IPL_ORIGIN_TL )
144 frame.copyTo( frameCopy );
146 flip( frame, frameCopy, 0 );
149 detectCPU(frameCopy, faces, cpu_cascade, scale, false);
153 detect(frameCopy, faces, cascade, scale, false);
155 Draw(frameCopy, faces, scale);
156 if( waitKey( 10 ) >= 0 )
165 cvReleaseCapture( &capture );
169 cout << "In image read" << endl;
171 vector<Rect> ref_rst;
172 double accuracy = 0.;
173 for(int i = 0; i <= LOOP_NUM; i ++)
175 cout << "loop" << i << endl;
178 detectCPU(image, faces, cpu_cascade, scale, i==0?false:true);
182 detect(image, faces, cascade, scale, i==0?false:true);
185 detectCPU(image, ref_rst, cpu_cascade, scale, false);
186 accuracy = checkRectSimilarity(image.size(), ref_rst, faces);
192 cout << "average CPU time (noCamera) : ";
194 cout << "average GPU time (noCamera) : ";
195 cout << getTime() / LOOP_NUM << " ms" << endl;
196 cout << "accuracy value: " << accuracy <<endl;
199 Draw(image, faces, scale);
203 cvDestroyWindow("result");
207 void detect( Mat& img, vector<Rect>& faces,
208 ocl::OclCascadeClassifierBuf& cascade,
209 double scale, bool calTime)
211 ocl::oclMat image(img);
212 ocl::oclMat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
213 if(calTime) workBegin();
214 ocl::cvtColor( image, gray, CV_BGR2GRAY );
215 ocl::resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
216 ocl::equalizeHist( smallImg, smallImg );
218 cascade.detectMultiScale( smallImg, faces, 1.1,
221 , Size(30,30), Size(0, 0) );
222 if(calTime) workEnd();
226 void detectCPU( Mat& img, vector<Rect>& faces,
227 CascadeClassifier& cascade,
228 double scale, bool calTime)
230 if(calTime) workBegin();
231 Mat cpu_gray, cpu_smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
232 cvtColor(img, cpu_gray, CV_BGR2GRAY);
233 resize(cpu_gray, cpu_smallImg, cpu_smallImg.size(), 0, 0, INTER_LINEAR);
234 equalizeHist(cpu_smallImg, cpu_smallImg);
235 cascade.detectMultiScale(cpu_smallImg, faces, 1.1,
236 3, 0 | CV_HAAR_SCALE_IMAGE,
237 Size(30, 30), Size(0, 0));
238 if(calTime) workEnd();
242 void Draw(Mat& img, vector<Rect>& faces, double scale)
245 for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
248 Scalar color = colors[i%8];
250 center.x = cvRound((r->x + r->width*0.5)*scale);
251 center.y = cvRound((r->y + r->height*0.5)*scale);
252 radius = cvRound((r->width + r->height)*0.25*scale);
253 circle( img, center, radius, color, 3, 8, 0 );
255 imwrite( outputName, img );
256 if(abs(scale-1.0)>.001)
258 resize(img, img, Size((int)(img.cols/scale), (int)(img.rows/scale)));
260 imshow( "result", img );
265 double checkRectSimilarity(Size sz, vector<Rect>& ob1, vector<Rect>& ob2)
267 double final_test_result = 0.0;
268 size_t sz1 = ob1.size();
269 size_t sz2 = ob2.size();
273 return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
279 Mat cpu_result(sz, CV_8UC1);
282 for(vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
284 Mat cpu_result_roi(cpu_result, *r);
285 cpu_result_roi.setTo(1);
286 cpu_result.copyTo(cpu_result);
288 int cpu_area = countNonZero(cpu_result > 0);
291 Mat gpu_result(sz, CV_8UC1);
293 for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
295 cv::Mat gpu_result_roi(gpu_result, *r2);
296 gpu_result_roi.setTo(1);
297 gpu_result.copyTo(gpu_result);
301 multiply(cpu_result, gpu_result, result_);
302 int result = countNonZero(result_ > 0);
303 if(cpu_area!=0 && result!=0)
304 final_test_result = 1.0 - (double)result/(double)cpu_area;
305 else if(cpu_area==0 && result!=0)
306 final_test_result = -1;
308 return final_test_result;