11#include " DNNDetector.h"
2+ #include " nms.h"
23
34// /
45// / \brief DNNDetector::DNNDetector
@@ -10,8 +11,18 @@ DNNDetector::DNNDetector(
1011 cv::UMat& colorFrame
1112 )
1213 :
13- BaseDetector(collectPoints, colorFrame)
14+ BaseDetector(collectPoints, colorFrame),
15+ m_WHRatio(InWidth / (float )InHeight),
16+ m_inScaleFactor(0 .007843f ),
17+ m_meanVal(127.5 ),
18+ m_confidenceThreshold(0 .2f )
1419{
20+ m_classNames = { " background" ,
21+ " aeroplane" , " bicycle" , " bird" , " boat" ,
22+ " bottle" , " bus" , " car" , " cat" , " chair" ,
23+ " cow" , " diningtable" , " dog" , " horse" ,
24+ " motorbike" , " person" , " pottedplant" ,
25+ " sheep" , " sofa" , " train" , " tvmonitor" };
1526}
1627
1728// /
@@ -38,38 +49,63 @@ bool DNNDetector::Init(std::string modelConfiguration, std::string modelBinary)
3849// /
3950void DNNDetector::Detect (cv::UMat& colorFrame)
4051{
41- const int inWidth = 300 ;
42- const int inHeight = 300 ;
43- const float WHRatio = inWidth / (float )inHeight;
44- const float inScaleFactor = 0 .007843f ;
45- const float meanVal = 127.5 ;
46- const float confidenceThreshold = 0 .2f ;
47- std::string classNames[] = {" background" ,
48- " aeroplane" , " bicycle" , " bird" , " boat" ,
49- " bottle" , " bus" , " car" , " cat" , " chair" ,
50- " cow" , " diningtable" , " dog" , " horse" ,
51- " motorbike" , " person" , " pottedplant" ,
52- " sheep" , " sofa" , " train" , " tvmonitor" };
53-
54- cv::Size cropSize;
55- if (colorFrame.cols / (float )colorFrame.rows > WHRatio)
52+ m_regions.clear ();
53+
54+ regions_t tmpRegions;
55+
56+ cv::Mat colorMat = colorFrame.getMat (cv::ACCESS_READ );
57+
58+ if (colorFrame.cols / (float )colorFrame.rows > m_WHRatio)
5659 {
57- cropSize = cv::Size (cvRound (colorFrame.rows * WHRatio), colorFrame.rows );
60+ cv::Rect crop (0 , 0 , cvRound (colorFrame.rows * m_WHRatio), colorFrame.rows );
61+
62+ for (; crop.x < colorMat.cols ; crop.x += crop.width )
63+ {
64+ if (crop.x + crop.width >= colorMat.cols )
65+ {
66+ crop.x = colorMat.cols - crop.width ;
67+ }
68+
69+ DetectInCrop (colorMat, crop, tmpRegions);
70+ }
5871 }
5972 else
6073 {
61- cropSize = cv::Size (colorFrame.cols , cvRound (colorFrame.cols / WHRatio));
74+ cv::Rect crop (0 , 0 , colorFrame.cols , cvRound (colorFrame.cols / m_WHRatio));
75+
76+ for (; crop.y < colorMat.rows ; crop.y += crop.height )
77+ {
78+ if (crop.y + crop.height >= colorMat.rows )
79+ {
80+ crop.y = colorMat.rows - crop.height ;
81+ }
82+
83+ DetectInCrop (colorMat, crop, tmpRegions);
84+ }
6285 }
6386
64- cv::Rect crop (cv::Point ((colorFrame.cols - cropSize.width ) / 2 , (colorFrame.rows - cropSize.height ) / 2 ), cropSize);
87+ nms3<CRegion>(tmpRegions, m_regions, 0 .5f ,
88+ [](const CRegion& reg) -> cv::Rect { return reg.m_rect ; },
89+ [](const CRegion& reg) -> float { return reg.m_confidence ; },
90+ 0 , 0 .f );
91+ }
6592
66- cv::Mat inputBlob = cv::dnn::blobFromImage (colorFrame.getMat (cv::ACCESS_READ ), inScaleFactor, cv::Size (inWidth, inHeight), meanVal, false ); // Convert Mat to batch of images
93+ // /
94+ // / \brief DNNDetector::DetectInCrop
95+ // / \param colorFrame
96+ // / \param crop
97+ // / \param tmpRegions
98+ // /
99+ void DNNDetector::DetectInCrop (cv::Mat colorFrame, const cv::Rect& crop, regions_t & tmpRegions)
100+ {
101+ // Convert Mat to batch of images
102+ cv::Mat inputBlob = cv::dnn::blobFromImage (cv::Mat (colorFrame, crop), m_inScaleFactor, cv::Size (InWidth, InHeight), m_meanVal, false , true );
67103
68104 m_net.setInput (inputBlob, " data" ); // set the network input
69105
70106 cv::Mat detection = m_net.forward (" detection_out" ); // compute output
71107
72- std::vector<double > layersTimings;
108+ // std::vector<double> layersTimings;
73109 // double freq = cv::getTickFrequency() / 1000;
74110 // double time = m_net.getPerfProfile(layersTimings) / freq;
75111
@@ -81,13 +117,13 @@ void DNNDetector::Detect(cv::UMat& colorFrame)
81117 // putText(frame, ss.str(), Point(20,20), 0, 0.5, Scalar(0,0,255));
82118 // std::cout << "Inference time, ms: " << time << endl;
83119
84- m_regions. clear ( );
120+ // cv::Point correctPoint((colorFrame.cols - crop.width) / 2, (colorFrame.rows - crop.height) / 2 );
85121
86122 for (int i = 0 ; i < detectionMat.rows ; ++i)
87123 {
88124 float confidence = detectionMat.at <float >(i, 2 );
89125
90- if (confidence > confidenceThreshold )
126+ if (confidence > m_confidenceThreshold )
91127 {
92128 size_t objectClass = (size_t )(detectionMat.at <float >(i, 1 ));
93129
@@ -98,7 +134,7 @@ void DNNDetector::Detect(cv::UMat& colorFrame)
98134
99135 cv::Rect object (xLeftBottom, yLeftBottom, xRightTop - xLeftBottom, yRightTop - yLeftBottom);
100136
101- m_regions .push_back (CRegion (object, classNames [objectClass], confidence));
137+ tmpRegions .push_back (CRegion (object, m_classNames [objectClass], confidence));
102138
103139 // cv::rectangle(frame, object, Scalar(0, 255, 0));
104140 // std::string label = classNames[objectClass] + ": " + std::to_string(confidence);
0 commit comments