@@ -26,6 +26,7 @@ static const char* params =
26
26
" { model | | model weights }"
27
27
" { camera_device | 0 | camera device number}"
28
28
" { source | | video or image for detection}"
29
+ " { style | box | box or line style draw }"
29
30
" { min_confidence | 0.24 | min confidence }"
30
31
" { class_names | | File with class names, [PATH-TO-DARKNET]/data/coco.names }" ;
31
32
@@ -78,7 +79,7 @@ int main(int argc, char** argv)
78
79
}
79
80
}
80
81
81
- vector<string > classNamesVec;
82
+ vector<String > classNamesVec;
82
83
ifstream classNamesFile (parser.get <String>(" class_names" ).c_str ());
83
84
if (classNamesFile.is_open ())
84
85
{
@@ -87,6 +88,8 @@ int main(int argc, char** argv)
87
88
classNamesVec.push_back (className);
88
89
}
89
90
91
+ String object_roi_style = parser.get <String>(" style" );
92
+
90
93
for (;;)
91
94
{
92
95
Mat frame;
@@ -114,11 +117,10 @@ int main(int argc, char** argv)
114
117
// ! [Make forward pass]
115
118
116
119
vector<double > layersTimings;
117
- double freq = getTickFrequency () / 1000 ;
118
- double time = net.getPerfProfile (layersTimings) / freq;
119
- ostringstream ss;
120
- ss << " FPS: " << 1000 /time << " ; time: " << time << " ms" ;
121
- putText (frame, ss.str (), Point (20 ,20 ), 0 , 0.5 , Scalar (0 ,0 ,255 ));
120
+ double tick_freq = getTickFrequency ();
121
+ double time_ms = net.getPerfProfile (layersTimings) / tick_freq * 1000 ;
122
+ putText (frame, format (" FPS: %.2f ; time: %.2f ms" , 1000 .f / time_ms, time_ms),
123
+ Point (20 , 20 ), 0 , 0.5 , Scalar (0 , 0 , 255 ));
122
124
123
125
float confidenceThreshold = parser.get <float >(" min_confidence" );
124
126
for (int i = 0 ; i < detectionMat.rows ; i++)
@@ -132,44 +134,34 @@ int main(int argc, char** argv)
132
134
133
135
if (confidence > confidenceThreshold)
134
136
{
135
- float x = detectionMat.at <float >(i, 0 );
136
- float y = detectionMat.at <float >(i, 1 );
137
- float width = detectionMat.at <float >(i, 2 );
138
- float height = detectionMat.at <float >(i, 3 );
139
- int xLeftBottom = static_cast <int >((x - width / 2 ) * frame.cols );
140
- int yLeftBottom = static_cast <int >((y - height / 2 ) * frame.rows );
141
- int xRightTop = static_cast <int >((x + width / 2 ) * frame.cols );
142
- int yRightTop = static_cast <int >((y + height / 2 ) * frame.rows );
143
-
144
- Rect object (xLeftBottom, yLeftBottom,
145
- xRightTop - xLeftBottom,
146
- yRightTop - yLeftBottom);
147
-
148
- rectangle (frame, object, Scalar (0 , 255 , 0 ));
149
-
150
- if (objectClass < classNamesVec.size ())
137
+ float x_center = detectionMat.at <float >(i, 0 ) * frame.cols ;
138
+ float y_center = detectionMat.at <float >(i, 1 ) * frame.rows ;
139
+ float width = detectionMat.at <float >(i, 2 ) * frame.cols ;
140
+ float height = detectionMat.at <float >(i, 3 ) * frame.rows ;
141
+ Point p1 (cvRound (x_center - width / 2 ), cvRound (y_center - height / 2 ));
142
+ Point p2 (cvRound (x_center + width / 2 ), cvRound (y_center + height / 2 ));
143
+ Rect object (p1, p2);
144
+
145
+ Scalar object_roi_color (0 , 255 , 0 );
146
+
147
+ if (object_roi_style == " box" )
151
148
{
152
- ss.str (" " );
153
- ss << confidence;
154
- String conf (ss.str ());
155
- String label = String (classNamesVec[objectClass]) + " : " + conf;
156
- int baseLine = 0 ;
157
- Size labelSize = getTextSize (label, FONT_HERSHEY_SIMPLEX, 0.5 , 1 , &baseLine);
158
- rectangle (frame, Rect (Point (xLeftBottom, yLeftBottom ),
159
- Size (labelSize.width , labelSize.height + baseLine)),
160
- Scalar (255 , 255 , 255 ), CV_FILLED);
161
- putText (frame, label, Point (xLeftBottom, yLeftBottom+labelSize.height ),
162
- FONT_HERSHEY_SIMPLEX, 0.5 , Scalar (0 ,0 ,0 ));
149
+ rectangle (frame, object, object_roi_color);
163
150
}
164
151
else
165
152
{
166
- cout << " Class: " << objectClass << endl;
167
- cout << " Confidence: " << confidence << endl;
168
- cout << " " << xLeftBottom
169
- << " " << yLeftBottom
170
- << " " << xRightTop
171
- << " " << yRightTop << endl;
153
+ Point p_center (cvRound (x_center), cvRound (y_center));
154
+ line (frame, object.tl (), p_center, object_roi_color, 1 );
172
155
}
156
+
157
+ String className = objectClass < classNamesVec.size () ? classNamesVec[objectClass] : cv::format (" unknown(%d)" , objectClass);
158
+ String label = format (" %s: %.2f" , className.c_str (), confidence);
159
+ int baseLine = 0 ;
160
+ Size labelSize = getTextSize (label, FONT_HERSHEY_SIMPLEX, 0.5 , 1 , &baseLine);
161
+ rectangle (frame, Rect (p1, Size (labelSize.width , labelSize.height + baseLine)),
162
+ object_roi_color, CV_FILLED);
163
+ putText (frame, label, p1 + Point (0 , labelSize.height ),
164
+ FONT_HERSHEY_SIMPLEX, 0.5 , Scalar (0 ,0 ,0 ));
173
165
}
174
166
}
175
167
0 commit comments