1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <sstream>
5 #include <iomanip>
6 #include <stdexcept>
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"
12 
13 using namespace std;
14 using namespace cv;
15 
16 bool help_showed = false;
17 
18 class Args
19 {
20 public:
21     Args();
22     static Args read(int argc, char** argv);
23 
24     string src;
25     bool src_is_folder;
26     bool src_is_video;
27     bool src_is_camera;
28     int camera_id;
29 
30     bool svm_load;
31     string svm;
32 
33     bool write_video;
34     string dst_video;
35     double dst_video_fps;
36 
37     bool make_gray;
38 
39     bool resize_src;
40     int width, height;
41 
42     double scale;
43     int nlevels;
44     int gr_threshold;
45 
46     double hit_threshold;
47     bool hit_threshold_auto;
48 
49     int win_width;
50     int win_stride_width, win_stride_height;
51     int block_width;
52     int block_stride_width, block_stride_height;
53     int cell_width;
54     int nbins;
55 
56     bool gamma_corr;
57 };
58 
59 
60 class App
61 {
62 public:
63     App(const Args& s);
64     void run();
65 
66     void handleKey(char key);
67 
68     void hogWorkBegin();
69     void hogWorkEnd();
70     string hogWorkFps() const;
71 
72     void workBegin();
73     void workEnd();
74     string workFps() const;
75 
76     string message() const;
77 
78 private:
79     App operator=(App&);
80 
81     Args args;
82     bool running;
83 
84     bool use_gpu;
85     bool make_gray;
86     double scale;
87     int gr_threshold;
88     int nlevels;
89     double hit_threshold;
90     bool gamma_corr;
91 
92     int64 hog_work_begin;
93     double hog_work_fps;
94 
95     int64 work_begin;
96     double work_fps;
97 };
98 
printHelp()99 static void printHelp()
100 {
101     cout << "Histogram of Oriented Gradients descriptor and detector sample.\n"
102          << "\nUsage: hog\n"
103          << "  (<image>|--video <vide>|--camera <camera_id>) # frames source\n"
104          << "  or"
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";
127     help_showed = true;
128 }
129 
main(int argc,char ** argv)130 int main(int argc, char** argv)
131 {
132     try
133     {
134         Args args;
135         if (argc < 2)
136         {
137             printHelp();
138             args.camera_id = 0;
139             args.src_is_camera = true;
140         }
141         else
142         {
143             args = Args::read(argc, argv);
144             if (help_showed)
145                 return -1;
146         }
147         App app(args);
148         app.run();
149     }
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; }
153     return 0;
154 }
155 
156 
Args()157 Args::Args()
158 {
159     src_is_video = false;
160     src_is_camera = false;
161     src_is_folder = false;
162     svm_load = false;
163     camera_id = 0;
164 
165     write_video = false;
166     dst_video_fps = 24.;
167 
168     make_gray = false;
169 
170     resize_src = false;
171     width = 640;
172     height = 480;
173 
174     scale = 1.05;
175     nlevels = 13;
176     gr_threshold = 8;
177     hit_threshold = 1.4;
178     hit_threshold_auto = true;
179 
180     win_width = 48;
181     win_stride_width = 8;
182     win_stride_height = 8;
183     block_width = 16;
184     block_stride_width = 8;
185     block_stride_height = 8;
186     cell_width = 8;
187     nbins = 9;
188 
189     gamma_corr = true;
190 }
191 
192 
read(int argc,char ** argv)193 Args Args::read(int argc, char** argv)
194 {
195     Args args;
196     for (int i = 1; i < argc; i++)
197     {
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")
203         {
204             args.hit_threshold = atof(argv[++i]);
205             args.hit_threshold_auto = false;
206         }
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]));
229     }
230     return args;
231 }
232 
233 
App(const Args & s)234 App::App(const Args& s)
235 {
236     cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
237 
238     args = s;
239     cout << "\nControls:\n"
240          << "\tESC - exit\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"
247          << endl;
248 
249     use_gpu = true;
250     make_gray = args.make_gray;
251     scale = args.scale;
252     gr_threshold = args.gr_threshold;
253     nlevels = args.nlevels;
254 
255     if (args.hit_threshold_auto)
256         args.hit_threshold = args.win_width == 48 ? 1.4 : 0.;
257     hit_threshold = args.hit_threshold;
258 
259     gamma_corr = args.gamma_corr;
260 
261     cout << "Scale: " << scale << endl;
262     if (args.resize_src)
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;
274     cout << endl;
275 }
276 
277 
run()278 void App::run()
279 {
280     running = true;
281     cv::VideoWriter video_writer;
282 
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);
288 
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);
291 
292     if(args.svm_load) {
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;
298         } else {
299             const std::string what =
300                     "could not load model for hog classifier from file: "
301                     + model_file_name;
302             throw std::runtime_error(what);
303         }
304 
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"
309                     + model_file_name;
310             throw std::runtime_error(what);
311         }
312 
313         gpu_hog->setSVMDetector(svm_model);
314         cpu_hog.setSVMDetector(svm_model);
315     } else {
316         // Create HOG descriptors and detectors here
317         Mat detector = gpu_hog->getDefaultPeopleDetector();
318 
319         gpu_hog->setSVMDetector(detector);
320         cpu_hog.setSVMDetector(detector);
321     }
322 
323     cout << "gpusvmDescriptorSize : " << gpu_hog->getDescriptorSize()
324          << endl;
325     cout << "cpusvmDescriptorSize : " << cpu_hog.getDescriptorSize()
326          << endl;
327 
328     while (running)
329     {
330         VideoCapture vc;
331         Mat frame;
332         vector<String> filenames;
333 
334         unsigned int count = 1;
335 
336         if (args.src_is_video)
337         {
338             vc.open(args.src.c_str());
339             if (!vc.isOpened())
340                 throw runtime_error(string("can't open video file: " + args.src));
341             vc >> frame;
342         }
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
348             if (!frame.data)
349                 cerr << "Problem loading image from folder!!!" << endl;
350         }
351         else if (args.src_is_camera)
352         {
353             vc.open(args.camera_id);
354             if (!vc.isOpened())
355             {
356                 stringstream msg;
357                 msg << "can't open camera: " << args.camera_id;
358                 throw runtime_error(msg.str());
359             }
360             vc >> frame;
361         }
362         else
363         {
364             frame = imread(args.src);
365             if (frame.empty())
366                 throw runtime_error(string("can't open image file: " + args.src));
367         }
368 
369         Mat img_aux, img, img_to_show;
370         cuda::GpuMat gpu_img;
371 
372         // Iterate over all frames
373         while (running && !frame.empty())
374         {
375             workBegin();
376 
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);
381 
382             // Resize image
383             if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));
384             else img = img_aux;
385             img_to_show = img;
386 
387             vector<Rect> found;
388 
389             // Perform HOG classification
390             hogWorkBegin();
391             if (use_gpu)
392             {
393                 gpu_img.upload(img);
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);
400             }
401             else
402             {
403                 cpu_hog.nlevels = nlevels;
404                 cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
405                                          Size(0, 0), scale, gr_threshold);
406             }
407             hogWorkEnd();
408 
409             // Draw positive classified windows
410             for (size_t i = 0; i < found.size(); i++)
411             {
412                 Rect r = found[i];
413                 rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3);
414             }
415 
416             if (use_gpu)
417                 putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
418             else
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);
423 
424             if (args.src_is_video || args.src_is_camera) vc >> frame;
425             if (args.src_is_folder) {
426                 count++;
427                 if (count < filenames.size()) {
428                     frame = imread(filenames[count]);
429                 } else {
430                     Mat empty;
431                     frame = empty;
432                 }
433             }
434 
435             workEnd();
436 
437             if (args.write_video)
438             {
439                 if (!video_writer.isOpened())
440                 {
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");
445                 }
446 
447                 if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR);
448                 else cvtColor(img_to_show, img, COLOR_BGRA2BGR);
449 
450                 video_writer << img;
451             }
452 
453             handleKey((char)waitKey(3));
454         }
455     }
456 }
457 
458 
handleKey(char key)459 void App::handleKey(char key)
460 {
461     switch (key)
462     {
463     case 27:
464         running = false;
465         break;
466     case 'm':
467     case 'M':
468         use_gpu = !use_gpu;
469         cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
470         break;
471     case 'g':
472     case 'G':
473         make_gray = !make_gray;
474         cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
475         break;
476     case '1':
477         scale *= 1.05;
478         cout << "Scale: " << scale << endl;
479         break;
480     case 'q':
481     case 'Q':
482         scale /= 1.05;
483         cout << "Scale: " << scale << endl;
484         break;
485     case '2':
486         nlevels++;
487         cout << "Levels number: " << nlevels << endl;
488         break;
489     case 'w':
490     case 'W':
491         nlevels = max(nlevels - 1, 1);
492         cout << "Levels number: " << nlevels << endl;
493         break;
494     case '3':
495         gr_threshold++;
496         cout << "Group threshold: " << gr_threshold << endl;
497         break;
498     case 'e':
499     case 'E':
500         gr_threshold = max(0, gr_threshold - 1);
501         cout << "Group threshold: " << gr_threshold << endl;
502         break;
503     case '4':
504         hit_threshold+=0.25;
505         cout << "Hit threshold: " << hit_threshold << endl;
506         break;
507     case 'r':
508     case 'R':
509         hit_threshold = max(0.0, hit_threshold - 0.25);
510         cout << "Hit threshold: " << hit_threshold << endl;
511         break;
512     case 'c':
513     case 'C':
514         gamma_corr = !gamma_corr;
515         cout << "Gamma correction: " << gamma_corr << endl;
516         break;
517     }
518 }
519 
520 
hogWorkBegin()521 inline void App::hogWorkBegin() { hog_work_begin = getTickCount(); }
522 
hogWorkEnd()523 inline void App::hogWorkEnd()
524 {
525     int64 delta = getTickCount() - hog_work_begin;
526     double freq = getTickFrequency();
527     hog_work_fps = freq / delta;
528 }
529 
hogWorkFps() const530 inline string App::hogWorkFps() const
531 {
532     stringstream ss;
533     ss << hog_work_fps;
534     return ss.str();
535 }
536 
537 
workBegin()538 inline void App::workBegin() { work_begin = getTickCount(); }
539 
workEnd()540 inline void App::workEnd()
541 {
542     int64 delta = getTickCount() - work_begin;
543     double freq = getTickFrequency();
544     work_fps = freq / delta;
545 }
546 
workFps() const547 inline string App::workFps() const
548 {
549     stringstream ss;
550     ss << work_fps;
551     return ss.str();
552 }
553