forked from opencv/opencv_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
503 lines (442 loc) · 17.4 KB
/
demo.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include <vector>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace dnn;
const auto backendTargetPairs = vector<pair<Backend, Target>>
{
{DNN_BACKEND_OPENCV, DNN_TARGET_CPU},
{DNN_BACKEND_CUDA, DNN_TARGET_CUDA},
{DNN_BACKEND_CUDA, DNN_TARGET_CUDA_FP16},
{DNN_BACKEND_TIMVX, DNN_TARGET_NPU},
{DNN_BACKEND_CANN, DNN_TARGET_NPU}
};
const vector<string> nanodetClassLabels =
{
"person", "bicycle", "car", "motorcycle", "airplane", "bus",
"train", "truck", "boat", "traffic light", "fire hydrant",
"stop sign", "parking meter", "bench", "bird", "cat", "dog",
"horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
"backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl",
"banana", "apple", "sandwich", "orange", "broccoli", "carrot",
"hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "tv", "laptop",
"mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock",
"vase", "scissors", "teddy bear", "hair drier", "toothbrush"
};
class NanoDet
{
public:
NanoDet(const String& modelPath, const float probThresh = 0.35, const float iouThresh = 0.6,
const Backend bId = DNN_BACKEND_DEFAULT, const Target tId = DNN_TARGET_CPU) :
modelPath(modelPath), probThreshold(probThresh),
iouThreshold(iouThresh), backendId(bId), targetId(tId),
imageShape(416, 416), regMax(7)
{
this->strides = { 8, 16, 32, 64 };
this->net = readNet(modelPath);
this->net.setPreferableBackend(bId);
this->net.setPreferableTarget(tId);
this->project = Mat::zeros(1, this->regMax + 1, CV_32F);
for (size_t i = 0; i <= this->regMax; ++i)
{
this->project.at<float>(0, i) = static_cast<float>(i);
}
this->mean = Scalar(103.53, 116.28, 123.675);
this->std = Scalar(1.0 / 57.375, 1.0 / 57.12, 1.0 / 58.395);
this->generateAnchors();
}
Mat preProcess(const Mat& inputImage)
{
Image2BlobParams paramNanodet;
paramNanodet.datalayout = DNN_LAYOUT_NCHW;
paramNanodet.ddepth = CV_32F;
paramNanodet.mean = this->mean;
paramNanodet.scalefactor = this->std;
paramNanodet.size = this->imageShape;
Mat blob;
blobFromImageWithParams(inputImage, blob, paramNanodet);
return blob;
}
Mat infer(const Mat& sourceImage)
{
Mat blob = this->preProcess(sourceImage);
this->net.setInput(blob);
vector<Mat> modelOutput;
this->net.forward(modelOutput, this->net.getUnconnectedOutLayersNames());
Mat preds = this->postProcess(modelOutput);
return preds;
}
Mat reshapeIfNeeded(const Mat& input)
{
if (input.dims == 3)
{
return input.reshape(0, input.size[1]);
}
return input;
}
Mat softmaxActivation(const Mat& input)
{
Mat x_exp, x_sum, x_repeat_sum, result;
exp(input.reshape(0, input.total() / (this->regMax + 1)), x_exp);
reduce(x_exp, x_sum, 1, REDUCE_SUM, CV_32F);
repeat(x_sum, 1, this->regMax + 1, x_repeat_sum);
divide(x_exp, x_repeat_sum, result);
return result;
}
Mat applyProjection(Mat& input)
{
Mat repeat_project;
repeat(this->project, input.rows, 1, repeat_project);
multiply(input, repeat_project, input);
reduce(input, input, 1, REDUCE_SUM, CV_32F);
Mat projection = input.col(0).clone();
return projection.reshape(0, projection.total() / 4);
}
void preNMS(Mat& anchors, Mat& bbox_pred, Mat& cls_score, const int nms_pre = 1000)
{
Mat max_scores;
reduce(cls_score, max_scores, 1, REDUCE_MAX);
Mat indices;
sortIdx(max_scores.t(), indices, SORT_DESCENDING);
Mat indices_float = indices.colRange(0, nms_pre);
Mat selected_anchors, selected_bbox_pred, selected_cls_score;
for (int j = 0; j < indices_float.cols; ++j)
{
selected_anchors.push_back(anchors.row(indices_float.at<int>(j)));
selected_bbox_pred.push_back(bbox_pred.row(indices_float.at<int>(j)));
selected_cls_score.push_back(cls_score.row(indices_float.at<int>(j)));
}
anchors = selected_anchors;
bbox_pred = selected_bbox_pred;
cls_score = selected_cls_score;
}
void clipBoundingBoxes(Mat& x1, Mat& y1, Mat& x2, Mat& y2)
{
Mat zeros = Mat::zeros(x1.size(), x1.type());
x1 = min(max(x1, zeros), Scalar(this->imageShape.width - 1));
y1 = min(max(y1, zeros), Scalar(this->imageShape.height - 1));
x2 = min(max(x2, zeros), Scalar(this->imageShape.width - 1));
y2 = min(max(y2, zeros), Scalar(this->imageShape.height - 1));
}
Mat calculateBoundingBoxes(const Mat& anchors, const Mat& bbox_pred)
{
Mat x1 = anchors.col(0) - bbox_pred.col(0);
Mat y1 = anchors.col(1) - bbox_pred.col(1);
Mat x2 = anchors.col(0) + bbox_pred.col(2);
Mat y2 = anchors.col(1) + bbox_pred.col(3);
clipBoundingBoxes(x1, y1, x2, y2);
Mat bboxes;
hconcat(vector<Mat>{x1, y1, x2, y2}, bboxes);
return bboxes;
}
vector<Rect2d> bboxMatToRect2d(const Mat& bboxes)
{
Mat bboxes_wh(bboxes.clone());
bboxes_wh.colRange(2, 4) = bboxes_wh.colRange(2, 4) -= bboxes_wh.colRange(0, 2);
vector<Rect2d> boxesXYXY;
for (size_t i = 0; i < bboxes_wh.rows; i++)
{
boxesXYXY.emplace_back(bboxes.at<float>(i, 0),
bboxes.at<float>(i, 1),
bboxes.at<float>(i, 2),
bboxes.at<float>(i, 3));
}
return boxesXYXY;
}
Mat postProcess(const vector<Mat>& preds)
{
vector<Mat> cls_scores, bbox_preds;
for (size_t i = 0; i < preds.size(); i += 2)
{
cls_scores.push_back(preds[i]);
bbox_preds.push_back(preds[i + 1]);
}
vector<Mat> bboxes_mlvl;
vector<Mat> scores_mlvl;
for (size_t i = 0; i < strides.size(); ++i)
{
if (i >= cls_scores.size() || i >= bbox_preds.size()) continue;
// Extract necessary data
int stride = strides[i];
Mat cls_score = reshapeIfNeeded(cls_scores[i]);
Mat bbox_pred = reshapeIfNeeded(bbox_preds[i]);
Mat anchors = anchorsMlvl[i].t();
// Softmax activation, projection, and calculate bounding boxes
bbox_pred = softmaxActivation(bbox_pred);
bbox_pred = applyProjection(bbox_pred);
bbox_pred = stride * bbox_pred;
const int nms_pre = 1000;
if (nms_pre > 0 && cls_score.rows > nms_pre)
{
preNMS(anchors, bbox_pred, cls_score, nms_pre);
}
Mat bboxes = calculateBoundingBoxes(anchors, bbox_pred);
bboxes_mlvl.push_back(bboxes);
scores_mlvl.push_back(cls_score);
}
Mat bboxes;
Mat scores;
vconcat(bboxes_mlvl, bboxes);
vconcat(scores_mlvl, scores);
vector<Rect2d> boxesXYXY = bboxMatToRect2d(bboxes);
vector<int> classIds;
vector<float> confidences;
for (size_t i = 0; i < scores.rows; ++i)
{
Point maxLoc;
minMaxLoc(scores.row(i), nullptr, nullptr, nullptr, &maxLoc);
classIds.push_back(maxLoc.x);
confidences.push_back(scores.at<float>(i, maxLoc.x));
}
vector<int> indices;
NMSBoxesBatched(boxesXYXY, confidences, classIds, probThreshold, iouThreshold, indices);
Mat result(int(indices.size()), 6, CV_32FC1);
int row = 0;
for (auto idx : indices)
{
bboxes.rowRange(idx, idx + 1).copyTo(result(Rect(0, row, 4, 1)));
result.at<float>(row, 4) = confidences[idx];
result.at<float>(row, 5) = static_cast<float>(classIds[idx]);
row++;
}
if (indices.size() == 0)
{
return Mat();
}
return result;
}
void generateAnchors()
{
for (const int stride : strides) {
int feat_h = this->imageShape.height / stride;
int feat_w = this->imageShape.width / stride;
vector<Mat> anchors;
for (int y = 0; y < feat_h; ++y)
{
for (int x = 0; x < feat_w; ++x)
{
float shift_x = x * stride;
float shift_y = y * stride;
float cx = shift_x + 0.5 * (stride - 1);
float cy = shift_y + 0.5 * (stride - 1);
Mat anchor_point = (Mat_<float>(2, 1) << cx, cy);
anchors.push_back(anchor_point);
}
}
Mat anchors_mat;
hconcat(anchors, anchors_mat);
this->anchorsMlvl.push_back(anchors_mat);
}
}
private:
Net net;
String modelPath;
vector<int> strides;
Size imageShape;
int regMax;
float probThreshold;
float iouThreshold;
Backend backendId;
Target targetId;
Mat project;
Scalar mean;
Scalar std;
vector<Mat> anchorsMlvl;
};
// Function to resize and pad an image and return both the image and scale information
tuple<Mat, vector<double>> letterbox(const Mat& sourceImage, const Size& target_size = Size(416, 416))
{
Mat img = sourceImage.clone();
double top = 0, left = 0, newh = target_size.height, neww = target_size.width;
if (img.rows != img.cols)
{
double hw_scale = static_cast<double>(img.rows) / img.cols;
if (hw_scale > 1)
{
newh = target_size.height;
neww = static_cast<int>(target_size.width / hw_scale);
resize(img, img, Size(neww, newh), 0, 0, INTER_AREA);
left = static_cast<int>((target_size.width - neww) * 0.5);
copyMakeBorder(img, img, 0, 0, left, target_size.width - neww - left, BORDER_CONSTANT, Scalar(0));
}
else
{
newh = static_cast<int>(target_size.height * hw_scale);
neww = target_size.width;
resize(img, img, Size(neww, newh), 0, 0, INTER_AREA);
top = static_cast<int>((target_size.height - newh) * 0.5);
copyMakeBorder(img, img, top, target_size.height - newh - top, 0, 0, BORDER_CONSTANT, Scalar(0));
}
}
else
{
resize(img, img, target_size, 0, 0, INTER_AREA);
}
vector<double> letterbox_scale = {top, left, newh, neww};
return make_tuple(img, letterbox_scale);
}
// Function to scale bounding boxes back to original image coordinates
vector<int> unletterbox(const Mat& bbox, const Size& original_image_shape, const vector<double>& letterbox_scale)
{
vector<int> ret(bbox.cols);
int h = original_image_shape.height;
int w = original_image_shape.width;
double top = letterbox_scale[0];
double left = letterbox_scale[1];
double newh = letterbox_scale[2];
double neww = letterbox_scale[3];
if (h == w)
{
double ratio = static_cast<double>(h) / newh;
for (int& val : ret)
{
val = static_cast<int>(val * ratio);
}
return ret;
}
double ratioh = static_cast<double>(h) / newh;
double ratiow = static_cast<double>(w) / neww;
ret[0] = max(static_cast<int>((bbox.at<float>(0) - left) * ratiow), 0);
ret[1] = max(static_cast<int>((bbox.at<float>(1) - top) * ratioh), 0);
ret[2] = min(static_cast<int>((bbox.at<float>(2) - left) * ratiow), w);
ret[3] = min(static_cast<int>((bbox.at<float>(3) - top) * ratioh), h);
return ret;
}
// Function to visualize predictions on an image
Mat visualize(const Mat& preds, const Mat& result_image, const vector<double>& letterbox_scale, bool video, double fps = 0.0)
{
Mat visualized_image = result_image.clone();
// Draw FPS if provided
if (fps > 0.0 && video)
{
std::ostringstream fps_stream;
fps_stream << "FPS: " << std::fixed << std::setprecision(2) << fps;
putText(visualized_image, fps_stream.str(), Point(10, 25), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
}
// Draw bounding boxes and labels for each prediction
for (size_t i = 0; i < preds.rows; i++)
{
Mat pred = preds.row(i);
Mat bbox = pred.colRange(0, 4);
double conf = pred.at<float>(4);
int classid = static_cast<int>(pred.at<float>(5));
// Convert bbox coordinates back to original image space
vector<int> unnormalized_bbox = unletterbox(bbox, visualized_image.size(), letterbox_scale);
// Draw bounding box
rectangle(visualized_image, Point(unnormalized_bbox[0], unnormalized_bbox[1]),
Point(unnormalized_bbox[2], unnormalized_bbox[3]), Scalar(0, 255, 0), 2);
// Draw label
stringstream label;
label << nanodetClassLabels[classid] << ": " << fixed << setprecision(2) << conf;
putText(visualized_image, label.str(), Point(unnormalized_bbox[0], unnormalized_bbox[1] - 10),
FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
}
return visualized_image;
}
void processImage(Mat& inputImage, NanoDet& nanodet, TickMeter& tm, bool save, bool vis, bool video)
{
cvtColor(inputImage, inputImage, COLOR_BGR2RGB);
tuple<Mat, vector<double>> w = letterbox(inputImage);
Mat inputBlob = get<0>(w);
vector<double> letterboxScale = get<1>(w);
tm.start();
Mat predictions = nanodet.infer(inputBlob);
tm.stop();
if (!video)
{
cout << "Inference time: " << tm.getTimeMilli() << " ms\n";
}
Mat img = visualize(predictions, inputImage, letterboxScale, video, tm.getFPS());
cvtColor(img, img, COLOR_BGR2RGB);
if (save)
{
static const string kOutputName = "result.jpg";
imwrite(kOutputName, img);
if (!video)
{
cout << "Results saved to " + kOutputName << endl;
}
}
if (vis)
{
static const string kWinName = "model";
imshow(kWinName, img);
}
}
const String keys =
"{ help h | | Print help message. }"
"{ model m | object_detection_nanodet_2022nov.onnx | Usage: Path to the model, defaults to object_detection_nanodet_2022nov.onnx }"
"{ input i | | Path to the input image. Omit for using the default camera.}"
"{ confidence | 0.35 | Class confidence }"
"{ nms | 0.6 | Enter nms IOU threshold }"
"{ save s | true | Specify to save results. This flag is invalid when using the camera. }"
"{ vis v | true | Specify to open a window for result visualization. This flag is invalid when using the camera. }"
"{ backend bt | 0 | Choose one of computation backends: "
"0: (default) OpenCV implementation + CPU, "
"1: CUDA + GPU (CUDA), "
"2: CUDA + GPU (CUDA FP16), "
"3: TIM-VX + NPU, "
"4: CANN + NPU}";
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run Nanodet inference using OpenCV, a contribution by Sri Siddarth Chakaravarthy as part of GSOC_2022.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string model = parser.get<String>("model");
string inputPath = parser.get<String>("input");
float confThreshold = parser.get<float>("confidence");
float nmsThreshold = parser.get<float>("nms");
bool save = parser.get<bool>("save");
bool vis = parser.get<bool>("vis");
int backendTargetid = parser.get<int>("backend");
if (model.empty())
{
CV_Error(Error::StsError, "Model file " + model + " not found");
}
NanoDet nanodet(model, confThreshold, nmsThreshold,
backendTargetPairs[backendTargetid].first, backendTargetPairs[backendTargetid].second);
TickMeter tm;
if (parser.has("input"))
{
Mat inputImage = imread(samples::findFile(inputPath));
static const bool kNotVideo = false;
processImage(inputImage, nanodet, tm, save, vis, kNotVideo);
waitKey(0);
}
else
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
CV_Error(Error::StsError, "Cannot open video or file");
}
Mat frame;
while (waitKey(1) < 0)
{
cap >> frame;
if (frame.empty())
{
cout << "Frame is empty" << endl;
waitKey();
break;
}
tm.reset();
static const bool kIsVideo = true;
processImage(frame, nanodet, tm, save, vis, kIsVideo);
}
cap.release();
}
return 0;
}