7 #include <opencv2/core/utility.hpp>
8 #include "opencv2/cudaobjdetect.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)
123 args.src_is_camera = true;
127 args = Args::read(argc, argv);
134 catch (const Exception& e) { return cout << "error: " << e.what() << endl, 1; }
135 catch (const exception& e) { return cout << "error: " << e.what() << endl, 1; }
136 catch(...) { return cout << "unknown exception" << endl, 1; }
143 src_is_video = false;
144 src_is_camera = false;
160 hit_threshold_auto = true;
163 win_stride_width = 8;
164 win_stride_height = 8;
170 Args Args::read(int argc, char** argv)
173 for (int i = 1; i < argc; i++)
175 if (string(argv[i]) == "--make_gray") args.make_gray = (string(argv[++i]) == "true");
176 else if (string(argv[i]) == "--resize_src") args.resize_src = (string(argv[++i]) == "true");
177 else if (string(argv[i]) == "--width") args.width = atoi(argv[++i]);
178 else if (string(argv[i]) == "--height") args.height = atoi(argv[++i]);
179 else if (string(argv[i]) == "--hit_threshold")
181 args.hit_threshold = atof(argv[++i]);
182 args.hit_threshold_auto = false;
184 else if (string(argv[i]) == "--scale") args.scale = atof(argv[++i]);
185 else if (string(argv[i]) == "--nlevels") args.nlevels = atoi(argv[++i]);
186 else if (string(argv[i]) == "--win_width") args.win_width = atoi(argv[++i]);
187 else if (string(argv[i]) == "--win_stride_width") args.win_stride_width = atoi(argv[++i]);
188 else if (string(argv[i]) == "--win_stride_height") args.win_stride_height = atoi(argv[++i]);
189 else if (string(argv[i]) == "--gr_threshold") args.gr_threshold = atoi(argv[++i]);
190 else if (string(argv[i]) == "--gamma_correct") args.gamma_corr = (string(argv[++i]) == "true");
191 else if (string(argv[i]) == "--write_video") args.write_video = (string(argv[++i]) == "true");
192 else if (string(argv[i]) == "--dst_video") args.dst_video = argv[++i];
193 else if (string(argv[i]) == "--dst_video_fps") args.dst_video_fps = atof(argv[++i]);
194 else if (string(argv[i]) == "--help") printHelp();
195 else if (string(argv[i]) == "--video") { args.src = argv[++i]; args.src_is_video = true; }
196 else if (string(argv[i]) == "--camera") { args.camera_id = atoi(argv[++i]); args.src_is_camera = true; }
197 else if (args.src.empty()) args.src = argv[i];
198 else throw runtime_error((string("unknown key: ") + argv[i]));
204 App::App(const Args& s)
206 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
209 cout << "\nControls:\n"
211 << "\tm - change mode GPU <-> CPU\n"
212 << "\tg - convert image to gray or not\n"
213 << "\t1/q - increase/decrease HOG scale\n"
214 << "\t2/w - increase/decrease levels count\n"
215 << "\t3/e - increase/decrease HOG group threshold\n"
216 << "\t4/r - increase/decrease hit threshold\n"
220 make_gray = args.make_gray;
222 gr_threshold = args.gr_threshold;
223 nlevels = args.nlevels;
225 if (args.hit_threshold_auto)
226 args.hit_threshold = args.win_width == 48 ? 1.4 : 0.;
227 hit_threshold = args.hit_threshold;
229 gamma_corr = args.gamma_corr;
231 if (args.win_width != 64 && args.win_width != 48)
234 cout << "Scale: " << scale << endl;
236 cout << "Resized source: (" << args.width << ", " << args.height << ")\n";
237 cout << "Group threshold: " << gr_threshold << endl;
238 cout << "Levels number: " << nlevels << endl;
239 cout << "Win width: " << args.win_width << endl;
240 cout << "Win stride: (" << args.win_stride_width << ", " << args.win_stride_height << ")\n";
241 cout << "Hit threshold: " << hit_threshold << endl;
242 cout << "Gamma correction: " << gamma_corr << endl;
250 cv::VideoWriter video_writer;
252 Size win_size(args.win_width, args.win_width * 2); //(64, 128) or (48, 96)
253 Size win_stride(args.win_stride_width, args.win_stride_height);
255 cv::Ptr<cv::cuda::HOG> gpu_hog = cv::cuda::HOG::create(win_size);
256 cv::HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9);
258 // Create HOG descriptors and detectors here
259 Mat detector = gpu_hog->getDefaultPeopleDetector();
261 gpu_hog->setSVMDetector(detector);
262 cpu_hog.setSVMDetector(detector);
269 if (args.src_is_video)
271 vc.open(args.src.c_str());
273 throw runtime_error(string("can't open video file: " + args.src));
276 else if (args.src_is_camera)
278 vc.open(args.camera_id);
282 msg << "can't open camera: " << args.camera_id;
283 throw runtime_error(msg.str());
289 frame = imread(args.src);
291 throw runtime_error(string("can't open image file: " + args.src));
294 Mat img_aux, img, img_to_show;
295 cuda::GpuMat gpu_img;
297 // Iterate over all frames
298 while (running && !frame.empty())
302 // Change format of the image
303 if (make_gray) cvtColor(frame, img_aux, COLOR_BGR2GRAY);
304 else if (use_gpu) cvtColor(frame, img_aux, COLOR_BGR2BGRA);
305 else frame.copyTo(img_aux);
308 if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));
314 // Perform HOG classification
319 gpu_hog->setNumLevels(nlevels);
320 gpu_hog->setHitThreshold(hit_threshold);
321 gpu_hog->setWinStride(win_stride);
322 gpu_hog->setScaleFactor(scale);
323 gpu_hog->setGroupThreshold(gr_threshold);
324 gpu_hog->detectMultiScale(gpu_img, found);
328 cpu_hog.nlevels = nlevels;
329 cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
330 Size(0, 0), scale, gr_threshold);
334 // Draw positive classified windows
335 for (size_t i = 0; i < found.size(); i++)
338 rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3);
342 putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
344 putText(img_to_show, "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
345 putText(img_to_show, "FPS (HOG only): " + hogWorkFps(), Point(5, 65), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
346 putText(img_to_show, "FPS (total): " + workFps(), Point(5, 105), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
347 imshow("opencv_gpu_hog", img_to_show);
349 if (args.src_is_video || args.src_is_camera) vc >> frame;
353 if (args.write_video)
355 if (!video_writer.isOpened())
357 video_writer.open(args.dst_video, VideoWriter::fourcc('x','v','i','d'), args.dst_video_fps,
358 img_to_show.size(), true);
359 if (!video_writer.isOpened())
360 throw std::runtime_error("can't create video writer");
363 if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR);
364 else cvtColor(img_to_show, img, COLOR_BGRA2BGR);
369 handleKey((char)waitKey(3));
375 void App::handleKey(char key)
385 cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
389 make_gray = !make_gray;
390 cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
394 cout << "Scale: " << scale << endl;
399 cout << "Scale: " << scale << endl;
403 cout << "Levels number: " << nlevels << endl;
407 nlevels = max(nlevels - 1, 1);
408 cout << "Levels number: " << nlevels << endl;
412 cout << "Group threshold: " << gr_threshold << endl;
416 gr_threshold = max(0, gr_threshold - 1);
417 cout << "Group threshold: " << gr_threshold << endl;
421 cout << "Hit threshold: " << hit_threshold << endl;
425 hit_threshold = max(0.0, hit_threshold - 0.25);
426 cout << "Hit threshold: " << hit_threshold << endl;
430 gamma_corr = !gamma_corr;
431 cout << "Gamma correction: " << gamma_corr << endl;
437 inline void App::hogWorkBegin() { hog_work_begin = getTickCount(); }
439 inline void App::hogWorkEnd()
441 int64 delta = getTickCount() - hog_work_begin;
442 double freq = getTickFrequency();
443 hog_work_fps = freq / delta;
446 inline string App::hogWorkFps() const
454 inline void App::workBegin() { work_begin = getTickCount(); }
456 inline void App::workEnd()
458 int64 delta = getTickCount() - work_begin;
459 double freq = getTickFrequency();
460 work_fps = freq / delta;
463 inline string App::workFps() const