4#include <visp3/core/vpConfig.h>
7#if defined(HAVE_OPENCV_DNN) && defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_DISPLAY) && \
8 ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
10#include <visp3/core/vpIoTools.h>
11#include <visp3/detection/vpDetectorDNNOpenCV.h>
12#include <visp3/gui/vpDisplayFactory.h>
14#include <opencv2/videoio.hpp>
16#ifdef VISP_HAVE_NLOHMANN_JSON
17#include VISP_NLOHMANN_JSON(json.hpp)
18using json = nlohmann::json;
21#ifdef ENABLE_VISP_NAMESPACE
27 DETECTION_CONTAINER_MAP = 0,
28 DETECTION_CONTAINER_VECTOR = 1,
29 DETECTION_CONTAINER_BOTH = 2,
30 DETECTION_CONTAINER_COUNT = 3
31} ChosenDetectionContainer;
33std::string chosenDetectionContainerToString(
const ChosenDetectionContainer &choice);
34ChosenDetectionContainer chosenDetectionContainerFromString(
const std::string &choiceStr);
35std::string getAvailableDetectionContainer();
37std::string chosenDetectionContainerToString(
const ChosenDetectionContainer &choice)
40 case DETECTION_CONTAINER_MAP:
42 case DETECTION_CONTAINER_VECTOR:
44 case DETECTION_CONTAINER_BOTH:
52ChosenDetectionContainer chosenDetectionContainerFromString(
const std::string &choiceStr)
54 ChosenDetectionContainer choice(DETECTION_CONTAINER_COUNT);
55 bool hasFoundMatch =
false;
56 for (
unsigned int i = 0;
i < DETECTION_CONTAINER_COUNT && !hasFoundMatch;
i++) {
57 ChosenDetectionContainer candidate = (ChosenDetectionContainer)i;
66std::string getAvailableDetectionContainer()
68 std::string availableContainers(
"< ");
69 for (
unsigned int i = 0;
i < DETECTION_CONTAINER_COUNT - 1;
i++) {
70 std::string name = chosenDetectionContainerToString((ChosenDetectionContainer)i);
71 availableContainers += name +
" , ";
73 availableContainers +=
74 chosenDetectionContainerToString((ChosenDetectionContainer)(DETECTION_CONTAINER_COUNT - 1)) +
" >";
75 return availableContainers;
78int main(
int argc,
const char *argv[])
80#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
86 std::string opt_device(
"0");
88 std::string opt_dnn_model =
"opencv_face_detector_uint8.pb";
89 std::string opt_dnn_config =
"opencv_face_detector.pbtxt";
90 std::string opt_dnn_framework =
"none";
91 std::string opt_dnn_label_file =
"";
94 int opt_dnn_width = 300, opt_dnn_height = 300;
95 double opt_dnn_meanR = 104.0, opt_dnn_meanG = 177.0, opt_dnn_meanB = 123.0;
96 double opt_dnn_scale_factor = 1.0;
97 bool opt_dnn_swapRB =
false;
98 bool opt_step_by_step =
false;
99 float opt_dnn_confThresh = 0.5f;
100 float opt_dnn_nmsThresh = 0.4f;
101 double opt_dnn_filterThresh = 0.25;
102 ChosenDetectionContainer opt_dnn_containerType = DETECTION_CONTAINER_MAP;
103 bool opt_verbose =
false;
104 std::string opt_input_json =
"";
105 std::string opt_output_json =
"";
107 for (
int i = 1;
i < argc;
i++) {
108 if (std::string(argv[i]) ==
"--device" && i + 1 < argc) {
109 opt_device = std::string(argv[++i]);
111 else if (std::string(argv[i]) ==
"--step-by-step") {
112 opt_step_by_step =
true;
114 else if (std::string(argv[i]) ==
"--model" && i + 1 < argc) {
115 opt_dnn_model = std::string(argv[++i]);
117 else if (std::string(argv[i]) ==
"--type" && i + 1 < argc) {
120 else if (std::string(argv[i]) ==
"--config" && i + 1 < argc) {
121 opt_dnn_config = std::string(argv[++i]);
122 if (opt_dnn_config.find(
"none") != std::string::npos) {
123 opt_dnn_config = std::string();
126 else if (std::string(argv[i]) ==
"--framework" && i + 1 < argc) {
127 opt_dnn_framework = std::string(argv[++i]);
128 if (opt_dnn_framework.find(
"none") != std::string::npos) {
129 opt_dnn_framework = std::string();
132 else if (std::string(argv[i]) ==
"--width" && i + 1 < argc) {
133 opt_dnn_width = atoi(argv[++i]);
135 else if (std::string(argv[i]) ==
"--height" && i + 1 < argc) {
136 opt_dnn_height = atoi(argv[++i]);
138 else if (std::string(argv[i]) ==
"--mean" && i + 3 < argc) {
139 opt_dnn_meanR = atof(argv[++i]);
140 opt_dnn_meanG = atof(argv[++i]);
141 opt_dnn_meanB = atof(argv[++i]);
143 else if (std::string(argv[i]) ==
"--scale" && i + 1 < argc) {
144 opt_dnn_scale_factor = atof(argv[++i]);
146 else if (std::string(argv[i]) ==
"--swapRB") {
147 opt_dnn_swapRB =
true;
149 else if (std::string(argv[i]) ==
"--confThresh" && i + 1 < argc) {
150 opt_dnn_confThresh =
static_cast<float>(atof(argv[++i]));
152 else if (std::string(argv[i]) ==
"--nmsThresh" && i + 1 < argc) {
153 opt_dnn_nmsThresh =
static_cast<float>(atof(argv[++i]));
155 else if (std::string(argv[i]) ==
"--filterThresh" && i + 1 < argc) {
156 opt_dnn_filterThresh = atof(argv[++i]);
158 else if (std::string(argv[i]) ==
"--labels" && i + 1 < argc) {
159 opt_dnn_label_file = std::string(argv[++i]);
161 else if (std::string(argv[i]) ==
"--container" && i + 1 < argc) {
162 opt_dnn_containerType = chosenDetectionContainerFromString(std::string(argv[++i]));
164 else if (std::string(argv[i]) ==
"--input-json" && i + 1 < argc) {
165 opt_input_json = std::string(std::string(argv[++i]));
167 else if (std::string(argv[i]) ==
"--output-json" && i + 1 < argc) {
168 opt_output_json = std::string(std::string(argv[++i]));
170 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
173 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
174 std::cout <<
"\nSYNOPSIS " << std::endl
175 << argv[0] <<
" [--device <video>]"
176 <<
" [--model <dnn weights file>]"
177 <<
" [--type <dnn type>]"
178 <<
" [--config <dnn config file]"
179 <<
" [--framework <name>]"
180 <<
" [--width <blob width>] [--height <blob height>]"
181 <<
" [--mean <meanR meanG meanB>]"
182 <<
" [--scale <scale factor>]"
184 <<
" [--confThresh <threshold>]"
185 <<
" [--nmsThresh <threshold>]"
186 <<
" [--filterThresh <threshold>]"
187 <<
" [--labels <file>]"
188 <<
" [--container <type>]"
189 <<
" [--input-json <path_to_input_json>]"
190 <<
" [--output-json <path_to_output_json>]"
191 <<
" [--step-by-step]"
192 <<
" [--verbose, -v]"
193 <<
" [--help, -h]" << std::endl;
194 std::cout <<
"\nOPTIONS " << std::endl
195 <<
" --device <video>" << std::endl
196 <<
" Camera device number or video name used to stream images." << std::endl
197 <<
" To use the first camera found on the bus set 0. On Ubuntu setting 0" << std::endl
198 <<
" will use /dev/video0 device. To use a video simply put the name of" << std::endl
199 <<
" the video, like \"path/my-video.mp4\" or \"path/image-%04d.png\"" << std::endl
200 <<
" if your video is a sequence of images." << std::endl
201 <<
" Default: " << opt_device << std::endl
203 <<
" --model <dnn weights file>" << std::endl
204 <<
" Path to dnn network trained weights." << std::endl
205 <<
" Default: " << opt_dnn_model << std::endl
207 <<
" --type <dnn type>" << std::endl
208 <<
" Type of dnn network. Admissible values are in " << std::endl
210 <<
" Default: " << opt_dnn_type << std::endl
212 <<
" --config <dnn config file>" << std::endl
213 <<
" Path to dnn network config file or \"none\" not to use one. " << std::endl
214 <<
" Default: " << opt_dnn_config << std::endl
216 <<
" --framework <name>" << std::endl
217 <<
" Framework name or \"none\" not to specify one. " << std::endl
218 <<
" Default: " << opt_dnn_framework << std::endl
220 <<
" --width <blob width>" << std::endl
221 <<
" Input images will be resized to this width. " << std::endl
222 <<
" Default: " << opt_dnn_width << std::endl
224 <<
" --height <blob height>" << std::endl
225 <<
" Input images will be resized to this height. " << std::endl
226 <<
" Default: " << opt_dnn_height << std::endl
228 <<
" --mean <meanR meanG meanB>" << std::endl
229 <<
" Mean RGB subtraction values. " << std::endl
230 <<
" Default: " << opt_dnn_meanR <<
" " << opt_dnn_meanG <<
" " << opt_dnn_meanB << std::endl
232 <<
" --scale <scale factor>" << std::endl
233 <<
" Scale factor used to normalize the range of pixel values. " << std::endl
234 <<
" Default: " << opt_dnn_scale_factor << std::endl
236 <<
" --swapRB" << std::endl
237 <<
" When used this option allows to swap Red and Blue channels. " << std::endl
239 <<
" --confThresh <threshold>" << std::endl
240 <<
" Confidence threshold. " << std::endl
241 <<
" Default: " << opt_dnn_confThresh << std::endl
243 <<
" --nmsThresh <threshold>" << std::endl
244 <<
" Non maximum suppression threshold. " << std::endl
245 <<
" Default: " << opt_dnn_nmsThresh << std::endl
247 <<
" --filterThresh <threshold >" << std::endl
248 <<
" Filter threshold. Set 0. to disable." << std::endl
249 <<
" Default: " << opt_dnn_filterThresh << std::endl
251 <<
" --labels <file>" << std::endl
252 <<
" Path to label file either in txt or yaml format. Keep empty if unknown." << std::endl
253 <<
" Default: \"" << opt_dnn_label_file <<
"\"" << std::endl
255 <<
" --container <type>" << std::endl
256 <<
" Container type in " << getAvailableDetectionContainer() << std::endl
257 <<
" Default: " << chosenDetectionContainerToString(opt_dnn_containerType) << std::endl
259 <<
" --input-json <path_to_input_json>" << std::endl
260 <<
" Input JSON file used to configure the DNN. If set, the other arguments will be used to override the values set in the json file." << std::endl
261 <<
" Default: empty" << std::endl
263 <<
" --output-json <type>" << std::endl
264 <<
" Output JSON file where will be saved the DNN configuration. If empty, does not save the configuration." << std::endl
265 <<
" Default: empty" << std::endl
267 <<
" --step-by-step" << std::endl
268 <<
" Enable step by step mode, waiting for a user click to process next image." << std::endl
270 <<
" --verbose, -v" << std::endl
271 <<
" Enable verbose mode." << std::endl
273 <<
" --help, -h" << std::endl
274 <<
" Display this helper message." << std::endl
280 std::cout <<
"Video device : " << opt_device << std::endl;
281 std::cout <<
"Label file (optional): " << (opt_dnn_label_file.empty() ?
"None" : opt_dnn_label_file) << std::endl;
283 cv::VideoCapture capture;
284 bool hasCaptureOpeningSucceeded;
286 hasCaptureOpeningSucceeded = capture.open(std::atoi(opt_device.c_str()));
289 hasCaptureOpeningSucceeded = capture.open(opt_device);
291 if (!hasCaptureOpeningSucceeded) {
292 std::cout <<
"Capture from camera: " << opt_device <<
" didn't work" << std::endl;
301 "The file containing the classes labels \"" + opt_dnn_label_file +
"\" does not exist !"));
305#ifdef VISP_HAVE_NLOHMANN_JSON
306 if (!opt_input_json.empty()) {
312 if (!opt_input_json.empty()) {
313 std::cerr <<
"Error: NLOHMANN JSON library is not installed, please install it following ViSP documentation to configure the vpDetectorDNNOpenCV from a JSON file." << std::endl;
320 , cv::Size(opt_dnn_width, opt_dnn_height), opt_dnn_filterThresh, cv::Scalar(opt_dnn_meanR, opt_dnn_meanG, opt_dnn_meanB)
321 , opt_dnn_scale_factor, opt_dnn_swapRB, opt_dnn_type
322 , opt_dnn_model, opt_dnn_config, opt_dnn_framework
330#ifdef VISP_HAVE_NLOHMANN_JSON
331 if (!opt_output_json.empty()) {
335 if (!opt_output_json.empty()) {
336 std::cerr <<
"Error: NLOHMANN JSON library is not installed, please install it following ViSP documentation to save the configuration in a JSON file." << std::endl;
343 if (frame.type() == CV_8UC4) {
346 cv::cvtColor(cpy, frame, cv::COLOR_RGBA2BGR);
351 if (I.getSize() == 0) {
356 std::cout <<
"Process image: " << I.getWidth() <<
" x " << I.getHeight() << std::endl;
363 std::cout <<
"Process new image" << std::endl;
368 if (opt_dnn_containerType == DETECTION_CONTAINER_MAP || opt_dnn_containerType == DETECTION_CONTAINER_BOTH) {
371 std::map<std::string, std::vector<vpDetectorDNNOpenCV::DetectedFeatures2D> > detections;
372 dnn.
detect(frame, detections);
377 for (
auto key_val : detections) {
379 std::cout <<
" Class name : " << key_val.first << std::endl;
383 std::cout <<
" Bounding box : " << detection.
getBoundingBox() << std::endl;
384 std::cout <<
" Class Id : " << detection.
getClassId() << std::endl;
386 std::cout <<
" Class name : " << detection.
getClassName().value() << std::endl;
394 std::ostringstream oss_map;
395 oss_map <<
"Detection time (map): " <<
t <<
" ms";
398 std::cout <<
" " << oss_map.str() << std::endl;
404 if (opt_dnn_containerType == DETECTION_CONTAINER_VECTOR || opt_dnn_containerType == DETECTION_CONTAINER_BOTH) {
407 std::vector<vpDetectorDNNOpenCV::DetectedFeatures2D> detections_vec;
408 dnn.
detect(frame, detections_vec);
413 for (
auto detection : detections_vec) {
415 std::cout <<
" Bounding box : " << detection.
getBoundingBox() << std::endl;
416 std::cout <<
" Class Id : " << detection.
getClassId() << std::endl;
417 std::optional<std::string> classname_opt = detection.
getClassName();
418 std::cout <<
" Class name : " << (classname_opt ? *classname_opt :
"Not known") << std::endl;
425 std::ostringstream oss_vec;
426 oss_vec <<
"Detection time (vector): " << t_vector <<
" ms";
429 std::cout <<
" " << oss_vec.str() << std::endl;
436 if (opt_step_by_step) {
458 std::cout <<
e.what() << std::endl;
460#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
461 if (display !=
nullptr) {
472#if !defined(HAVE_OPENCV_DNN)
473 std::cout <<
"This tutorial needs OpenCV dnn module that is missing." << std::endl;
475#if !defined(HAVE_OPENCV_VIDEOIO)
476 std::cout <<
"This tutorial needs OpenCV videoio module that is missing." << std::endl;
478#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))
479 std::cout <<
"This tutorial needs std::c++17 standard enabled." << std::endl;
Structure containing the bounding box, expressed in pixels, confidence and class information about an...
void display(const vpImage< Type > &img, const vpColor &color=vpColor::blue, unsigned int thickness=1) const
vpRect getBoundingBox() const
double getConfidenceScore() const
std::optional< std::string > getClassName() const
unsigned int getClassId() const
Structure containing some information required for the configuration of a vpDetectorDNNOpenCV object.
void initFromJSON(const std::string &jsonPath)
Initialize detector from a json config file.
DNNResultsParsingType
Enumeration listing the types of DNN for which the vpDetectorDNNOpenCV furnishes the methods permitti...
static DNNResultsParsingType dnnResultsParsingTypeFromString(const std::string &name)
void setNetConfig(const NetConfig &config)
virtual bool detect(const vpImage< unsigned char > &I, std::vector< DetectedFeatures2D > &output)
Object detection using OpenCV DNN module.
const NetConfig & getNetConfig() const
static std::string getAvailableDnnResultsParsingTypes()
Get the list of the parsing methods / types of DNNs supported by the vpDetectorDNNOpenCV class.
void saveConfigurationInJSON(const std::string &jsonPath) const
Save the network configuration in a JSON file.
Class that defines generic functionalities for display.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
static bool isNumber(const std::string &str)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()