7 #include <opencv2/core/utility.hpp>
8 #include "opencv2/cuda.hpp"
9 #include "opencv2/highgui.hpp"
10 #include "opencv2/objdetect.hpp"
11 #include "opencv2/imgproc.hpp"
16 bool help_showed = false;
22 static Args read(int argc, char** argv);
43 bool hit_threshold_auto;
46 int win_stride_width, win_stride_height;
58 void handleKey(char key);
62 string hogWorkFps() const;
66 string workFps() const;
68 string message() const;
91 static void printHelp()
93 cout << "Histogram of Oriented Gradients descriptor and detector sample.\n"
94 << "\nUsage: hog_gpu\n"
95 << " (<image>|--video <vide>|--camera <camera_id>) # frames source\n"
96 << " [--make_gray <true/false>] # convert image to gray one or not\n"
97 << " [--resize_src <true/false>] # do resize of the source image or not\n"
98 << " [--width <int>] # resized image width\n"
99 << " [--height <int>] # resized image height\n"
100 << " [--hit_threshold <double>] # classifying plane distance threshold (0.0 usually)\n"
101 << " [--scale <double>] # HOG window scale factor\n"
102 << " [--nlevels <int>] # max number of HOG window scales\n"
103 << " [--win_width <int>] # width of the window (48 or 64)\n"
104 << " [--win_stride_width <int>] # distance by OX axis between neighbour wins\n"
105 << " [--win_stride_height <int>] # distance by OY axis between neighbour wins\n"
106 << " [--gr_threshold <int>] # merging similar rects constant\n"
107 << " [--gamma_correct <int>] # do gamma correction or not\n"
108 << " [--write_video <bool>] # write video or not\n"
109 << " [--dst_video <path>] # output video path\n"
110 << " [--dst_video_fps <double>] # output video fps\n";
114 int main(int argc, char** argv)
120 Args args = Args::read(argc, argv);
126 catch (const Exception& e) { return cout << "error: " << e.what() << endl, 1; }
127 catch (const exception& e) { return cout << "error: " << e.what() << endl, 1; }
128 catch(...) { return cout << "unknown exception" << endl, 1; }
135 src_is_video = false;
136 src_is_camera = false;
152 hit_threshold_auto = true;
155 win_stride_width = 8;
156 win_stride_height = 8;
162 Args Args::read(int argc, char** argv)
165 for (int i = 1; i < argc; i++)
167 if (string(argv[i]) == "--make_gray") args.make_gray = (string(argv[++i]) == "true");
168 else if (string(argv[i]) == "--resize_src") args.resize_src = (string(argv[++i]) == "true");
169 else if (string(argv[i]) == "--width") args.width = atoi(argv[++i]);
170 else if (string(argv[i]) == "--height") args.height = atoi(argv[++i]);
171 else if (string(argv[i]) == "--hit_threshold")
173 args.hit_threshold = atof(argv[++i]);
174 args.hit_threshold_auto = false;
176 else if (string(argv[i]) == "--scale") args.scale = atof(argv[++i]);
177 else if (string(argv[i]) == "--nlevels") args.nlevels = atoi(argv[++i]);
178 else if (string(argv[i]) == "--win_width") args.win_width = atoi(argv[++i]);
179 else if (string(argv[i]) == "--win_stride_width") args.win_stride_width = atoi(argv[++i]);
180 else if (string(argv[i]) == "--win_stride_height") args.win_stride_height = atoi(argv[++i]);
181 else if (string(argv[i]) == "--gr_threshold") args.gr_threshold = atoi(argv[++i]);
182 else if (string(argv[i]) == "--gamma_correct") args.gamma_corr = (string(argv[++i]) == "true");
183 else if (string(argv[i]) == "--write_video") args.write_video = (string(argv[++i]) == "true");
184 else if (string(argv[i]) == "--dst_video") args.dst_video = argv[++i];
185 else if (string(argv[i]) == "--dst_video_fps") args.dst_video_fps = atof(argv[++i]);
186 else if (string(argv[i]) == "--help") printHelp();
187 else if (string(argv[i]) == "--video") { args.src = argv[++i]; args.src_is_video = true; }
188 else if (string(argv[i]) == "--camera") { args.camera_id = atoi(argv[++i]); args.src_is_camera = true; }
189 else if (args.src.empty()) args.src = argv[i];
190 else throw runtime_error((string("unknown key: ") + argv[i]));
196 App::App(const Args& s)
198 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
201 cout << "\nControls:\n"
203 << "\tm - change mode GPU <-> CPU\n"
204 << "\tg - convert image to gray or not\n"
205 << "\t1/q - increase/decrease HOG scale\n"
206 << "\t2/w - increase/decrease levels count\n"
207 << "\t3/e - increase/decrease HOG group threshold\n"
208 << "\t4/r - increase/decrease hit threshold\n"
212 make_gray = args.make_gray;
214 gr_threshold = args.gr_threshold;
215 nlevels = args.nlevels;
217 if (args.hit_threshold_auto)
218 args.hit_threshold = args.win_width == 48 ? 1.4 : 0.;
219 hit_threshold = args.hit_threshold;
221 gamma_corr = args.gamma_corr;
223 if (args.win_width != 64 && args.win_width != 48)
226 cout << "Scale: " << scale << endl;
228 cout << "Resized source: (" << args.width << ", " << args.height << ")\n";
229 cout << "Group threshold: " << gr_threshold << endl;
230 cout << "Levels number: " << nlevels << endl;
231 cout << "Win width: " << args.win_width << endl;
232 cout << "Win stride: (" << args.win_stride_width << ", " << args.win_stride_height << ")\n";
233 cout << "Hit threshold: " << hit_threshold << endl;
234 cout << "Gamma correction: " << gamma_corr << endl;
242 cv::VideoWriter video_writer;
244 Size win_size(args.win_width, args.win_width * 2); //(64, 128) or (48, 96)
245 Size win_stride(args.win_stride_width, args.win_stride_height);
247 // Create HOG descriptors and detectors here
248 vector<float> detector;
249 if (win_size == Size(64, 128))
250 detector = cv::cuda::HOGDescriptor::getPeopleDetector64x128();
252 detector = cv::cuda::HOGDescriptor::getPeopleDetector48x96();
254 cv::cuda::HOGDescriptor gpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9,
255 cv::cuda::HOGDescriptor::DEFAULT_WIN_SIGMA, 0.2, gamma_corr,
256 cv::cuda::HOGDescriptor::DEFAULT_NLEVELS);
257 cv::HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9, 1, -1,
258 HOGDescriptor::L2Hys, 0.2, gamma_corr, cv::HOGDescriptor::DEFAULT_NLEVELS);
259 gpu_hog.setSVMDetector(detector);
260 cpu_hog.setSVMDetector(detector);
267 if (args.src_is_video)
269 vc.open(args.src.c_str());
271 throw runtime_error(string("can't open video file: " + args.src));
274 else if (args.src_is_camera)
276 vc.open(args.camera_id);
280 msg << "can't open camera: " << args.camera_id;
281 throw runtime_error(msg.str());
287 frame = imread(args.src);
289 throw runtime_error(string("can't open image file: " + args.src));
292 Mat img_aux, img, img_to_show;
293 cuda::GpuMat gpu_img;
295 // Iterate over all frames
296 while (running && !frame.empty())
300 // Change format of the image
301 if (make_gray) cvtColor(frame, img_aux, COLOR_BGR2GRAY);
302 else if (use_gpu) cvtColor(frame, img_aux, COLOR_BGR2BGRA);
303 else frame.copyTo(img_aux);
306 if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));
310 gpu_hog.nlevels = nlevels;
311 cpu_hog.nlevels = nlevels;
315 // Perform HOG classification
320 gpu_hog.detectMultiScale(gpu_img, found, hit_threshold, win_stride,
321 Size(0, 0), scale, gr_threshold);
323 else cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
324 Size(0, 0), scale, gr_threshold);
327 // Draw positive classified windows
328 for (size_t i = 0; i < found.size(); i++)
331 rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3);
335 putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
337 putText(img_to_show, "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
338 putText(img_to_show, "FPS (HOG only): " + hogWorkFps(), Point(5, 65), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
339 putText(img_to_show, "FPS (total): " + workFps(), Point(5, 105), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
340 imshow("opencv_gpu_hog", img_to_show);
342 if (args.src_is_video || args.src_is_camera) vc >> frame;
346 if (args.write_video)
348 if (!video_writer.isOpened())
350 video_writer.open(args.dst_video, VideoWriter::fourcc('x','v','i','d'), args.dst_video_fps,
351 img_to_show.size(), true);
352 if (!video_writer.isOpened())
353 throw std::runtime_error("can't create video writer");
356 if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR);
357 else cvtColor(img_to_show, img, COLOR_BGRA2BGR);
362 handleKey((char)waitKey(3));
368 void App::handleKey(char key)
378 cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
382 make_gray = !make_gray;
383 cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
387 cout << "Scale: " << scale << endl;
392 cout << "Scale: " << scale << endl;
396 cout << "Levels number: " << nlevels << endl;
400 nlevels = max(nlevels - 1, 1);
401 cout << "Levels number: " << nlevels << endl;
405 cout << "Group threshold: " << gr_threshold << endl;
409 gr_threshold = max(0, gr_threshold - 1);
410 cout << "Group threshold: " << gr_threshold << endl;
414 cout << "Hit threshold: " << hit_threshold << endl;
418 hit_threshold = max(0.0, hit_threshold - 0.25);
419 cout << "Hit threshold: " << hit_threshold << endl;
423 gamma_corr = !gamma_corr;
424 cout << "Gamma correction: " << gamma_corr << endl;
430 inline void App::hogWorkBegin() { hog_work_begin = getTickCount(); }
432 inline void App::hogWorkEnd()
434 int64 delta = getTickCount() - hog_work_begin;
435 double freq = getTickFrequency();
436 hog_work_fps = freq / delta;
439 inline string App::hogWorkFps() const
447 inline void App::workBegin() { work_begin = getTickCount(); }
449 inline void App::workEnd()
451 int64 delta = getTickCount() - work_begin;
452 double freq = getTickFrequency();
453 work_fps = freq / delta;
456 inline string App::workFps() const