Visual Servoing Platform  version 3.3.1 under development (2020-08-12)
vpDetectorDNN.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * DNN object detection using OpenCV DNN module.
33  *
34  *****************************************************************************/
35 #ifndef _vpDetectorDNN_h_
36 #define _vpDetectorDNN_h_
37 
38 #include <visp3/core/vpConfig.h>
39 
40 #if (VISP_HAVE_OPENCV_VERSION >= 0x030403) && defined(VISP_HAVE_OPENCV_DNN)
41 #include <opencv2/dnn.hpp>
42 #include <visp3/detection/vpDetectorBase.h>
43 
52 class VISP_EXPORT vpDetectorDNN : public vpDetectorBase
53 {
54 public:
55  vpDetectorDNN();
56  virtual ~vpDetectorDNN();
57 
58  virtual bool detect(const vpImage<unsigned char> &I);
59  virtual bool detect(const vpImage<vpRGBa> &I, std::vector<vpRect> &boundingBoxes);
60 
61  std::vector<vpRect> getDetectionBBs(bool afterNMS=true) const;
62  std::vector<int> getDetectionClassIds(bool afterNMS=true) const;
63  std::vector<float> getDetectionConfidence(bool afterNMS=true) const;
64 
65  void readNet(const std::string &model, const std::string &config="", const std::string &framework="");
66  void setConfidenceThreshold(float confThreshold);
67  void setInputSize(int width, int height);
68  void setMean(double meanR, double meanG, double meanB);
69  void setNMSThreshold(float nmsThreshold);
70  void setPreferableBackend(int backendId);
71  void setPreferableTarget(int targetId);
72  void setScaleFactor(double scaleFactor);
73  void setSwapRB(bool swapRB);
74 
75 private:
76 #if (VISP_HAVE_OPENCV_VERSION == 0x030403)
77  std::vector<cv::String> getOutputsNames();
78 #endif
79  void postProcess();
80 
82  cv::Mat m_blob;
84  std::vector<cv::Rect> m_boxes;
86  std::vector<cv::Rect> m_boxesNMS;
88  std::vector<int> m_classIds;
90  std::vector<float> m_confidences;
92  float m_confidenceThreshold;
94  vpImage<vpRGBa> m_I_color;
96  cv::Mat m_img;
98  std::vector<int> m_indices;
100  cv::Size m_inputSize;
102  cv::Scalar m_mean;
104  cv::dnn::Net m_net;
106  float m_nmsThreshold;
108  std::vector<cv::String> m_outNames;
110  std::vector<cv::Mat> m_outs;
112  double m_scaleFactor;
114  bool m_swapRB;
115 };
116 #endif
117 #endif
virtual bool detect(const vpImage< unsigned char > &I)=0