|
1 |
| -#include <iostream> |
2 |
| -#include <stdexcept> |
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html |
| 4 | + |
3 | 5 | #include <opencv2/objdetect.hpp>
|
4 | 6 | #include <opencv2/highgui.hpp>
|
5 | 7 | #include <opencv2/imgproc.hpp>
|
6 |
| -#include <opencv2/imgcodecs.hpp> |
7 |
| -#include <opencv2/video.hpp> |
8 | 8 | #include <opencv2/videoio.hpp>
|
| 9 | +#include <iostream> |
| 10 | +#include <iomanip> |
9 | 11 |
|
10 | 12 | using namespace cv;
|
11 | 13 | using namespace std;
|
12 | 14 |
|
13 |
| - |
14 |
| -const char* keys = |
| 15 | +class Detector |
15 | 16 | {
|
16 |
| - "{ help h | | print help message }" |
17 |
| - "{ image i | | specify input image}" |
18 |
| - "{ camera c | | enable camera capturing }" |
19 |
| - "{ video v | ../data/vtest.avi | use video as input }" |
20 |
| - "{ directory d | | images directory}" |
21 |
| -}; |
22 |
| - |
23 |
| -static void detectAndDraw(const HOGDescriptor &hog, Mat &img) |
24 |
| -{ |
25 |
| - vector<Rect> found, found_filtered; |
26 |
| - double t = (double) getTickCount(); |
27 |
| - // Run the detector with default parameters. to get a higher hit-rate |
28 |
| - // (and more false alarms, respectively), decrease the hitThreshold and |
29 |
| - // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). |
30 |
| - hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); |
31 |
| - t = (double) getTickCount() - t; |
32 |
| - cout << "detection time = " << (t*1000./cv::getTickFrequency()) << " ms" << endl; |
33 |
| - |
34 |
| - for(size_t i = 0; i < found.size(); i++ ) |
| 17 | + enum Mode { Default, Daimler } m; |
| 18 | + HOGDescriptor hog, hog_d; |
| 19 | +public: |
| 20 | + Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9) |
35 | 21 | {
|
36 |
| - Rect r = found[i]; |
37 |
| - |
38 |
| - size_t j; |
39 |
| - // Do not add small detections inside a bigger detection. |
40 |
| - for ( j = 0; j < found.size(); j++ ) |
41 |
| - if ( j != i && (r & found[j]) == r ) |
42 |
| - break; |
43 |
| - |
44 |
| - if ( j == found.size() ) |
45 |
| - found_filtered.push_back(r); |
| 22 | + hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); |
| 23 | + hog_d.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector()); |
46 | 24 | }
|
47 |
| - |
48 |
| - for (size_t i = 0; i < found_filtered.size(); i++) |
| 25 | + void toggleMode() { m = (m == Default ? Daimler : Default); } |
| 26 | + string modeName() const { return (m == Default ? "Default" : "Daimler"); } |
| 27 | + vector<Rect> detect(InputArray img) |
| 28 | + { |
| 29 | + // Run the detector with default parameters. to get a higher hit-rate |
| 30 | + // (and more false alarms, respectively), decrease the hitThreshold and |
| 31 | + // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). |
| 32 | + vector<Rect> found; |
| 33 | + if (m == Default) |
| 34 | + hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2, false); |
| 35 | + else if (m == Daimler) |
| 36 | + hog_d.detectMultiScale(img, found, 0.5, Size(8,8), Size(32,32), 1.05, 2, true); |
| 37 | + return found; |
| 38 | + } |
| 39 | + void adjustRect(Rect & r) const |
49 | 40 | {
|
50 |
| - Rect r = found_filtered[i]; |
51 |
| - |
52 | 41 | // The HOG detector returns slightly larger rectangles than the real objects,
|
53 | 42 | // so we slightly shrink the rectangles to get a nicer output.
|
54 | 43 | r.x += cvRound(r.width*0.1);
|
55 | 44 | r.width = cvRound(r.width*0.8);
|
56 | 45 | r.y += cvRound(r.height*0.07);
|
57 | 46 | r.height = cvRound(r.height*0.8);
|
58 |
| - rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); |
59 | 47 | }
|
60 |
| -} |
| 48 | +}; |
| 49 | + |
| 50 | +static const string keys = "{ help h | | print help message }" |
| 51 | + "{ camera c | 0 | capture video from camera (device index starting from 0) }" |
| 52 | + "{ video v | | use video as input }"; |
61 | 53 |
|
62 | 54 | int main(int argc, char** argv)
|
63 | 55 | {
|
64 | 56 | CommandLineParser parser(argc, argv, keys);
|
65 |
| - |
| 57 | + parser.about("This sample demonstrates the use ot the HoG descriptor."); |
66 | 58 | if (parser.has("help"))
|
67 | 59 | {
|
68 |
| - cout << "\nThis program demonstrates the use of the HoG descriptor using\n" |
69 |
| - " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"; |
70 | 60 | parser.printMessage();
|
71 |
| - cout << "During execution:\n\tHit q or ESC key to quit.\n" |
72 |
| - "\tUsing OpenCV version " << CV_VERSION << "\n" |
73 |
| - "Note: camera device number must be different from -1.\n" << endl; |
74 | 61 | return 0;
|
75 | 62 | }
|
76 |
| - |
77 |
| - HOGDescriptor hog; |
78 |
| - hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); |
79 |
| - namedWindow("people detector", 1); |
80 |
| - |
81 |
| - string pattern_glob = ""; |
82 |
| - string video_filename = "../data/vtest.avi"; |
83 |
| - int camera_id = -1; |
84 |
| - if (parser.has("directory")) |
| 63 | + int camera = parser.get<int>("camera"); |
| 64 | + string file = parser.get<string>("video"); |
| 65 | + if (!parser.check()) |
85 | 66 | {
|
86 |
| - pattern_glob = parser.get<string>("directory"); |
| 67 | + parser.printErrors(); |
| 68 | + return 1; |
87 | 69 | }
|
88 |
| - else if (parser.has("image")) |
89 |
| - { |
90 |
| - pattern_glob = parser.get<string>("image"); |
91 |
| - } |
92 |
| - else if (parser.has("camera")) |
93 |
| - { |
94 |
| - camera_id = parser.get<int>("camera"); |
95 |
| - } |
96 |
| - else if (parser.has("video")) |
| 70 | + |
| 71 | + VideoCapture cap; |
| 72 | + if (file.empty()) |
| 73 | + cap.open(camera); |
| 74 | + else |
| 75 | + cap.open(file.c_str()); |
| 76 | + if (!cap.isOpened()) |
97 | 77 | {
|
98 |
| - video_filename = parser.get<string>("video"); |
| 78 | + cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl; |
| 79 | + return 2; |
99 | 80 | }
|
100 | 81 |
|
101 |
| - if (!pattern_glob.empty() || camera_id != -1 || !video_filename.empty()) |
| 82 | + cout << "Press 'q' or <ESC> to quit." << endl; |
| 83 | + cout << "Press <space> to toggle between Default and Daimler detector" << endl; |
| 84 | + Detector detector; |
| 85 | + Mat frame; |
| 86 | + for (;;) |
102 | 87 | {
|
103 |
| - //Read from input image files |
104 |
| - vector<String> filenames; |
105 |
| - //Read from video file |
106 |
| - VideoCapture vc; |
107 |
| - Mat frame; |
108 |
| - |
109 |
| - if (!pattern_glob.empty()) |
| 88 | + cap >> frame; |
| 89 | + if (frame.empty()) |
110 | 90 | {
|
111 |
| - String folder(pattern_glob); |
112 |
| - glob(folder, filenames); |
| 91 | + cout << "Finished reading: empty frame" << endl; |
| 92 | + break; |
113 | 93 | }
|
114 |
| - else if (camera_id != -1) |
| 94 | + int64 t = getTickCount(); |
| 95 | + vector<Rect> found = detector.detect(frame); |
| 96 | + t = getTickCount() - t; |
| 97 | + |
| 98 | + // show the window |
115 | 99 | {
|
116 |
| - vc.open(camera_id); |
117 |
| - if (!vc.isOpened()) |
118 |
| - { |
119 |
| - stringstream msg; |
120 |
| - msg << "can't open camera: " << camera_id; |
121 |
| - throw runtime_error(msg.str()); |
122 |
| - } |
| 100 | + ostringstream buf; |
| 101 | + buf << "Mode: " << detector.modeName() << " ||| " |
| 102 | + << "FPS: " << fixed << setprecision(1) << (getTickFrequency() / (double)t); |
| 103 | + putText(frame, buf.str(), Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255), 2, LINE_AA); |
123 | 104 | }
|
124 |
| - else |
| 105 | + for (vector<Rect>::iterator i = found.begin(); i != found.end(); ++i) |
125 | 106 | {
|
126 |
| - vc.open(video_filename.c_str()); |
127 |
| - if (!vc.isOpened()) |
128 |
| - throw runtime_error(string("can't open video file: " + video_filename)); |
| 107 | + Rect &r = *i; |
| 108 | + detector.adjustRect(r); |
| 109 | + rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 2); |
129 | 110 | }
|
| 111 | + imshow("People detector", frame); |
130 | 112 |
|
131 |
| - vector<String>::const_iterator it_image = filenames.begin(); |
132 |
| - |
133 |
| - for (;;) |
| 113 | + // interact with user |
| 114 | + const char key = (char)waitKey(30); |
| 115 | + if (key == 27 || key == 'q') // ESC |
134 | 116 | {
|
135 |
| - if (!pattern_glob.empty()) |
136 |
| - { |
137 |
| - bool read_image_ok = false; |
138 |
| - for (; it_image != filenames.end(); ++it_image) |
139 |
| - { |
140 |
| - cout << "\nRead: " << *it_image << endl; |
141 |
| - // Read current image |
142 |
| - frame = imread(*it_image); |
143 |
| - |
144 |
| - if (!frame.empty()) |
145 |
| - { |
146 |
| - ++it_image; |
147 |
| - read_image_ok = true; |
148 |
| - break; |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - //No more valid images |
153 |
| - if (!read_image_ok) |
154 |
| - { |
155 |
| - //Release the image in order to exit the while loop |
156 |
| - frame.release(); |
157 |
| - } |
158 |
| - } |
159 |
| - else |
160 |
| - { |
161 |
| - vc >> frame; |
162 |
| - } |
163 |
| - |
164 |
| - if (frame.empty()) |
165 |
| - break; |
166 |
| - |
167 |
| - detectAndDraw(hog, frame); |
168 |
| - |
169 |
| - imshow("people detector", frame); |
170 |
| - int c = waitKey( vc.isOpened() ? 30 : 0 ) & 255; |
171 |
| - if ( c == 'q' || c == 'Q' || c == 27) |
172 |
| - break; |
| 117 | + cout << "Exit requested" << endl; |
| 118 | + break; |
| 119 | + } |
| 120 | + else if (key == ' ') |
| 121 | + { |
| 122 | + detector.toggleMode(); |
173 | 123 | }
|
174 | 124 | }
|
175 |
| - |
176 | 125 | return 0;
|
177 | 126 | }
|
0 commit comments