Skip to content

Commit 7abaae3

Browse files
cabeloalalek
authored andcommitted
Merge pull request opencv#10446 from cabelo:style-dnn-yolo
* add style draw in yolo * fix sintaxe whitespace * fix conversion from float to int * sample refactoring - minor code style fixes - avoid confusing "bottom" names - use cv::format - always draw object detection/roi
1 parent 940a901 commit 7abaae3

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

samples/dnn/yolo_object_detection.cpp

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static const char* params =
2626
"{ model | | model weights }"
2727
"{ camera_device | 0 | camera device number}"
2828
"{ source | | video or image for detection}"
29+
"{ style | box | box or line style draw }"
2930
"{ min_confidence | 0.24 | min confidence }"
3031
"{ class_names | | File with class names, [PATH-TO-DARKNET]/data/coco.names }";
3132

@@ -78,7 +79,7 @@ int main(int argc, char** argv)
7879
}
7980
}
8081

81-
vector<string> classNamesVec;
82+
vector<String> classNamesVec;
8283
ifstream classNamesFile(parser.get<String>("class_names").c_str());
8384
if (classNamesFile.is_open())
8485
{
@@ -87,6 +88,8 @@ int main(int argc, char** argv)
8788
classNamesVec.push_back(className);
8889
}
8990

91+
String object_roi_style = parser.get<String>("style");
92+
9093
for(;;)
9194
{
9295
Mat frame;
@@ -114,11 +117,10 @@ int main(int argc, char** argv)
114117
//! [Make forward pass]
115118

116119
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));
122124

123125
float confidenceThreshold = parser.get<float>("min_confidence");
124126
for (int i = 0; i < detectionMat.rows; i++)
@@ -132,44 +134,34 @@ int main(int argc, char** argv)
132134

133135
if (confidence > confidenceThreshold)
134136
{
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")
151148
{
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);
163150
}
164151
else
165152
{
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);
172155
}
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));
173165
}
174166
}
175167

0 commit comments

Comments
 (0)