Skip to content

Commit 0042bac

Browse files
authored
Merge pull request opencv#10207 from cabelo:cabelo-opencv
Repair: incorrect display of class in DNN
2 parents 4d721e3 + df5ec54 commit 0042bac

File tree

4 files changed

+101
-35
lines changed

4 files changed

+101
-35
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
YOLO DNNs {#tutorial_dnn_yolo}
2+
===============================
3+
4+
Introduction
5+
------------
6+
7+
In this text you will learn how to use opencv_dnn module using yolo_object_detection (Sample of using OpenCV dnn module in real time with device capture, video and image).
8+
9+
We will demonstrate results of this example on the following picture.
10+
![Picture example](images/yolo.jpg)
11+
12+
Examples
13+
--------
14+
15+
VIDEO DEMO:
16+
@youtube{NHtRlndE2cg}
17+
18+
Source Code
19+
-----------
20+
21+
The latest version of sample source code can be downloaded [here](https://github.com/opencv/opencv/blob/master/samples/dnn/yolo_object_detection.cpp).
22+
23+
@include dnn/yolo_object_detection.cpp
24+
25+
How to compile in command line with pkg-config
26+
----------------------------------------------
27+
28+
@code{.bash}
29+
30+
# g++ `pkg-config --cflags opencv` `pkg-config --libs opencv` yolo_object_detection.cpp -o yolo_object_detection
31+
32+
@endcode
33+
34+
Execute in webcam:
35+
36+
@code{.bash}
37+
38+
$ yolo_object_detection -camera_device=0 -cfg=[PATH-TO-DARKNET]/cfg/yolo.cfg -model=[PATH-TO-DARKNET]/yolo.weights -class_names=[PATH-TO-DARKNET]/data/coco.names
39+
40+
@endcode
41+
42+
Execute with image:
43+
44+
@code{.bash}
45+
46+
$ yolo_object_detection -source=[PATH-IMAGE] -cfg=[PATH-TO-DARKNET]/cfg/yolo.cfg -model=[PATH-TO-DARKNET]/yolo.weights -class_names=[PATH-TO-DARKNET]/data/coco.names
47+
48+
@endcode
49+
50+
Execute in video file:
51+
52+
@code{.bash}
53+
54+
$ yolo_object_detection -source=[PATH-TO-VIDEO] -cfg=[PATH-TO-DARKNET]/cfg/yolo.cfg -model=[PATH-TO-DARKNET]/yolo.weights -class_names=[PATH-TO-DARKNET]/data/coco.names
55+
56+
@endcode
57+
58+
Questions and suggestions email to: Alessandro de Oliveira Faria cabelo@opensuse.org or OpenCV Team.
210 KB
Loading

doc/tutorials/dnn/table_of_content_dnn.markdown

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ Deep Neural Networks (dnn module) {#tutorial_table_of_content_dnn}
1515

1616
*Author:* Dmitry Kurtaev
1717

18-
This tutorial guidelines how to run your models in OpenCV deep learning module using Halide language backend
18+
This tutorial guidelines how to run your models in OpenCV deep learning module using Halide language backend.
1919

2020
- @subpage tutorial_dnn_halide_scheduling
2121

2222
*Compatibility:* \> OpenCV 3.3
2323

2424
*Author:* Dmitry Kurtaev
2525

26-
In this tutorial we describe the ways to schedule your networks using Halide backend in OpenCV deep learning module.
26+
In this tutorial we describe the ways to schedule your networks using Halide backend in OpenCV deep learning module.
27+
28+
- @subpage tutorial_dnn_yolo
29+
30+
*Compatibility:* \> OpenCV 3.3.1
31+
32+
*Author:* Alessandro de Oliveira Faria
33+
34+
In this tutorial you will learn how to use opencv_dnn module using yolo_object_detection with device capture, video file or image.

samples/dnn/yolo_object_detection.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
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+
14
#include <opencv2/dnn.hpp>
25
#include <opencv2/dnn/shape_utils.hpp>
36
#include <opencv2/imgproc.hpp>
47
#include <opencv2/highgui.hpp>
5-
using namespace cv;
6-
using namespace cv::dnn;
7-
88
#include <fstream>
99
#include <iostream>
1010
#include <algorithm>
1111
#include <cstdlib>
12+
1213
using namespace std;
14+
using namespace cv;
15+
using namespace cv::dnn;
1316

1417
const size_t network_width = 416;
1518
const size_t network_height = 416;
1619

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 }";
3434

3535
int main(int argc, char** argv)
3636
{
@@ -61,7 +61,7 @@ int main(int argc, char** argv)
6161
}
6262

6363
VideoCapture cap;
64-
if (parser.get<String>("video").empty())
64+
if (parser.get<String>("source").empty())
6565
{
6666
int cameraDevice = parser.get<int>("camera_device");
6767
cap = VideoCapture(cameraDevice);
@@ -73,7 +73,7 @@ int main(int argc, char** argv)
7373
}
7474
else
7575
{
76-
cap.open(parser.get<String>("video"));
76+
cap.open(parser.get<String>("source"));
7777
if(!cap.isOpened())
7878
{
7979
cout << "Couldn't open image or video: " << parser.get<String>("video") << endl;
@@ -86,7 +86,7 @@ int main(int argc, char** argv)
8686
if (classNamesFile.is_open())
8787
{
8888
string className = "";
89-
while (classNamesFile >> className)
89+
while (std::getline(classNamesFile, className))
9090
classNamesVec.push_back(className);
9191
}
9292

@@ -119,14 +119,14 @@ int main(int argc, char** argv)
119119

120120
//! [Make forward pass]
121121
Mat detectionMat = net.forward("detection_out"); //compute output
122-
//! [Make forward pass]
122+
//! [Make forward pass]
123123

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));
130130

131131
float confidenceThreshold = parser.get<float>("min_confidence");
132132
for (int i = 0; i < detectionMat.rows; i++)
@@ -163,10 +163,10 @@ int main(int argc, char** argv)
163163
String label = String(classNamesVec[objectClass]) + ": " + conf;
164164
int baseLine = 0;
165165
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 ),
167167
Size(labelSize.width, labelSize.height + baseLine)),
168168
Scalar(255, 255, 255), CV_FILLED);
169-
putText(frame, label, Point(xLeftBottom, yLeftBottom),
169+
putText(frame, label, Point(xLeftBottom, yLeftBottom+labelSize.height),
170170
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
171171
}
172172
else
@@ -181,7 +181,7 @@ int main(int argc, char** argv)
181181
}
182182
}
183183

184-
imshow("detections", frame);
184+
imshow("YOLO: Detections", frame);
185185
if (waitKey(1) >= 0) break;
186186
}
187187

0 commit comments

Comments
 (0)