core_detect.hpp 5.4 KB

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