-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
267 lines (213 loc) · 8.48 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (C) 2018-2019 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "core.hpp"
#include "utils.hpp"
#include "tracker.hpp"
#include "descriptor.hpp"
#include "distance.hpp"
#include "detector.hpp"
#include "pedestrian_tracker_demo.hpp"
#include <monitors/presenter.h>
#include <opencv2/core.hpp>
#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <memory>
#include <string>
#include <gflags/gflags.h>
using namespace InferenceEngine;
using ImageWithFrameIndex = std::pair<cv::Mat, int>;
std::unique_ptr<PedestrianTracker>
CreatePedestrianTracker(const std::string& reid_model,
const InferenceEngine::Core & ie,
const std::string & deviceName,
bool should_keep_tracking_info) {
TrackerParams params;
if (should_keep_tracking_info) {
params.drop_forgotten_tracks = false;
params.max_num_objects_in_track = -1;
}
std::unique_ptr<PedestrianTracker> tracker(new PedestrianTracker(params));
// Load reid-model.
std::shared_ptr<IImageDescriptor> descriptor_fast =
std::make_shared<ResizedImageDescriptor>(
cv::Size(16, 32), cv::InterpolationFlags::INTER_LINEAR);
std::shared_ptr<IDescriptorDistance> distance_fast =
std::make_shared<MatchTemplateDistance>();
tracker->set_descriptor_fast(descriptor_fast);
tracker->set_distance_fast(distance_fast);
if (!reid_model.empty()) {
CnnConfig reid_config(reid_model);
reid_config.max_batch_size = 16; // defaulting to 16
std::shared_ptr<IImageDescriptor> descriptor_strong =
std::make_shared<DescriptorIE>(reid_config, ie, deviceName);
if (descriptor_strong == nullptr) {
THROW_IE_EXCEPTION << "[SAMPLES] internal error - invalid descriptor";
}
std::shared_ptr<IDescriptorDistance> distance_strong =
std::make_shared<CosDistance>(descriptor_strong->size());
tracker->set_descriptor_strong(descriptor_strong);
tracker->set_distance_strong(distance_strong);
} else {
std::cout << "WARNING: Reid model "
<< "was not specified. "
<< "Only fast reidentification approach will be used." << std::endl;
}
return tracker;
}
bool ParseAndCheckCommandLine(int argc, char *argv[]) {
// ---------------------------Parsing and validation of input args--------------------------------------
gflags::ParseCommandLineNonHelpFlags(&argc, &argv, true);
if (FLAGS_h) {
showUsage();
showAvailableDevices();
return false;
}
if (FLAGS_i.empty()) {
throw std::logic_error("Parameter -i is not set");
}
if (FLAGS_m_det.empty()) {
throw std::logic_error("Parameter -m_det is not set");
}
if (FLAGS_m_reid.empty()) {
throw std::logic_error("Parameter -m_reid is not set");
}
return true;
}
int main_work(int argc, char **argv) {
std::cout << "InferenceEngine: " << GetInferenceEngineVersion() << std::endl;
if (!ParseAndCheckCommandLine(argc, argv)) {
return 0;
}
// Reading command line parameters.
auto det_model = FLAGS_m_det;
auto reid_model = FLAGS_m_reid;
auto detlog_out = FLAGS_out;
auto detector_mode = FLAGS_d_det;
auto reid_mode = FLAGS_d_reid;
auto custom_cpu_library = FLAGS_l;
auto path_to_custom_layers = FLAGS_c;
bool should_use_perf_counter = FLAGS_pc;
bool should_print_out = FLAGS_r;
bool should_show = !FLAGS_no_show;
int delay = FLAGS_delay;
if (!should_show)
delay = -1;
should_show = (delay >= 0);
bool should_save_det_log = !detlog_out.empty();
if ((FLAGS_last >= 0) && (FLAGS_first > FLAGS_last)) {
throw std::runtime_error("The first frame index (" + std::to_string(FLAGS_first) + ") must be greater than the "
"last frame index (" + std::to_string(FLAGS_last) + ')');
}
std::vector<std::string> devices{detector_mode, reid_mode};
InferenceEngine::Core ie =
LoadInferenceEngine(
devices, custom_cpu_library, path_to_custom_layers,
should_use_perf_counter);
DetectorConfig detector_confid(det_model);
ObjectDetector pedestrian_detector(detector_confid, ie, detector_mode);
bool should_keep_tracking_info = should_save_det_log || should_print_out;
std::unique_ptr<PedestrianTracker> tracker =
CreatePedestrianTracker(reid_model, ie, reid_mode,
should_keep_tracking_info);
cv::VideoCapture cap;
try {
int intInput = std::stoi(FLAGS_i);
if (!cap.open(intInput)) {
throw std::runtime_error("Can't open " + std::to_string(intInput));
}
} catch (const std::invalid_argument&) {
if (!cap.open(FLAGS_i)) {
throw std::runtime_error("Can't open " + FLAGS_i);
}
} catch (const std::out_of_range&) {
if (!cap.open(FLAGS_i)) {
throw std::runtime_error("Can't open " + FLAGS_i);
}
}
double video_fps = cap.get(cv::CAP_PROP_FPS);
if (0.0 == video_fps) {
// the default frame rate for DukeMTMC dataset
video_fps = 60.0;
}
if (0 >= FLAGS_first && !cap.set(cv::CAP_PROP_POS_FRAMES, FLAGS_first)) {
throw std::runtime_error("Can't set the frame to begin with");
}
std::cout << "To close the application, press 'CTRL+C' here";
if (!FLAGS_no_show) {
std::cout << " or switch to the output window and press ESC key";
}
std::cout << std::endl;
cv::Size graphSize{static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH) / 4), 60};
Presenter presenter(FLAGS_u, 10, graphSize);
for (int32_t frame_idx = std::max(0, FLAGS_first); 0 > FLAGS_last || frame_idx <= FLAGS_last; ++frame_idx) {
cv::Mat frame;
if (!cap.read(frame)) {
break;
}
pedestrian_detector.submitFrame(frame, frame_idx);
pedestrian_detector.waitAndFetchResults();
TrackedObjects detections = pedestrian_detector.getResults();
// timestamp in milliseconds
uint64_t cur_timestamp = static_cast<uint64_t >(1000.0 / video_fps * frame_idx);
tracker->Process(frame, detections, cur_timestamp);
presenter.drawGraphs(frame);
if (should_show) {
// Drawing colored "worms" (tracks).
frame = tracker->DrawActiveTracks(frame);
// Drawing all detected objects on a frame by BLUE COLOR
for (const auto &detection : detections) {
cv::rectangle(frame, detection.rect, cv::Scalar(255, 0, 0), 3);
}
// Drawing tracked detections only by RED color and print ID and detection
// confidence level.
for (const auto &detection : tracker->TrackedDetections()) {
cv::rectangle(frame, detection.rect, cv::Scalar(0, 0, 255), 3);
std::string text = std::to_string(detection.object_id) +
" conf: " + std::to_string(detection.confidence);
cv::putText(frame, text, detection.rect.tl(), cv::FONT_HERSHEY_COMPLEX,
1.0, cv::Scalar(0, 0, 255), 3);
}
cv::resize(frame, frame, cv::Size(), 0.5, 0.5);
cv::imshow("dbg", frame);
char k = cv::waitKey(delay);
if (k == 27)
break;
presenter.handleKey(k);
}
if (should_save_det_log && (frame_idx % 100 == 0)) {
DetectionLog log = tracker->GetDetectionLog(true);
SaveDetectionLogToTrajFile(detlog_out, log);
}
}
if (should_keep_tracking_info) {
DetectionLog log = tracker->GetDetectionLog(true);
if (should_save_det_log)
SaveDetectionLogToTrajFile(detlog_out, log);
if (should_print_out)
PrintDetectionLog(log);
}
if (should_use_perf_counter) {
pedestrian_detector.PrintPerformanceCounts(getFullDeviceName(ie, FLAGS_d_det));
tracker->PrintReidPerformanceCounts(getFullDeviceName(ie, FLAGS_d_reid));
}
std::cout << presenter.reportMeans() << '\n';
return 0;
}
int main(int argc, char **argv) {
try {
main_work(argc, argv);
}
catch (const std::exception& error) {
std::cout << "[ ERROR ] " << error.what() << std::endl;
return 1;
}
catch (...) {
std::cout << "[ ERROR ] Unknown/internal exception happened." << std::endl;
return 1;
}
std::cout << "Execution successful" << std::endl;
return 0;
}