Visual Servoing Platform  version 3.5.1 under development (2022-07-01)
tutorial-dnn-object-detection-live.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/detection/vpDetectorDNN.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 
8 int main(int argc, const char *argv[])
9 {
10 #if (VISP_HAVE_OPENCV_VERSION >= 0x030403) && defined(VISP_HAVE_OPENCV_DNN)
11  try {
12  int opt_device = 0;
13  std::string input = "";
15  std::string model = "opencv_face_detector_uint8.pb";
16  std::string config = "opencv_face_detector.pbtxt";
18  int inputWidth = 300, inputHeight = 300;
19  double meanR = 104.0, meanG = 177.0, meanB = 123.0;
20  double scaleFactor = 1.0;
21  bool swapRB = false;
22  float confThresh = 0.5f;
23  float nmsThresh = 0.4f;
24  std::string labelFile = "";
25  for (int i = 1; i < argc; i++) {
26  if (std::string(argv[i]) == "--device" && i + 1 < argc) {
27  opt_device = atoi(argv[i + 1]);
28  } else if (std::string(argv[i]) == "--input" && i + 1 < argc) {
29  input = std::string(argv[i + 1]);
30  } else if (std::string(argv[i]) == "--model" && i + 1 < argc) {
31  model = std::string(argv[i + 1]);
32  } else if (std::string(argv[i]) == "--config" && i + 1 < argc) {
33  config = std::string(argv[i + 1]);
34  } else if (std::string(argv[i]) == "--width" && i + 1 < argc) {
35  inputWidth = atoi(argv[i + 1]);
36  } else if (std::string(argv[i]) == "--height" && i + 1 < argc) {
37  inputHeight = atoi(argv[i + 1]);
38  } else if (std::string(argv[i]) == "--mean" && i + 3 < argc) {
39  meanR = atof(argv[i + 1]);
40  meanG = atof(argv[i + 2]);
41  meanB = atof(argv[i + 3]);
42  } else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
43  scaleFactor = atof(argv[i + 1]);
44  } else if (std::string(argv[i]) == "--swapRB") {
45  swapRB = true;
46  } else if (std::string(argv[i]) == "--confThresh" && i + 1 < argc) {
47  confThresh = (float)atof(argv[i + 1]);
48  } else if (std::string(argv[i]) == "--nmsThresh" && i + 1 < argc) {
49  nmsThresh = (float)atof(argv[i + 1]);
50  } else if (std::string(argv[i]) == "--labels" && i + 1 < argc) {
51  labelFile = std::string(argv[i + 1]);
52  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
53  std::cout << argv[0]
54  << " --device <camera device number> --input <path to image or video>"
55  " (camera is used if input is empty) --model <path to net trained weights>"
56  " --config <path to net config file>"
57  " --width <blob width> --height <blob height>"
58  " -- mean <meanR meanG meanB> --scale <scale factor>"
59  " --swapRB --confThresh <confidence threshold>"
60  " --nmsThresh <NMS threshold> --labels <path to label file>"
61  << std::endl;
62  return EXIT_SUCCESS;
63  }
64  }
65 
66  std::cout << "Model: " << model << std::endl;
67  std::cout << "Config: " << config << std::endl;
68  std::cout << "Width: " << inputWidth << std::endl;
69  std::cout << "Height: " << inputHeight << std::endl;
70  std::cout << "Mean: " << meanR << ", " << meanG << ", " << meanB << std::endl;
71  std::cout << "Scale: " << scaleFactor << std::endl;
72  std::cout << "Swap RB? " << swapRB << std::endl;
73  std::cout << "Confidence threshold: " << confThresh << std::endl;
74  std::cout << "NMS threshold: " << nmsThresh << std::endl;
75 
76  cv::VideoCapture capture;
77  if (input.empty()) {
78  capture.open(opt_device);
79  } else {
80  capture.open(input);
81  }
82 
84 #if defined(VISP_HAVE_X11)
85  vpDisplayX d;
86 #elif defined(VISP_HAVE_GDI)
87  vpDisplayGDI d;
88 #elif defined(VISP_HAVE_OPENCV)
90 #endif
91 
93  vpDetectorDNN dnn;
94  dnn.readNet(model, config);
95  dnn.setInputSize(inputWidth, inputHeight);
96  dnn.setMean(meanR, meanG, meanB);
97  dnn.setScaleFactor(scaleFactor);
98  dnn.setSwapRB(swapRB);
99  dnn.setConfidenceThreshold(confThresh);
100  dnn.setNMSThreshold(nmsThresh);
102 
103  std::vector<std::string> labels;
104  if (!labelFile.empty()) {
105  std::ifstream f_label(labelFile);
106  std::string line;
107  while (std::getline(f_label, line)) {
108  labels.push_back(line);
109  }
110  }
111 
112  cv::Mat frame;
113  while (true) {
114  capture >> frame;
115  if (frame.empty())
116  break;
117 
118  if (I.getSize() == 0) {
119  vpImageConvert::convert(frame, I);
120  d.init(I);
121  vpDisplay::setTitle(I, "DNN object detection");
122  } else {
123  vpImageConvert::convert(frame, I);
124  }
125 
126  double t = vpTime::measureTimeMs();
128  std::vector<vpRect> boundingBoxes;
129  dnn.detect(I, boundingBoxes);
131  t = vpTime::measureTimeMs() - t;
132 
134 
136  std::vector<int> classIds = dnn.getDetectionClassIds();
137  std::vector<float> confidences = dnn.getDetectionConfidence();
139  for (size_t i = 0; i < boundingBoxes.size(); i++) {
140  vpDisplay::displayRectangle(I, boundingBoxes[i], vpColor::red, false, 2);
141 
142  std::ostringstream oss;
143  if (labels.empty())
144  oss << "class: " << classIds[i];
145  else
146  oss << labels[classIds[i]];
147  oss << " - conf: " << confidences[i];
148 
149  vpDisplay::displayText(I, (int)boundingBoxes[i].getTop() - 10, (int)boundingBoxes[i].getLeft() + 10, oss.str(),
150  vpColor::red);
151  }
152  std::ostringstream oss;
153  oss << "Detection time: " << t << " ms";
154  vpDisplay::displayText(I, 20, 20, oss.str(), vpColor::red);
155 
156  vpDisplay::flush(I);
157  if (vpDisplay::getClick(I, false))
158  break;
159  }
160 
161  } catch (const vpException &e) {
162  std::cout << e.what() << std::endl;
163  }
164 #else
165  (void)argc;
166  (void)argv;
167 #endif
168  return EXIT_SUCCESS;
169 }
static const vpColor red
Definition: vpColor.h:217
std::vector< float > getDetectionConfidence(bool afterNMS=true) const
void setMean(double meanR, double meanG, double meanB)
void setInputSize(int width, int height)
void setScaleFactor(double scaleFactor)
void readNet(const std::string &model, const std::string &config="", const std::string &framework="")
void setNMSThreshold(float nmsThreshold)
void setConfidenceThreshold(float confThreshold)
void setSwapRB(bool swapRB)
std::vector< int > getDetectionClassIds(bool afterNMS=true) const
virtual bool detect(const vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * what() const
Definition: vpException.cpp:99
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getSize() const
Definition: vpImage.h:227
VISP_EXPORT double measureTimeMs()