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);
47 bool hit_threshold_auto;
50 int win_stride_width, win_stride_height;
52 int block_stride_width, block_stride_height;
66 void handleKey(char key);
70 string hogWorkFps() const;
74 string workFps() const;
76 string message() const;
99 static void printHelp()
101 cout << "Histogram of Oriented Gradients descriptor and detector sample.\n"
102 << "\nUsage: hog_gpu\n"
103 << " (<image>|--video <vide>|--camera <camera_id>) # frames source\n"
105 << " (--folder <folder_path>) # load images from folder\n"
106 << " [--svm <file> # load svm file"
107 << " [--make_gray <true/false>] # convert image to gray one or not\n"
108 << " [--resize_src <true/false>] # do resize of the source image or not\n"
109 << " [--width <int>] # resized image width\n"
110 << " [--height <int>] # resized image height\n"
111 << " [--hit_threshold <double>] # classifying plane distance threshold (0.0 usually)\n"
112 << " [--scale <double>] # HOG window scale factor\n"
113 << " [--nlevels <int>] # max number of HOG window scales\n"
114 << " [--win_width <int>] # width of the window\n"
115 << " [--win_stride_width <int>] # distance by OX axis between neighbour wins\n"
116 << " [--win_stride_height <int>] # distance by OY axis between neighbour wins\n"
117 << " [--block_width <int>] # width of the block\n"
118 << " [--block_stride_width <int>] # distance by 0X axis between neighbour blocks\n"
119 << " [--block_stride_height <int>] # distance by 0Y axis between neighbour blocks\n"
120 << " [--cell_width <int>] # width of the cell\n"
121 << " [--nbins <int>] # number of bins\n"
122 << " [--gr_threshold <int>] # merging similar rects constant\n"
123 << " [--gamma_correct <int>] # do gamma correction or not\n"
124 << " [--write_video <bool>] # write video or not\n"
125 << " [--dst_video <path>] # output video path\n"
126 << " [--dst_video_fps <double>] # output video fps\n";
130 int main(int argc, char** argv)
139 args.src_is_camera = true;
143 args = Args::read(argc, argv);
150 catch (const Exception& e) { return cout << "error: " << e.what() << endl, 1; }
151 catch (const exception& e) { return cout << "error: " << e.what() << endl, 1; }
152 catch(...) { return cout << "unknown exception" << endl, 1; }
159 src_is_video = false;
160 src_is_camera = false;
161 src_is_folder = false;
178 hit_threshold_auto = true;
181 win_stride_width = 8;
182 win_stride_height = 8;
184 block_stride_width = 8;
185 block_stride_height = 8;
193 Args Args::read(int argc, char** argv)
196 for (int i = 1; i < argc; i++)
198 if (string(argv[i]) == "--make_gray") args.make_gray = (string(argv[++i]) == "true");
199 else if (string(argv[i]) == "--resize_src") args.resize_src = (string(argv[++i]) == "true");
200 else if (string(argv[i]) == "--width") args.width = atoi(argv[++i]);
201 else if (string(argv[i]) == "--height") args.height = atoi(argv[++i]);
202 else if (string(argv[i]) == "--hit_threshold")
204 args.hit_threshold = atof(argv[++i]);
205 args.hit_threshold_auto = false;
207 else if (string(argv[i]) == "--scale") args.scale = atof(argv[++i]);
208 else if (string(argv[i]) == "--nlevels") args.nlevels = atoi(argv[++i]);
209 else if (string(argv[i]) == "--win_width") args.win_width = atoi(argv[++i]);
210 else if (string(argv[i]) == "--win_stride_width") args.win_stride_width = atoi(argv[++i]);
211 else if (string(argv[i]) == "--win_stride_height") args.win_stride_height = atoi(argv[++i]);
212 else if (string(argv[i]) == "--block_width") args.block_width = atoi(argv[++i]);
213 else if (string(argv[i]) == "--block_stride_width") args.block_stride_width = atoi(argv[++i]);
214 else if (string(argv[i]) == "--block_stride_height") args.block_stride_height = atoi(argv[++i]);
215 else if (string(argv[i]) == "--cell_width") args.cell_width = atoi(argv[++i]);
216 else if (string(argv[i]) == "--nbins") args.nbins = atoi(argv[++i]);
217 else if (string(argv[i]) == "--gr_threshold") args.gr_threshold = atoi(argv[++i]);
218 else if (string(argv[i]) == "--gamma_correct") args.gamma_corr = (string(argv[++i]) == "true");
219 else if (string(argv[i]) == "--write_video") args.write_video = (string(argv[++i]) == "true");
220 else if (string(argv[i]) == "--dst_video") args.dst_video = argv[++i];
221 else if (string(argv[i]) == "--dst_video_fps") args.dst_video_fps = atof(argv[++i]);
222 else if (string(argv[i]) == "--help") printHelp();
223 else if (string(argv[i]) == "--video") { args.src = argv[++i]; args.src_is_video = true; }
224 else if (string(argv[i]) == "--camera") { args.camera_id = atoi(argv[++i]); args.src_is_camera = true; }
225 else if (string(argv[i]) == "--folder") { args.src = argv[++i]; args.src_is_folder = true;}
226 else if (string(argv[i]) == "--svm") { args.svm = argv[++i]; args.svm_load = true;}
227 else if (args.src.empty()) args.src = argv[i];
228 else throw runtime_error((string("unknown key: ") + argv[i]));
234 App::App(const Args& s)
236 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
239 cout << "\nControls:\n"
241 << "\tm - change mode GPU <-> CPU\n"
242 << "\tg - convert image to gray or not\n"
243 << "\t1/q - increase/decrease HOG scale\n"
244 << "\t2/w - increase/decrease levels count\n"
245 << "\t3/e - increase/decrease HOG group threshold\n"
246 << "\t4/r - increase/decrease hit threshold\n"
250 make_gray = args.make_gray;
252 gr_threshold = args.gr_threshold;
253 nlevels = args.nlevels;
255 if (args.hit_threshold_auto)
256 args.hit_threshold = args.win_width == 48 ? 1.4 : 0.;
257 hit_threshold = args.hit_threshold;
259 gamma_corr = args.gamma_corr;
261 cout << "Scale: " << scale << endl;
263 cout << "Resized source: (" << args.width << ", " << args.height << ")\n";
264 cout << "Group threshold: " << gr_threshold << endl;
265 cout << "Levels number: " << nlevels << endl;
266 cout << "Win size: (" << args.win_width << ", " << args.win_width*2 << ")\n";
267 cout << "Win stride: (" << args.win_stride_width << ", " << args.win_stride_height << ")\n";
268 cout << "Block size: (" << args.block_width << ", " << args.block_width << ")\n";
269 cout << "Block stride: (" << args.block_stride_width << ", " << args.block_stride_height << ")\n";
270 cout << "Cell size: (" << args.cell_width << ", " << args.cell_width << ")\n";
271 cout << "Bins number: " << args.nbins << endl;
272 cout << "Hit threshold: " << hit_threshold << endl;
273 cout << "Gamma correction: " << gamma_corr << endl;
281 cv::VideoWriter video_writer;
283 Size win_stride(args.win_stride_width, args.win_stride_height);
284 Size win_size(args.win_width, args.win_width * 2);
285 Size block_size(args.block_width, args.block_width);
286 Size block_stride(args.block_stride_width, args.block_stride_height);
287 Size cell_size(args.cell_width, args.cell_width);
289 cv::Ptr<cv::cuda::HOG> gpu_hog = cv::cuda::HOG::create(win_size, block_size, block_stride, cell_size, args.nbins);
290 cv::HOGDescriptor cpu_hog(win_size, block_size, block_stride, cell_size, args.nbins);
293 std::vector<float> svm_model;
294 const std::string model_file_name = args.svm;
295 FileStorage ifs(model_file_name, FileStorage::READ);
296 if (ifs.isOpened()) {
297 ifs["svm_detector"] >> svm_model;
299 const std::string what =
300 "could not load model for hog classifier from file: "
302 throw std::runtime_error(what);
305 // check if the variables are initialized
306 if (svm_model.empty()) {
307 const std::string what =
308 "HoG classifier: svm model could not be loaded from file"
310 throw std::runtime_error(what);
313 gpu_hog->setSVMDetector(svm_model);
314 cpu_hog.setSVMDetector(svm_model);
316 // Create HOG descriptors and detectors here
317 Mat detector = gpu_hog->getDefaultPeopleDetector();
319 gpu_hog->setSVMDetector(detector);
320 cpu_hog.setSVMDetector(detector);
323 cout << "gpusvmDescriptorSize : " << gpu_hog->getDescriptorSize()
325 cout << "cpusvmDescriptorSize : " << cpu_hog.getDescriptorSize()
332 vector<String> filenames;
334 unsigned int count = 1;
336 if (args.src_is_video)
338 vc.open(args.src.c_str());
340 throw runtime_error(string("can't open video file: " + args.src));
343 else if (args.src_is_folder) {
344 String folder = args.src;
345 cout << folder << endl;
346 glob(folder, filenames);
347 frame = imread(filenames[count]); // 0 --> .gitignore
349 cerr << "Problem loading image from folder!!!" << endl;
351 else if (args.src_is_camera)
353 vc.open(args.camera_id);
357 msg << "can't open camera: " << args.camera_id;
358 throw runtime_error(msg.str());
364 frame = imread(args.src);
366 throw runtime_error(string("can't open image file: " + args.src));
369 Mat img_aux, img, img_to_show;
370 cuda::GpuMat gpu_img;
372 // Iterate over all frames
373 while (running && !frame.empty())
377 // Change format of the image
378 if (make_gray) cvtColor(frame, img_aux, COLOR_BGR2GRAY);
379 else if (use_gpu) cvtColor(frame, img_aux, COLOR_BGR2BGRA);
380 else frame.copyTo(img_aux);
383 if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));
389 // Perform HOG classification
394 gpu_hog->setNumLevels(nlevels);
395 gpu_hog->setHitThreshold(hit_threshold);
396 gpu_hog->setWinStride(win_stride);
397 gpu_hog->setScaleFactor(scale);
398 gpu_hog->setGroupThreshold(gr_threshold);
399 gpu_hog->detectMultiScale(gpu_img, found);
403 cpu_hog.nlevels = nlevels;
404 cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
405 Size(0, 0), scale, gr_threshold);
409 // Draw positive classified windows
410 for (size_t i = 0; i < found.size(); i++)
413 rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3);
417 putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
419 putText(img_to_show, "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
420 putText(img_to_show, "FPS HOG: " + hogWorkFps(), Point(5, 65), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
421 putText(img_to_show, "FPS total: " + workFps(), Point(5, 105), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
422 imshow("opencv_gpu_hog", img_to_show);
424 if (args.src_is_video || args.src_is_camera) vc >> frame;
425 if (args.src_is_folder) {
427 if (count < filenames.size()) {
428 frame = imread(filenames[count]);
437 if (args.write_video)
439 if (!video_writer.isOpened())
441 video_writer.open(args.dst_video, VideoWriter::fourcc('x','v','i','d'), args.dst_video_fps,
442 img_to_show.size(), true);
443 if (!video_writer.isOpened())
444 throw std::runtime_error("can't create video writer");
447 if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR);
448 else cvtColor(img_to_show, img, COLOR_BGRA2BGR);
453 handleKey((char)waitKey(3));
459 void App::handleKey(char key)
469 cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
473 make_gray = !make_gray;
474 cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
478 cout << "Scale: " << scale << endl;
483 cout << "Scale: " << scale << endl;
487 cout << "Levels number: " << nlevels << endl;
491 nlevels = max(nlevels - 1, 1);
492 cout << "Levels number: " << nlevels << endl;
496 cout << "Group threshold: " << gr_threshold << endl;
500 gr_threshold = max(0, gr_threshold - 1);
501 cout << "Group threshold: " << gr_threshold << endl;
505 cout << "Hit threshold: " << hit_threshold << endl;
509 hit_threshold = max(0.0, hit_threshold - 0.25);
510 cout << "Hit threshold: " << hit_threshold << endl;
514 gamma_corr = !gamma_corr;
515 cout << "Gamma correction: " << gamma_corr << endl;
521 inline void App::hogWorkBegin() { hog_work_begin = getTickCount(); }
523 inline void App::hogWorkEnd()
525 int64 delta = getTickCount() - hog_work_begin;
526 double freq = getTickFrequency();
527 hog_work_fps = freq / delta;
530 inline string App::hogWorkFps() const
538 inline void App::workBegin() { work_begin = getTickCount(); }
540 inline void App::workEnd()
542 int64 delta = getTickCount() - work_begin;
543 double freq = getTickFrequency();
544 work_fps = freq / delta;
547 inline string App::workFps() const