Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
vpDetectorDNNOpenCV.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * DNN object detection using OpenCV DNN module.
32  */
33 #ifndef _vpDetectorDNN_h_
34 #define _vpDetectorDNN_h_
35 
36 #include <visp3/core/vpConfig.h>
37 
38 // Check if std:c++17 or higher.
39 // Here we cannot use (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_17) in the declaration of the class
40 #if (VISP_HAVE_OPENCV_VERSION >= 0x030403) && defined(HAVE_OPENCV_DNN) && \
41  ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
42 
43 #include <map>
44 #include <string>
45 #include <vector>
46 
47 #include <opencv2/dnn.hpp>
48 
49 #include <visp3/core/vpColor.h>
50 #include <visp3/core/vpDisplay.h>
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpRect.h>
53 
54 #include <optional>
55 #ifdef VISP_HAVE_NLOHMANN_JSON
56 #include <nlohmann/json.hpp>
57 #endif
58 
81 class VISP_EXPORT vpDetectorDNNOpenCV
82 {
83 public:
89  typedef enum DNNResultsParsingType
90  {
91  USER_SPECIFIED = 0,
92  FASTER_RCNN = 1,
93  SSD_MOBILENET = 2,
94  RESNET_10 = 3,
95  YOLO_V3 = 4,
96  YOLO_V4 = 5,
97  YOLO_V5 = 6,
98  YOLO_V7 = 7,
99  YOLO_V8 = 8,
100  COUNT = 9
101  } DNNResultsParsingType;
102 
103  typedef struct DetectionCandidates
104  {
105  std::vector< float > m_confidences;
106  std::vector< cv::Rect > m_boxes;
107  std::vector< int > m_classIds;
108  } DetectionCandidates;
109 
115  typedef class DetectedFeatures2D
116  {
117  protected:
118  vpRect m_bbox;
119  double m_score;
120  unsigned int m_cls;
121  std::optional<std::string> m_classname;
122  public:
134  inline explicit DetectedFeatures2D(double u_min, double u_max
135  , double v_min, double v_max
136  , unsigned int cls, double score
137  , const std::optional<std::string> &classname
138  )
139  : m_bbox(vpImagePoint(v_min, u_min), vpImagePoint(v_max, u_max))
140  , m_score(score)
141  , m_cls(cls)
142  {
143  if (classname) {
144  m_classname = classname;
145  }
146  else {
147  m_classname = std::nullopt;
148  }
149  };
150 
154  inline vpRect getBoundingBox() const { return m_bbox; }
158  inline double getConfidenceScore() const { return m_score; }
162  inline unsigned int getClassId() const { return m_cls; }
166  inline std::optional<std::string> getClassName() const { return m_classname; }
167 
168  template < typename Type >
169  void display(const vpImage< Type > &img, const vpColor &color = vpColor::blue, unsigned int thickness = 1) const;
170 
171  friend vpDetectorDNNOpenCV;
172  } DetectedFeatures2D;
173 
178  typedef class NetConfig
179  {
180  private:
181  float m_confThreshold;
182  float m_nmsThreshold;
183  std::vector<std::string> m_classNames;
184  cv::Size m_inputSize;
185  double m_filterSizeRatio;
187  cv::Scalar m_mean;
188  double m_scaleFactor;
189  bool m_swapRB; /*<! If true, swap R and B for mean subtraction, e.g. when a model has been trained on BGR image format.*/
190  DNNResultsParsingType m_parsingMethodType;
191  std::string m_modelFilename;
192  std::string m_modelConfigFilename; /*<! Path towards the model additional configuration file, e.g. pbtxt file.*/
193  std::string m_framework;
195 #ifdef VISP_HAVE_NLOHMANN_JSON
203  friend inline void from_json(const nlohmann::json &j, NetConfig &config)
204  {
205  config.m_confThreshold = j.value("confidenceThreshold", config.m_confThreshold);
206  if (config.m_confThreshold <= 0) {
207  throw vpException(vpException::badValue, "Confidence threshold should be > 0");
208  }
209 
210  config.m_nmsThreshold = j.value("nmsThreshold", config.m_nmsThreshold);
211  if (config.m_nmsThreshold <= 0) {
212  throw vpException(vpException::badValue, "Confidence threshold should be > 0");
213  }
214 
215  config.m_filterSizeRatio = j.value("filterSizeRatio", config.m_filterSizeRatio);
216 
217  config.m_classNames = j.value("classNames", config.m_classNames);
218 
219  std::pair<unsigned int, unsigned int> resolution = j.value("resolution", std::pair<unsigned int, unsigned int>(config.m_inputSize.width, config.m_inputSize.height));
220  config.m_inputSize.width = resolution.first;
221  config.m_inputSize.height = resolution.second;
222 
223  std::vector<double> v_mean = j.value("mean", std::vector<double>({ config.m_mean[0], config.m_mean[1], config.m_mean[2] }));
224  if (v_mean.size() != 3) {
225  throw(vpException(vpException::dimensionError, "Mean should have size = 3"));
226  }
227  config.m_mean = cv::Scalar(v_mean[0], v_mean[1], v_mean[2]);
228 
229  config.m_scaleFactor = j.value("scale", config.m_scaleFactor);
230  config.m_swapRB = j.value("swapRB", config.m_swapRB);
231  config.m_parsingMethodType = dnnResultsParsingTypeFromString(j.value("parsingType", dnnResultsParsingTypeToString(config.m_parsingMethodType)));
232  config.m_modelFilename = j.value("modelFile", config.m_modelFilename);
233  config.m_modelConfigFilename = j.value("configurationFile", config.m_modelConfigFilename);
234  config.m_framework = j.value("framework", config.m_framework);
235  }
236 
243  friend inline void to_json(nlohmann::json &j, const NetConfig &config)
244  {
245  std::pair<unsigned int, unsigned int> resolution = { config.m_inputSize.width, config.m_inputSize.height };
246  std::vector<double> v_mean = { config.m_mean[0], config.m_mean[1], config.m_mean[2] };
247  j = nlohmann::json {
248  {"confidenceThreshold", config.m_confThreshold } ,
249  {"nmsThreshold" , config.m_nmsThreshold } ,
250  {"filterSizeRatio" , config.m_filterSizeRatio} ,
251  {"classNames" , config.m_classNames } ,
252  {"resolution" , resolution } ,
253  {"mean" , v_mean } ,
254  {"scale" , config.m_scaleFactor } ,
255  {"swapRB" , config.m_swapRB } ,
256  {"parsingType" , dnnResultsParsingTypeToString(config.m_parsingMethodType) },
257  {"modelFile" , config.m_modelFilename } ,
258  {"configurationFile" , config.m_modelConfigFilename } ,
259  {"framework" , config.m_framework }
260  };
261  }
262 #endif
263 
264  public:
287  inline static std::vector<std::string> parseClassNamesFile(const std::string &filename)
288  {
289  std::vector<std::string> classNames;
290  std::ifstream ifs(filename);
291  std::string line;
292  while (getline(ifs, line)) {
293  if (line.find("[") == std::string::npos) {
294  classNames.push_back(line);
295  }
296  else {
297  std::string lineWithoutBracket;
298  if (line.find("[") != std::string::npos) {
299  lineWithoutBracket = line.substr(line.find("[") + 1, line.size() - 2); // Remove opening and closing brackets
300  }
301 
302  while (!lineWithoutBracket.empty()) {
303  std::string className;
304  auto start_pos = lineWithoutBracket.find("\"");
305  auto end_pos = lineWithoutBracket.find("\"", start_pos + 1);
306  className = lineWithoutBracket.substr(start_pos + 1, end_pos - (start_pos + 1));
307  if (!className.empty()) {
308  classNames.push_back(className);
309  lineWithoutBracket = lineWithoutBracket.substr(end_pos + 1);
310  }
311  }
312  }
313  }
314  return classNames;
315  }
316 
320  inline NetConfig()
321  : m_confThreshold(0.5f)
322  , m_nmsThreshold(0.4f)
323  , m_classNames()
324  , m_inputSize(300, 300)
325  , m_filterSizeRatio(0.)
326  , m_mean(127.5, 127.5, 127.5)
327  , m_scaleFactor(2.0 / 255.0)
328  , m_swapRB(true)
329  , m_parsingMethodType(vpDetectorDNNOpenCV::USER_SPECIFIED)
330  , m_modelFilename()
331  , m_modelConfigFilename()
332  , m_framework()
333  {
334 
335  }
336 
337  inline NetConfig(const NetConfig &config)
338  : m_confThreshold(config.m_confThreshold)
339  , m_nmsThreshold(config.m_nmsThreshold)
340  , m_classNames(config.m_classNames)
341  , m_inputSize(config.m_inputSize.width, config.m_inputSize.height)
342  , m_filterSizeRatio(config.m_filterSizeRatio)
343  , m_mean(cv::Scalar(config.m_mean[0], config.m_mean[1], config.m_mean[2]))
344  , m_scaleFactor(config.m_scaleFactor)
345  , m_swapRB(config.m_swapRB)
346  , m_parsingMethodType(config.m_parsingMethodType)
347  , m_modelFilename(config.m_modelFilename)
348  , m_modelConfigFilename(config.m_modelConfigFilename)
349  , m_framework(config.m_framework)
350  {
351 
352  }
353 
371  inline NetConfig(float confThresh, const float &nmsThresh, const std::vector<std::string> &classNames, const cv::Size &dnnInputSize, const double &filterSizeRatio = 0.
372  , const cv::Scalar &mean = cv::Scalar(127.5, 127.5, 127.5), const double &scaleFactor = 2. / 255., const bool &swapRB = true
373  , const DNNResultsParsingType &parsingType = vpDetectorDNNOpenCV::USER_SPECIFIED, const std::string &modelFilename = "", const std::string &configFilename = "", const std::string &framework = "")
374  : m_confThreshold(confThresh)
375  , m_nmsThreshold(nmsThresh)
376  , m_classNames(classNames)
377  , m_inputSize(dnnInputSize)
378  , m_filterSizeRatio(filterSizeRatio)
379  , m_mean(mean)
380  , m_scaleFactor(scaleFactor)
381  , m_swapRB(swapRB)
382  , m_parsingMethodType(parsingType)
383  , m_modelFilename(modelFilename)
384  , m_modelConfigFilename(configFilename)
385  , m_framework(framework)
386  { }
387 
405  inline NetConfig(const float &confThresh, const float &nmsThresh, const std::string &classNamesFile, const cv::Size &dnnInputSize, const double &filterSizeRatio = 0.
406  , const cv::Scalar &mean = cv::Scalar(127.5, 127.5, 127.5), const double &scaleFactor = 2. / 255., const bool &swapRB = true
407  , const DNNResultsParsingType &parsingType = vpDetectorDNNOpenCV::USER_SPECIFIED, const std::string &modelFilename = "", const std::string &configFilename = "", const std::string &framework = "")
408  : m_confThreshold(confThresh)
409  , m_nmsThreshold(nmsThresh)
410  , m_inputSize(dnnInputSize)
411  , m_filterSizeRatio(filterSizeRatio)
412  , m_mean(mean)
413  , m_scaleFactor(scaleFactor)
414  , m_swapRB(swapRB)
415  , m_parsingMethodType(parsingType)
416  , m_modelFilename(modelFilename)
417  , m_modelConfigFilename(configFilename)
418  , m_framework(framework)
419  {
420  m_classNames = parseClassNamesFile(classNamesFile);
421  }
422 
423  inline std::string toString() const
424  {
425  std::string text;
426  text += "Model : " + m_modelFilename + "\n";
427  text += "Type : " + vpDetectorDNNOpenCV::dnnResultsParsingTypeToString(m_parsingMethodType) + "\n";
428  text += "Config (optional): " + (m_modelConfigFilename.empty() ? "\"None\"" : m_modelConfigFilename) + "\n";
429  text += "Framework (optional): " + (m_framework.empty() ? "\"None\"" : m_framework) + "\n";
430  text += "Width x Height : " + std::to_string(m_inputSize.width) + " x " + std::to_string(m_inputSize.height) + "\n";
431  text += "Mean RGB : " + std::to_string(m_mean[0]) + " " + std::to_string(m_mean[1]) + " " + std::to_string(m_mean[2]) + "\n";
432  text += "Scale : " + std::to_string(m_scaleFactor) + "\n";
433  text += "Swap RB? : " + (m_swapRB ? std::string("true") : std::string("false")) + "\n";
434  text += "Confidence threshold : " + std::to_string(m_confThreshold) + "\n";
435  text += "NMS threshold : " + std::to_string(m_nmsThreshold) + "\n";
436  text += "Filter threshold : " +
437  (m_filterSizeRatio > std::numeric_limits<double>::epsilon() ? std::to_string(m_filterSizeRatio)
438  : "disabled") + "\n";
439  return text;
440  }
441 
442  friend inline std::ostream &operator<<(std::ostream &os, const NetConfig &config)
443  {
444  os << config.toString();
445  return os;
446  }
447 
448  NetConfig &operator=(const NetConfig &config)
449  {
450  m_confThreshold = config.m_confThreshold;
451  m_nmsThreshold = config.m_nmsThreshold;
452  m_classNames = config.m_classNames;
453  m_inputSize = cv::Size(config.m_inputSize.width, config.m_inputSize.height);
454  m_filterSizeRatio = config.m_filterSizeRatio;
455  m_mean = cv::Scalar(config.m_mean[0], config.m_mean[1], config.m_mean[2]);
456  m_scaleFactor = config.m_scaleFactor;
457  m_swapRB = config.m_swapRB;
458  m_parsingMethodType = config.m_parsingMethodType;
459  m_modelFilename = config.m_modelFilename;
460  m_modelConfigFilename = config.m_modelConfigFilename;
461  m_framework = config.m_framework;
462  return *this;
463  }
464 
465  friend vpDetectorDNNOpenCV;
466  } NetConfig;
467 
468  static std::string getAvailableDnnResultsParsingTypes();
469  static std::string dnnResultsParsingTypeToString(const DNNResultsParsingType &type);
470  static DNNResultsParsingType dnnResultsParsingTypeFromString(const std::string &name);
471  static std::vector<std::string> parseClassNamesFile(const std::string &filename);
472  vpDetectorDNNOpenCV();
473  vpDetectorDNNOpenCV(const NetConfig &config, const DNNResultsParsingType &typeParsingMethod, void (*parsingMethod)(DetectionCandidates &, std::vector<cv::Mat> &, const NetConfig &) = postProcess_unimplemented);
474 #ifdef VISP_HAVE_NLOHMANN_JSON
475  vpDetectorDNNOpenCV(const std::string &jsonPath, void (*parsingMethod)(DetectionCandidates &, std::vector<cv::Mat> &, const NetConfig &) = postProcess_unimplemented);
476  void initFromJSON(const std::string &jsonPath);
477  void saveConfigurationInJSON(const std::string &jsonPath) const;
478 #endif
479  virtual ~vpDetectorDNNOpenCV();
480 
481  virtual bool detect(const vpImage<unsigned char> &I, std::vector<DetectedFeatures2D> &output);
482  virtual bool detect(const vpImage<unsigned char> &I, std::map< std::string, std::vector<DetectedFeatures2D>> &output);
483  virtual bool detect(const vpImage<unsigned char> &I, std::vector< std::pair<std::string, std::vector<DetectedFeatures2D>>> &output);
484  virtual bool detect(const vpImage<vpRGBa> &I, std::vector<DetectedFeatures2D> &output);
485  virtual bool detect(const vpImage<vpRGBa> &I, std::map< std::string, std::vector<DetectedFeatures2D>> &output);
486  virtual bool detect(const vpImage<vpRGBa> &I, std::vector< std::pair<std::string, std::vector<DetectedFeatures2D>>> &output);
487  virtual bool detect(const cv::Mat &I, std::vector<DetectedFeatures2D> &output);
488  virtual bool detect(const cv::Mat &I, std::map< std::string, std::vector<DetectedFeatures2D>> &output);
489  virtual bool detect(const cv::Mat &I, std::vector< std::pair<std::string, std::vector<DetectedFeatures2D>>> &output);
490 
491  void readNet(const std::string &model, const std::string &config = "", const std::string &framework = "");
492 
493  void setNetConfig(const NetConfig &config);
494  void setConfidenceThreshold(const float &confThreshold);
495  void setNMSThreshold(const float &nmsThreshold);
496  void setDetectionFilterSizeRatio(const double &sizeRatio);
497  void setInputSize(const int &width, const int &height);
498  void setMean(const double &meanR, const double &meanG, const double &meanB);
499  void setPreferableBackend(const int &backendId);
500  void setPreferableTarget(const int &targetId);
501  void setScaleFactor(const double &scaleFactor);
502  void setSwapRB(const bool &swapRB);
503  void setParsingMethod(const DNNResultsParsingType &typeParsingMethod, void (*parsingMethod)(DetectionCandidates &, std::vector<cv::Mat> &, const NetConfig &) = postProcess_unimplemented);
504  inline const NetConfig &getNetConfig() const
505  {
506  return m_netConfig;
507  }
508 
509 #ifdef VISP_HAVE_NLOHMANN_JSON
517  friend inline void from_json(const nlohmann::json &j, vpDetectorDNNOpenCV &network)
518  {
519  network.m_netConfig = j.value("networkSettings", network.m_netConfig);
520  }
521 
528  friend inline void to_json(nlohmann::json &j, const vpDetectorDNNOpenCV &network)
529  {
530  j = nlohmann::json {
531  {"networkSettings", network.m_netConfig}
532  };
533  }
534 #endif
535 
536  friend inline std::ostream &operator<<(std::ostream &os, const vpDetectorDNNOpenCV &network)
537  {
538  os << network.m_netConfig;
539  return os;
540  }
541 
542 protected:
543 #if (VISP_HAVE_OPENCV_VERSION == 0x030403)
544  std::vector<cv::String> getOutputsNames();
545 #endif
546  std::vector<DetectedFeatures2D>
547  filterDetectionSingleClassInput(const std::vector<DetectedFeatures2D> &detected_features, const double minRatioOfAreaOk);
548 
549  std::vector<DetectedFeatures2D>
550  filterDetectionMultiClassInput(const std::vector<DetectedFeatures2D> &detected_features, const double minRatioOfAreaOk);
551 
552  std::map<std::string, std::vector<vpDetectorDNNOpenCV::DetectedFeatures2D>>
553  filterDetectionMultiClassInput(const std::map< std::string, std::vector<vpDetectorDNNOpenCV::DetectedFeatures2D>> &detected_features, const double minRatioOfAreaOk);
554 
555  void postProcess(DetectionCandidates &proposals);
556 
557  void postProcess_YoloV3_V4(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
558 
559  void postProcess_YoloV5_V7(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
560 
561  void postProcess_YoloV8(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
562 
563  void postProcess_FasterRCNN(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
564 
565 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
566  void postProcess_SSD_MobileNet(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
567 #endif
568 
569  void postProcess_ResNet_10(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
570 
571  static void postProcess_unimplemented(DetectionCandidates &proposals, std::vector<cv::Mat> &dnnRes, const NetConfig &netConfig);
572 
574  bool m_applySizeFilterAfterNMS;
576  cv::Mat m_blob;
578  vpImage<vpRGBa> m_I_color;
580  cv::Mat m_img;
582  std::vector<int> m_indices;
584  cv::dnn::Net m_net;
586  NetConfig m_netConfig;
588  std::vector<cv::String> m_outNames;
590  std::vector<cv::Mat> m_dnnRes;
592  void (*m_parsingMethod)(DetectionCandidates &, std::vector<cv::Mat> &, const NetConfig &);
593 };
594 
602 template < typename Type >
603 inline void
604 vpDetectorDNNOpenCV::DetectedFeatures2D::display(const vpImage< Type > &img, const vpColor &color, unsigned int thickness) const
605 {
606  vpDisplay::displayRectangle(img, m_bbox, color, false, thickness);
607 
608  std::stringstream ss;
609  if (m_classname) {
610  ss << *m_classname;
611  }
612  else {
613  ss << m_cls;
614  }
615  ss << "(" << std::setprecision(4) << m_score * 100. << "%)";
616  vpDisplay::displayText(img, m_bbox.getTopRight(), ss.str(), color);
617 }
618 #endif
619 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor blue
Definition: vpColor.h:217
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 emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
@ dimensionError
Bad dimension.
Definition: vpException.h:83
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Definition of the vpImage class member functions.
Definition: vpImage.h:69
Defines a rectangle in the plane.
Definition: vpRect.h:76
vpImagePoint getTopRight() const
Definition: vpRect.h:209
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.