1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 #ifndef _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_
5 #define _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_
6 
7 #include <vector>
8 #include <memory>
9 
10 #include <opencv2/imgproc.hpp>
11 
12 /** @defgroup dnn_objdetect DNN used for object detection
13 */
14 
15 namespace cv
16 {
17 namespace dnn_objdetect
18 {
19 
20     //! @addtogroup dnn_objdetect
21     //! @{
22 
23     /** @brief Structure to hold the details pertaining to a single bounding box
24      */
25     typedef struct
26     {
27       int xmin, xmax;
28       int ymin, ymax;
29       size_t class_idx;
30       std::string label_name;
31       double class_prob;
32     } object;
33 
34 
35     /** @brief A class to post process model predictions
36      */
37     class CV_EXPORTS InferBbox
38     {
39       public:
40         /** @brief Default constructer
41         @param _delta_bbox Blob containing relative coordinates of bounding boxes
42         @param _class_scores Blob containing the probability values of each class
43         @param _conf_scores Blob containing the confidence scores
44          */
45         InferBbox(Mat _delta_bbox, Mat _class_scores, Mat _conf_scores);
46 
47         /** @brief Filters the bounding boxes.
48          */
49         void filter(double thresh =  0.8);
50 
51         /** @brief Vector which holds the final detections of the model
52          */
53         std::vector<object> detections;
54 
55       protected:
56         /** @brief Transform relative coordinates from ConvDet to bounding box coordinates
57         @param bboxes Vector to hold the predicted bounding boxes
58          */
59         void transform_bboxes(std::vector<std::vector<double> > *bboxes);
60 
61         /** @brief Computes final probability values of each bounding box
62         @param final_probs Vector to hold the probability values
63          */
64         void final_probability_dist(std::vector<std::vector<double> > *final_probs);
65 
66         /** @brief Transform bounding boxes from [x, y, h, w] to [xmin, ymin, xmax, ymax]
67         @param pre Vector conatining initial co-ordinates
68         @param post Vector containing the transformed co-ordinates
69          */
70         void transform_bboxes_inv(std::vector<std::vector<double> > *pre,
71                                   std::vector<std::vector<double> > *post);
72 
73         /** @brief Ensures that the bounding box values are within image boundaries
74         @param min_max_boxes Vector containing bounding boxes of the form [xmin, ymin, xmax, ymax]
75          */
76         void assert_predictions(std::vector<std::vector<double> > *min_max_boxes);
77 
78         /** @brief Filter top `n` predictions
79         @param probs Final probability values of bounding boxes
80         @param boxes Predicted bounding box co-ordinates
81         @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes
82         @param top_n_idxs Containes class indices of top `n` bounding boxes
83         @param top_n_probs Contains probability values of top `n` bounding boxes
84          */
85         void filter_top_n(std::vector<std::vector<double> > *probs,
86                           std::vector<std::vector<double> > *boxes,
87                           std::vector<std::vector<double> > &top_n_boxes,
88                           std::vector<size_t> &top_n_idxs,
89                           std::vector<double> &top_n_probs);
90 
91         /** @brief Wrapper to apply Non-Maximal Supression
92         @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes
93         @param top_n_idxs Containes class indices of top `n` bounding boxes
94         @param top_n_probs Contains probability values of top `n` bounding boxes
95          */
96         void nms_wrapper(std::vector<std::vector<double> > &top_n_boxes,
97                          std::vector<size_t> &top_n_idxs,
98                          std::vector<double> &top_n_probs);
99 
100        /** @brief Applies Non-Maximal Supression
101        @param boxes Bounding box co-ordinates belonging to one class
102        @param probs Probability values of boxes belonging to one class
103         */
104         std::vector<bool> non_maximal_suppression(std::vector<std::vector<double> >
105                                          *boxes, std::vector<double> *probs);
106 
107        /** @brief Computes intersection over union of bounding boxes
108        @param boxes Vector of bounding box co-ordinates
109        @param base_box Base box wrt which IOU is calculated
110        @param iou Vector to store IOU values
111         */
112         void intersection_over_union(std::vector<std::vector<double> > *boxes,
113                           std::vector<double> *base_box, std::vector<double> *iou);
114 
comparator(std::pair<double,size_t> l1,std::pair<double,size_t> l2)115         static inline bool comparator (std::pair<double, size_t> l1,
116             std::pair<double, size_t> l2)
117         {
118           return l1.first > l2.first;
119         }
120 
121       private:
122         Mat delta_bbox;
123         Mat class_scores;
124         Mat conf_scores;
125 
126         unsigned int image_width;
127         unsigned int image_height;
128 
129         unsigned int W, H;
130         std::vector<std::vector<double> > anchors_values;
131         std::vector<std::pair<double, double> > anchor_center;
132         std::vector<std::pair<double, double> > anchor_shapes;
133 
134         std::vector<std::string> label_map;
135 
136         unsigned int num_classes;
137         unsigned int anchors_per_grid;
138         size_t anchors;
139         double intersection_thresh;
140         double nms_intersection_thresh;
141         size_t n_top_detections;
142         double epsilon;
143     };
144 
145     //! @}
146 } // namespace dnn_objdetect
147 } // namespace cv
148 #endif
149