@@ -45,7 +45,7 @@ std::vector<std::string> classes;
45
45
inline void preprocess (const Mat& frame, Net& net, Size inpSize, float scale,
46
46
const Scalar& mean, bool swapRB);
47
47
48
- void postprocess (Mat& frame, const std::vector<Mat>& out, Net& net);
48
+ void postprocess (Mat& frame, const std::vector<Mat>& out, Net& net, int backend );
49
49
50
50
void drawPred (int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
51
51
@@ -148,7 +148,8 @@ int main(int argc, char** argv)
148
148
149
149
// Load a model.
150
150
Net net = readNet (modelPath, configPath, parser.get <String>(" framework" ));
151
- net.setPreferableBackend (parser.get <int >(" backend" ));
151
+ int backend = parser.get <int >(" backend" );
152
+ net.setPreferableBackend (backend);
152
153
net.setPreferableTarget (parser.get <int >(" target" ));
153
154
std::vector<String> outNames = net.getUnconnectedOutLayersNames ();
154
155
@@ -245,7 +246,7 @@ int main(int argc, char** argv)
245
246
std::vector<Mat> outs = predictionsQueue.get ();
246
247
Mat frame = processedFramesQueue.get ();
247
248
248
- postprocess (frame, outs, net);
249
+ postprocess (frame, outs, net, backend );
249
250
250
251
if (predictionsQueue.counter > 1 )
251
252
{
@@ -285,7 +286,7 @@ int main(int argc, char** argv)
285
286
std::vector<Mat> outs;
286
287
net.forward (outs, outNames);
287
288
288
- postprocess (frame, outs, net);
289
+ postprocess (frame, outs, net, backend );
289
290
290
291
// Put efficiency information.
291
292
std::vector<double > layersTimes;
@@ -319,7 +320,7 @@ inline void preprocess(const Mat& frame, Net& net, Size inpSize, float scale,
319
320
}
320
321
}
321
322
322
- void postprocess (Mat& frame, const std::vector<Mat>& outs, Net& net)
323
+ void postprocess (Mat& frame, const std::vector<Mat>& outs, Net& net, int backend )
323
324
{
324
325
static std::vector<int > outLayers = net.getUnconnectedOutLayers ();
325
326
static std::string outLayerType = net.getLayer (outLayers[0 ])->type ;
@@ -396,11 +397,48 @@ void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net)
396
397
else
397
398
CV_Error (Error::StsNotImplemented, " Unknown output layer type: " + outLayerType);
398
399
399
- std::vector<int > indices;
400
- NMSBoxes (boxes, confidences, confThreshold, nmsThreshold, indices);
401
- for (size_t i = 0 ; i < indices.size (); ++i)
400
+ // NMS is used inside Region layer only on DNN_BACKEND_OPENCV for another backends we need NMS in sample
401
+ // or NMS is required if number of outputs > 1
402
+ if (outLayers.size () > 1 || (outLayerType == " Region" && backend != DNN_BACKEND_OPENCV))
403
+ {
404
+ std::map<int , std::vector<size_t > > class2indices;
405
+ for (size_t i = 0 ; i < classIds.size (); i++)
406
+ {
407
+ if (confidences[i] >= confThreshold)
408
+ {
409
+ class2indices[classIds[i]].push_back (i);
410
+ }
411
+ }
412
+ std::vector<Rect> nmsBoxes;
413
+ std::vector<float > nmsConfidences;
414
+ std::vector<int > nmsClassIds;
415
+ for (std::map<int , std::vector<size_t > >::iterator it = class2indices.begin (); it != class2indices.end (); ++it)
416
+ {
417
+ std::vector<Rect> localBoxes;
418
+ std::vector<float > localConfidences;
419
+ std::vector<size_t > classIndices = it->second ;
420
+ for (size_t i = 0 ; i < classIndices.size (); i++)
421
+ {
422
+ localBoxes.push_back (boxes[classIndices[i]]);
423
+ localConfidences.push_back (confidences[classIndices[i]]);
424
+ }
425
+ std::vector<int > nmsIndices;
426
+ NMSBoxes (localBoxes, localConfidences, confThreshold, nmsThreshold, nmsIndices);
427
+ for (size_t i = 0 ; i < nmsIndices.size (); i++)
428
+ {
429
+ size_t idx = nmsIndices[i];
430
+ nmsBoxes.push_back (localBoxes[idx]);
431
+ nmsConfidences.push_back (localConfidences[idx]);
432
+ nmsClassIds.push_back (it->first );
433
+ }
434
+ }
435
+ boxes = nmsBoxes;
436
+ classIds = nmsClassIds;
437
+ confidences = nmsConfidences;
438
+ }
439
+
440
+ for (size_t idx = 0 ; idx < boxes.size (); ++idx)
402
441
{
403
- int idx = indices[i];
404
442
Rect box = boxes[idx];
405
443
drawPred (classIds[idx], confidences[idx], box.x , box.y ,
406
444
box.x + box.width , box.y + box.height , frame);
0 commit comments