-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
405 lines (352 loc) · 13.6 KB
/
main.cpp
File metadata and controls
405 lines (352 loc) · 13.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// This code is written at BigVision LLC. It is based on the OpenCV project. It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html
// Usage example: ./object_detection_yolo.out --video=run.mp4
// ./object_detection_yolo.out --image=bird.jpg
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/core/types_c.h>
#include <opencv2/bgsegm.hpp>
#include "./DeepAppearanceDescriptor/classification.h"
#include "KalmanFilter/tracker.h"
using namespace std;
using namespace cv;
const char *keys =
"{help h usage ? | | Usage examples: \n\t\t./object_detection_yolo.out --image=dog.jpg \n\t\t./object_detection_yolo.out --video=run_sm.mp4}"
"{image i |<none>| input image }"
"{video v |<none>| input video }";
// yolo parameter
// Initialize the parameters
const float confThreshold = 0.5; // Confidence threshold
const float nmsThreshold = 0.4; // Non-maximum suppression threshold
const int inpWidth = 416; // Width of network's input image
const int inpHeight = 416; // Height of network's input image
vector < string > classes;
//Deep SORT parameter
const int nn_budget = 100;
const float max_cosine_distance = 0.2;
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat & frame,
const vector < Mat > &out, DETECTIONS & d);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top,
int right, int bottom, Mat & frame);
// Get the names of the output layers
vector < String > getOutputsNames(const dnn::Net & net);
void get_detections(DETECTBOX box, float confidence,
DETECTIONS & d);
int main(int argc, char **argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about
("Use this script to run object detection using YOLO3 in OpenCV.");
if (parser.has("help")) {
parser.printMessage();
return 0;
}
//deep SORT
tracker mytracker(max_cosine_distance, nn_budget);
//yolo
// Load names of classes
string classesFile = "coco.names";
ifstream ifs(classesFile.c_str());
string line;
cout << "### Import coco.names ###" << endl;
while (getline(ifs, line)) {
classes.push_back(line);
cout << line << endl;
}
// Give the configuration and weight files for the model
String modelConfiguration = "yolov3.cfg";
String modelWeights = "yolov3.weights";
// Load the network
dnn::Net net =
dnn::readNetFromDarknet(modelConfiguration,
modelWeights);
net.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
net.setPreferableTarget(dnn::DNN_TARGET_CPU);
cout <<
"### main 89: dnn::readNetFromDarknet ###" << endl;
// Open a video file or an image file or a camera stream.
string str, outputFile;
VideoCapture cap;
VideoWriter video;
Mat frame, blob;
try {
outputFile = "yolo_out_cpp.avi";
if (parser.has("image")) {
// Open the image file
str = parser.get < String > ("image");
ifstream ifile(str);
if (!ifile)
throw("error");
cap.open(str);
str.replace(str.end() - 4, str.end(),
"_yolo_out_cpp.jpg");
outputFile = str;
} else if (parser.has("video")) {
// Open the video file
str = parser.get < String > ("video");
ifstream ifile(str);
if (!ifile)
throw("error");
cap.open(str);
str.replace(str.end() - 4, str.end(),
"_yolo_out_cpp.avi");
outputFile = str;
} else {
cout << "### Capture video 0 120###" << endl;
cap.open(0);
cout << "### Capture video 0 122###" << endl;
}
// Open the webcaom
// else cap.open(parser.get<int>("device"));
}
catch( ...) {
cout <<
"Could not open the input image/video stream" <<
endl;
return 0;
}
cout << "### main 135: Capture video ###" << endl;
// Get the video writer initialized to save the output video
if (!parser.has("image")) {
video.open(outputFile, VideoWriter::fourcc('M',
'J', 'P', 'G'), 28.0,
Size(static_cast <
int >(cap.get(CAP_PROP_FRAME_WIDTH)),
static_cast <
int >(cap.get(CAP_PROP_FRAME_HEIGHT))));
}
// Create a window
static const string kWinName =
"Multiple Object Tracking";
namedWindow(kWinName, WINDOW_NORMAL);
// Process frames.
while (waitKey(1) < 0) {
// get frame from the video
cout << "### main 154: while loop ###" << endl;
cap >> frame;
cout << "### main 156: while loop ###" << endl;
// Stop the program if reached end of video
if (frame.empty()) {
cout << "Done processing !!!" << endl;
cout << "Output file is stored as " <<
outputFile << endl;
waitKey(3000);
break;
}
// Create a 4D blob from a frame.
cout << "### main 167: while loop 4D blob ###"
<< endl;
dnn::blobFromImage(frame, blob, 1 / 255.0,
cvSize(inpWidth, inpHeight), Scalar(0, 0,
0), true, false);
//Sets the input to the network
net.setInput(blob);
// Runs the forward pass to get output of the output layers
vector < Mat > outs;
net.forward(outs, getOutputsNames(net));
// Remove the bounding boxes with low confidence
DETECTIONS detections;
postprocess(frame, outs, detections);
cout << "Detections size:" << detections.size()
<< endl;
cout << "### main 185: before FeatureTensor ###"
<< endl;
bool tfIns =
Classifier::
getInstance()->getRectsFeature(frame,
detections);
cout << "### main 187: before FeatureTensor ###"
<< endl;
if (tfIns) {
cout << "### main 188 ###" << endl;
cout << "Tensorflow get feature succeed!"
<< endl;
mytracker.predict();
cout << "### main 189 ###" << endl;
mytracker.update(detections);
cout << "### main 190 ###" << endl;
vector < RESULT_DATA > result;
for (Track & track:mytracker.tracks) {
cout << "### main 196 ###" << endl;
if (!track.is_confirmed()
|| track.time_since_update > 1)
continue;
result.push_back(make_pair(track.track_id,
track.to_tlwh()));
}
for (unsigned int k = 0; k < detections.size();
k++) {
cout << "### main 205 ###" << endl;
DETECTBOX tmpbox = detections[k].tlwh;
Rect rect(tmpbox(0), tmpbox(1),
tmpbox(2), tmpbox(3));
rectangle(frame, rect, Scalar(0, 0,
255), 4);
// cvScalar的储存顺序是B-G-R,CV_RGB的储存顺序是R-G-B
for (unsigned int k = 0; k < result.size();
k++) {
cout << "### main 215 ###" << endl;
DETECTBOX tmp = result[k].second;
Rect rect = Rect(tmp(0), tmp(1), tmp(2),
tmp(3));
rectangle(frame, rect, Scalar(255,
255, 0), 2);
string label =
format("%d", result[k].first);
putText(frame, label,
Point(rect.x, rect.y),
FONT_HERSHEY_SIMPLEX, 0.8,
Scalar(255, 255, 0), 2);
}
}
} else {
cout << "### main 232 ###" << endl;
cout << "Tensorflow get feature failed!" <<
endl;;
}
cout << "### main 237 ###" << endl;
// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
vector < double >layersTimes;
double freq = getTickFrequency() / 1000;
cout << "### main 238 ###" << endl;
double t = net.getPerfProfile(layersTimes) / freq;
string label =
format
("Inference time for a frame : %.2f ms", t);
cout << "### main 239 ###" << endl;
putText(frame, label, Point(0, 15),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
cout << "### main 250 ###" << endl;
// Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
cout << "### main 260 ###" << endl;
if (parser.has("image")){
cout << "### main 261 ###" << endl;
imwrite(outputFile, detectedFrame);
}
else{
cout << "### main 261 ###" << endl;
//video.write(detectedFrame);//matthew debug
}
cout << "### main 268 ###" << endl;
imshow(kWinName, frame);
cout << "### main 271 ###" << endl;
}
cout << "### main 283 ###" << endl;
cap.release();
if (!parser.has("image"))
video.release();
cout << "### main 288 ###" << endl;
return 0;
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat & frame,
const vector < Mat > &outs, DETECTIONS & d)
{
vector < int >classIds;
vector < float >confidences;
vector < Rect > boxes;
for (size_t i = 0; i < outs.size(); ++i) {
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class
// with the highest score for the box.
float *data = (float *) outs[i].data;
for (int j = 0; j < outs[i].rows;
++j, data += outs[i].cols) {
Mat scores =
outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
minMaxLoc(scores, 0, &confidence, 0,
&classIdPoint);
if (static_cast < float >(confidence) >
(confThreshold)) {
int centerX = (int) (data[0] * frame.cols);
int centerY = (int) (data[1] * frame.rows);
int width = (int) (data[2] * frame.cols);
int height = (int) (data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float) confidence);
boxes.push_back(Rect(left, top, width,
height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector < int >indices;
dnn::NMSBoxes(boxes, confidences, confThreshold,
nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i) {
size_t idx = static_cast < size_t > (indices[i]);
Rect box = boxes[idx];
//目标检测 代码的可视化
//drawPred(classIds[idx], confidences[idx], box.x, box.y,box.x + box.width, box.y + box.height, frame);
get_detections(DETECTBOX(box.x, box.y, box.width,
box.height), confidences[idx], d);
}
}
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top,
int right, int bottom, Mat & frame)
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(left, top),
Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence
string label = format("%.2f", conf);
if (!classes.empty()) {
CV_Assert(classId < (int) classes.size());
label = classes[classId] + ":" + label;
}
//Display the label at the top of the bounding box
int baseLine;
Size labelSize =
getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1,
&baseLine);
top = max(top, labelSize.height);
rectangle(frame, Point(left,
top - round(1.5 * labelSize.height)),
Point(left + round(1.5 * labelSize.width),
top + baseLine), Scalar(255, 255, 255), FILLED);
putText(frame, label, Point(left, top),
FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 0), 1);
}
// Get the names of the output layers
vector < String > getOutputsNames(const dnn::Net & net)
{
static vector < String > names;
if (names.empty()) {
//Get the indices of the output layers, i.e. the layers with unconnected outputs
vector < int >outLayers =
net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector < String > layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
return names;
}
void get_detections(DETECTBOX box, float confidence,
DETECTIONS & d)
{
DETECTION_ROW tmpRow;
tmpRow.tlwh = box; //DETECTBOX(x, y, w, h);
tmpRow.confidence = confidence;
d.push_back(tmpRow);
}