7 #include "opencv2/ocl/ocl.hpp"
8 #include "opencv2/highgui/highgui.hpp"
16 App(CommandLineParser& cmd);
18 void handleKey(char key);
21 string hogWorkFps() const;
24 string workFps() const;
25 string message() const;
28 // This function test if gpu_rst matches cpu_rst.
29 // If the two vectors are not equal, it will return the difference in vector size
30 // Else if will return
31 // (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
32 double checkRectSimilarity(Size sz,
33 std::vector<Rect>& cpu_rst,
34 std::vector<Rect>& gpu_rst);
45 int win_stride_width, win_stride_height;
63 int main(int argc, char** argv)
66 "{ h | help | false | print help message }"
67 "{ i | input | | specify input image}"
68 "{ c | camera | -1 | enable camera capturing }"
69 "{ v | video | | use video as input }"
70 "{ g | gray | false | convert image to gray one or not}"
71 "{ s | scale | 1.0 | resize the image before detect}"
72 "{ l |larger_win| false | use 64x128 window}"
73 "{ o | output | | specify output path when input is images}";
74 CommandLineParser cmd(argc, argv, keys);
80 catch (const Exception& e)
82 return cout << "error: " << e.what() << endl, 1;
84 catch (const exception& e)
86 return cout << "error: " << e.what() << endl, 1;
90 return cout << "unknown exception" << endl, 1;
95 App::App(CommandLineParser& cmd)
97 cout << "\nControls:\n"
99 << "\tm - change mode GPU <-> CPU\n"
100 << "\tg - convert image to gray or not\n"
101 << "\to - save output image once, or switch on/off video save\n"
102 << "\t1/q - increase/decrease HOG scale\n"
103 << "\t2/w - increase/decrease levels count\n"
104 << "\t3/e - increase/decrease HOG group threshold\n"
105 << "\t4/r - increase/decrease hit threshold\n"
110 make_gray = cmd.get<bool>("g");
111 resize_scale = cmd.get<double>("s");
112 win_width = cmd.get<bool>("l") == true ? 64 : 48;
113 vdo_source = cmd.get<string>("v");
114 img_source = cmd.get<string>("i");
115 output = cmd.get<string>("o");
116 camera_id = cmd.get<int>("c");
118 win_stride_width = 8;
119 win_stride_height = 8;
122 hit_threshold = win_width == 48 ? 1.4 : 0.;
127 cout << "Group threshold: " << gr_threshold << endl;
128 cout << "Levels number: " << nlevels << endl;
129 cout << "Win width: " << win_width << endl;
130 cout << "Win stride: (" << win_stride_width << ", " << win_stride_height << ")\n";
131 cout << "Hit threshold: " << hit_threshold << endl;
132 cout << "Gamma correction: " << gamma_corr << endl;
138 vector<ocl::Info> oclinfo;
139 ocl::getDevice(oclinfo);
141 VideoWriter video_writer;
143 Size win_size(win_width, win_width * 2);
144 Size win_stride(win_stride_width, win_stride_height);
146 // Create HOG descriptors and detectors here
147 vector<float> detector;
148 if (win_size == Size(64, 128))
149 detector = ocl::HOGDescriptor::getPeopleDetector64x128();
151 detector = ocl::HOGDescriptor::getPeopleDetector48x96();
154 ocl::HOGDescriptor gpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9,
155 ocl::HOGDescriptor::DEFAULT_WIN_SIGMA, 0.2, gamma_corr,
156 ocl::HOGDescriptor::DEFAULT_NLEVELS);
157 HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9, 1, -1,
158 HOGDescriptor::L2Hys, 0.2, gamma_corr, cv::HOGDescriptor::DEFAULT_NLEVELS);
159 gpu_hog.setSVMDetector(detector);
160 cpu_hog.setSVMDetector(detector);
169 vc.open(vdo_source.c_str());
171 throw runtime_error(string("can't open video file: " + vdo_source));
174 else if (camera_id != -1)
180 msg << "can't open camera: " << camera_id;
181 throw runtime_error(msg.str());
187 frame = imread(img_source);
189 throw runtime_error(string("can't open image file: " + img_source));
192 Mat img_aux, img, img_to_show;
195 // Iterate over all frames
197 while (running && !frame.empty())
201 // Change format of the image
202 if (make_gray) cvtColor(frame, img_aux, CV_BGR2GRAY);
203 else if (use_gpu) cvtColor(frame, img_aux, CV_BGR2BGRA);
204 else frame.copyTo(img_aux);
207 if (abs(scale-1.0)>0.001)
209 Size sz((int)((double)img_aux.cols/resize_scale), (int)((double)img_aux.rows/resize_scale));
210 resize(img_aux, img, sz);
214 gpu_hog.nlevels = nlevels;
215 cpu_hog.nlevels = nlevels;
218 // Perform HOG classification
223 gpu_hog.detectMultiScale(gpu_img, found, hit_threshold, win_stride,
224 Size(0, 0), scale, gr_threshold);
227 // verify if GPU output same objects with CPU at 1st run
229 vector<Rect> ref_rst;
230 cvtColor(img, img, CV_BGRA2BGR);
231 cpu_hog.detectMultiScale(img, ref_rst, hit_threshold, win_stride,
232 Size(0, 0), scale, gr_threshold-2);
233 double accuracy = checkRectSimilarity(img.size(), ref_rst, found);
234 cout << "\naccuracy value: " << accuracy << endl;
237 else cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
238 Size(0, 0), scale, gr_threshold);
242 // Draw positive classified windows
243 for (size_t i = 0; i < found.size(); i++)
246 rectangle(img_to_show, r.tl(), r.br(), CV_RGB(0, 255, 0), 3);
250 putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
252 putText(img_to_show, "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
253 putText(img_to_show, "FPS (HOG only): " + hogWorkFps(), Point(5, 65), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
254 putText(img_to_show, "FPS (total): " + workFps(), Point(5, 105), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
255 imshow("opencv_gpu_hog", img_to_show);
256 if (vdo_source!="" || camera_id!=-1) vc >> frame;
260 if (output!="" && write_once)
262 if (img_source!="") // wirte image
265 imwrite(output, img_to_show);
269 if (!video_writer.isOpened())
271 video_writer.open(output, CV_FOURCC('x','v','i','d'), 24,
272 img_to_show.size(), true);
273 if (!video_writer.isOpened())
274 throw std::runtime_error("can't create video writer");
277 if (make_gray) cvtColor(img_to_show, img, CV_GRAY2BGR);
278 else cvtColor(img_to_show, img, CV_BGRA2BGR);
284 handleKey((char)waitKey(3));
289 void App::handleKey(char key)
299 cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
303 make_gray = !make_gray;
304 cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
308 cout << "Scale: " << scale << endl;
313 cout << "Scale: " << scale << endl;
317 cout << "Levels number: " << nlevels << endl;
321 nlevels = max(nlevels - 1, 1);
322 cout << "Levels number: " << nlevels << endl;
326 cout << "Group threshold: " << gr_threshold << endl;
330 gr_threshold = max(0, gr_threshold - 1);
331 cout << "Group threshold: " << gr_threshold << endl;
335 cout << "Hit threshold: " << hit_threshold << endl;
339 hit_threshold = max(0.0, hit_threshold - 0.25);
340 cout << "Hit threshold: " << hit_threshold << endl;
344 gamma_corr = !gamma_corr;
345 cout << "Gamma correction: " << gamma_corr << endl;
349 write_once = !write_once;
355 inline void App::hogWorkBegin()
357 hog_work_begin = getTickCount();
360 inline void App::hogWorkEnd()
362 int64 delta = getTickCount() - hog_work_begin;
363 double freq = getTickFrequency();
364 hog_work_fps = freq / delta;
367 inline string App::hogWorkFps() const
374 inline void App::workBegin()
376 work_begin = getTickCount();
379 inline void App::workEnd()
381 int64 delta = getTickCount() - work_begin;
382 double freq = getTickFrequency();
383 work_fps = freq / delta;
386 inline string App::workFps() const
394 double App::checkRectSimilarity(Size sz,
395 std::vector<Rect>& ob1,
396 std::vector<Rect>& ob2)
398 double final_test_result = 0.0;
399 size_t sz1 = ob1.size();
400 size_t sz2 = ob2.size();
404 return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
410 cv::Mat cpu_result(sz, CV_8UC1);
414 for(vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
416 cv::Mat cpu_result_roi(cpu_result, *r);
417 cpu_result_roi.setTo(1);
418 cpu_result.copyTo(cpu_result);
420 int cpu_area = cv::countNonZero(cpu_result > 0);
423 cv::Mat gpu_result(sz, CV_8UC1);
425 for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
427 cv::Mat gpu_result_roi(gpu_result, *r2);
428 gpu_result_roi.setTo(1);
429 gpu_result.copyTo(gpu_result);
433 multiply(cpu_result, gpu_result, result_);
434 int result = cv::countNonZero(result_ > 0);
435 if(cpu_area!=0 && result!=0)
436 final_test_result = 1.0 - (double)result/(double)cpu_area;
437 else if(cpu_area==0 && result!=0)
438 final_test_result = -1;
440 return final_test_result;