1
+ // Brief Sample of using OpenCV dnn module in real time with device capture, video and image.
2
+ // VIDEO DEMO: https://www.youtube.com/watch?v=NHtRlndE2cg
3
+
1
4
#include < opencv2/dnn.hpp>
2
5
#include < opencv2/dnn/shape_utils.hpp>
3
6
#include < opencv2/imgproc.hpp>
4
7
#include < opencv2/highgui.hpp>
5
- using namespace cv ;
6
- using namespace cv ::dnn;
7
-
8
8
#include < fstream>
9
9
#include < iostream>
10
10
#include < algorithm>
11
11
#include < cstdlib>
12
+
12
13
using namespace std ;
14
+ using namespace cv ;
15
+ using namespace cv ::dnn;
13
16
14
17
const size_t network_width = 416 ;
15
18
const size_t network_height = 416 ;
16
19
17
- const char * about = " This sample uses You only look once (YOLO)-Detector "
18
- " (https://arxiv.org/abs/1612.08242) "
19
- " to detect objects on camera/video/image.\n "
20
- " Models can be downloaded here: "
21
- " https://pjreddie.com/darknet/yolo/\n "
22
- " Default network is 416x416.\n "
23
- " Class names can be downloaded here: "
24
- " https://github.com/pjreddie/darknet/tree/master/data\n " ;
25
-
26
- const char * params
27
- = " { help | false | print usage }"
28
- " { cfg | | model configuration }"
29
- " { model | | model weights }"
30
- " { camera_device | 0 | camera device number}"
31
- " { video | | video or image for detection}"
32
- " { min_confidence | 0.24 | min confidence }"
33
- " { class_names | | class names }" ;
20
+ static const char * about =
21
+ " This sample uses You only look once (YOLO)-Detector (https://arxiv.org/abs/1612.08242) to detect objects on camera/video/image.\n "
22
+ " Models can be downloaded here: https://pjreddie.com/darknet/yolo/\n "
23
+ " Default network is 416x416.\n "
24
+ " Class names can be downloaded here: https://github.com/pjreddie/darknet/tree/master/data\n " ;
25
+
26
+ static const char * params =
27
+ " { help | false | print usage }"
28
+ " { cfg | | model configuration }"
29
+ " { model | | model weights }"
30
+ " { camera_device | 0 | camera device number}"
31
+ " { source | | video or image for detection}"
32
+ " { min_confidence | 0.24 | min confidence }"
33
+ " { class_names | | File with class names, [PATH-TO-DARKNET]/data/coco.names }" ;
34
34
35
35
int main (int argc, char ** argv)
36
36
{
@@ -61,7 +61,7 @@ int main(int argc, char** argv)
61
61
}
62
62
63
63
VideoCapture cap;
64
- if (parser.get <String>(" video " ).empty ())
64
+ if (parser.get <String>(" source " ).empty ())
65
65
{
66
66
int cameraDevice = parser.get <int >(" camera_device" );
67
67
cap = VideoCapture (cameraDevice);
@@ -73,7 +73,7 @@ int main(int argc, char** argv)
73
73
}
74
74
else
75
75
{
76
- cap.open (parser.get <String>(" video " ));
76
+ cap.open (parser.get <String>(" source " ));
77
77
if (!cap.isOpened ())
78
78
{
79
79
cout << " Couldn't open image or video: " << parser.get <String>(" video" ) << endl;
@@ -86,7 +86,7 @@ int main(int argc, char** argv)
86
86
if (classNamesFile.is_open ())
87
87
{
88
88
string className = " " ;
89
- while (classNamesFile >> className)
89
+ while (std::getline ( classNamesFile, className) )
90
90
classNamesVec.push_back (className);
91
91
}
92
92
@@ -119,14 +119,14 @@ int main(int argc, char** argv)
119
119
120
120
// ! [Make forward pass]
121
121
Mat detectionMat = net.forward (" detection_out" ); // compute output
122
- // ! [Make forward pass]
122
+ // ! [Make forward pass]
123
123
124
- vector<double > layersTimings;
125
- double freq = getTickFrequency () / 1000 ;
126
- double time = net.getPerfProfile (layersTimings) / freq;
127
- ostringstream ss;
128
- ss << " FPS: " << 1000 /time << " ; time: " << time << " ms" ;
129
- putText (frame, ss.str (), Point (20 ,20 ), 0 , 0.5 , Scalar (0 ,0 ,255 ));
124
+ vector<double > layersTimings;
125
+ double freq = getTickFrequency () / 1000 ;
126
+ double time = net.getPerfProfile (layersTimings) / freq;
127
+ ostringstream ss;
128
+ ss << " FPS: " << 1000 /time << " ; time: " << time << " ms" ;
129
+ putText (frame, ss.str (), Point (20 ,20 ), 0 , 0.5 , Scalar (0 ,0 ,255 ));
130
130
131
131
float confidenceThreshold = parser.get <float >(" min_confidence" );
132
132
for (int i = 0 ; i < detectionMat.rows ; i++)
@@ -163,10 +163,10 @@ int main(int argc, char** argv)
163
163
String label = String (classNamesVec[objectClass]) + " : " + conf;
164
164
int baseLine = 0 ;
165
165
Size labelSize = getTextSize (label, FONT_HERSHEY_SIMPLEX, 0.5 , 1 , &baseLine);
166
- rectangle (frame, Rect (Point (xLeftBottom, yLeftBottom - labelSize. height ),
166
+ rectangle (frame, Rect (Point (xLeftBottom, yLeftBottom ),
167
167
Size (labelSize.width , labelSize.height + baseLine)),
168
168
Scalar (255 , 255 , 255 ), CV_FILLED);
169
- putText (frame, label, Point (xLeftBottom, yLeftBottom),
169
+ putText (frame, label, Point (xLeftBottom, yLeftBottom+labelSize. height ),
170
170
FONT_HERSHEY_SIMPLEX, 0.5 , Scalar (0 ,0 ,0 ));
171
171
}
172
172
else
@@ -181,7 +181,7 @@ int main(int argc, char** argv)
181
181
}
182
182
}
183
183
184
- imshow (" detections " , frame);
184
+ imshow (" YOLO: Detections " , frame);
185
185
if (waitKey (1 ) >= 0 ) break ;
186
186
}
187
187
0 commit comments