@@ -145,7 +145,7 @@ void YoloDarknetDetector::Detect(const cv::UMat& colorFrame)
145145 }
146146 }
147147
148- if (crops.size () > 1 )
148+ if (crops.size () > 1 || m_batchSize > 1 )
149149 {
150150 nms3<CRegion>(tmpRegions, m_regions, 0 .4f ,
151151 [](const CRegion& reg) { return reg.m_brect ; },
@@ -305,25 +305,42 @@ void YoloDarknetDetector::FillBatchImg(const std::vector<cv::Mat>& batch, image_
305305// /
306306void YoloDarknetDetector::Detect (const std::vector<cv::UMat>& frames, std::vector<regions_t >& regions)
307307{
308- std::vector<cv::Mat> batch;
309- for (const auto & frame : frames)
308+ if (frames.size () == 1 )
310309 {
311- batch. emplace_back (frame .getMat (cv::ACCESS_READ));
310+ Detect (frames[ 0 ] .getMat (cv::ACCESS_READ), regions[ 0 ] );
312311 }
312+ else
313+ {
314+ std::vector<cv::Mat> batch;
315+ for (const auto & frame : frames)
316+ {
317+ batch.emplace_back (frame.getMat (cv::ACCESS_READ));
318+ }
313319
314- image_t detImage;
315- FillBatchImg (batch, detImage);
316- std::vector<std::vector<bbox_t >> result_vec = m_detector->detectBatch (detImage, static_cast <int >(frames.size ()), m_netSize.width , m_netSize.height , m_confidenceThreshold);
320+ image_t detImage;
321+ FillBatchImg (batch, detImage);
322+ std::vector<std::vector<bbox_t >> result_vec = m_detector->detectBatch (detImage, static_cast <int >(frames.size ()), m_netSize.width , m_netSize.height , m_confidenceThreshold);
317323
318- float wk = static_cast < float >(frames[ 0 ]. cols ) / m_netSize. width ;
319- float hk = static_cast < float >(frames [0 ].rows ) / m_netSize. height ;
320- for ( size_t i = 0 ; i < regions. size (); ++i)
321- {
322- for (const auto & bbox : result_vec[i] )
324+ regions_t tmpRegions ;
325+ tmpRegions. reserve (result_vec [0 ].size () + 16 ) ;
326+ float wk = static_cast < float >(frames[ 0 ]. cols ) / m_netSize. width ;
327+ float hk = static_cast < float >(frames[ 0 ]. rows ) / m_netSize. height ;
328+ for (size_t i = 0 ; i < regions. size (); ++i )
323329 {
324- if (m_classesWhiteList.empty () || m_classesWhiteList.find (T2T (bbox.obj_id )) != std::end (m_classesWhiteList))
325- regions[i].emplace_back (cv::Rect (cvRound (wk * bbox.x ), cvRound (hk * bbox.y ), cvRound (wk * bbox.w ), cvRound (hk * bbox.h )), T2T (bbox.obj_id ), bbox.prob );
330+ tmpRegions.clear ();
331+ for (const auto & bbox : result_vec[i])
332+ {
333+ if (m_classesWhiteList.empty () || m_classesWhiteList.find (T2T (bbox.obj_id )) != std::end (m_classesWhiteList))
334+ tmpRegions.emplace_back (cv::Rect (cvRound (wk * bbox.x ), cvRound (hk * bbox.y ), cvRound (wk * bbox.w ), cvRound (hk * bbox.h )), T2T (bbox.obj_id ), bbox.prob );
335+ }
336+
337+ nms3<CRegion>(tmpRegions, regions[i], 0 .4f ,
338+ [](const CRegion& reg) { return reg.m_brect ; },
339+ [](const CRegion& reg) { return reg.m_confidence ; },
340+ [](const CRegion& reg) { return reg.m_type ; },
341+ 0 , 0 .f );
326342 }
343+
344+ m_regions.assign (std::begin (regions.back ()), std::end (regions.back ()));
327345 }
328- m_regions.assign (std::begin (regions.back ()), std::end (regions.back ()));
329346}
0 commit comments