In this tutorial you will learn how to use the neural network to boost up the accuracy of the chart detection algorithm.
Or make sure you check the mcc module in the GUI version of CMake: cmake-gui.
<path_of_your_opencv_build_directory>/bin/example_mcc_chart_detection_with_network -t=<type_of_chart> -m=<path_to_neural_network> -pb=<path_to_models_pbtxt> -v=<optional_path_to_video_if_not_provided_webcam_will_be_used.mp4> --ci=<optional_camera_id_needed_only_if_video_not_provided> --nc=<optional_maximum_number_of_charts_in_image> --use_gpu <optional_should_gpu_be_used>
``'
* -t=# is the chart type where 0 (Standard), 1 (DigitalSG), 2 (Vinyl)
* --ci=# is the camera ID where 0 (default is the main camera), 1 (secondary camera) etc
* --nc=# By default its values is 1 which means only the best chart will be detected
Example:
Simple run on CPU (GPU wont be used) /home/opencv/build/bin/example_mcc_chart_detection_with_network -t=0 -m=/home/model.pb –pb=/home/model.pbtxt -v=mcc24.mp4
To run on GPU /home/opencv/build/bin/example_mcc_chart_detection_with_network -t=0 -m=/home/model.pb –pb=/home/model.pbtxt -v=mcc24.mp4 –use_gpu
To run on GPU and detect the best 5 charts (Detections can be less than 5 but not more than 5) /home/opencv/build/bin/example_mcc_chart_detection_with_network -t=0 -m=/home/model.pb –pb=/home/model.pbtxt -v=mcc24.mp4 –use_gpu –nc=5 ```
2
6#include <iostream>
7
11
12const char *about = "Basic chart detection using neural network";
13const char *keys = {
14 "{ help h usage ? | | show this message }"
15 "{t | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0}"
16 "{m | | File path of model, if you don't have the model you can \
17 find the link in the documentation}"
18 "{pb | | File path of pbtxt file, available along with with the model \
19 file }"
20 "{v | | Input from video file, if ommited, input comes from camera }"
21 "{ci | 0 | Camera id if input doesnt come from video (-v) }"
22 "{nc | 1 | Maximum number of charts in the image }"
23 "{use_gpu | | Add this flag if you want to use gpu}"};
24
25int main(
int argc,
char *argv[])
26{
27
28
29
30
32 parser.about(about);
33
34 if (parser.has("help"))
35 {
36 parser.printMessage();
37 return -1;
38 }
39
40 int t = parser.get<int>("t");
41
43 TYPECHART chartType = TYPECHART(t);
44
45 string model_path = parser.get<string>("m");
46 string pbtxt_path = parser.get<string>("pb");
47
48 int camId = parser.get<int>("ci");
49 int nc = parser.get<int>("nc");
50
52
53 if (parser.has("v"))
54 video = parser.get<
String>(
"v");
55
56 bool use_gpu = parser.has("use_gpu");
57
58 if (!parser.check())
59 {
60 parser.printErrors();
61 return 0;
62 }
63
65 int waitTime;
66 if (!video.empty())
67 {
68 inputVideo.
open(video);
69 waitTime = 10;
70 }
71 else
72 {
73 inputVideo.
open(camId);
74 waitTime = 10;
75 }
76
77
78
79
80
81
82
84
85 if (use_gpu)
86 {
89 }
90
92 if (!detector->setNet(net))
93 {
94 cout << "Loading Model failed: Aborting" << endl;
95 return 0;
96 }
97
98 while (inputVideo.
grab())
99 {
100 Mat image, imageCopy;
102
103 imageCopy = image.
clone();
104
105
106 if (!detector->process(image, chartType, nc, true))
107 {
108 printf("ChartColor not detected \n");
109 }
110 else
111 {
112
113
114 std::vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
116 {
117
118
120 cdraw->draw(image);
121 }
122 }
123
124 imshow(
"image result | q or esc to quit", image);
125 imshow(
"original", imageCopy);
126 char key = (char)
waitKey(waitTime);
127 if (key == 27)
128 break;
129 }
130
131 return 0;
132}
Designed for command line parsing.
Definition utility.hpp:820
n-dimensional dense array class
Definition mat.hpp:812
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:731
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool retrieve(OutputArray image, int flag=0)
Decodes and returns the grabbed video frame.
virtual bool grab()
Grabs the next frame from video file or capturing device.
This class allows to create and manipulate comprehensive artificial neural networks.
Definition dnn.hpp:475
void setPreferableBackend(int backendId)
Ask network to use specific computation backend where it supported.
void setPreferableTarget(int targetId)
Ask network to make computations on specific target device.
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:342
Net readNetFromTensorflow(CV_WRAP_FILE_PATH const String &model, CV_WRAP_FILE_PATH const String &config=String())
Reads a network model stored in TensorFlow framework's format.
@ DNN_BACKEND_CUDA
Definition dnn.hpp:79
@ DNN_TARGET_CUDA
Definition dnn.hpp:103
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition checker_detector.hpp:46
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102