// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #ifndef _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_ #define _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_ #include #include #include /** @defgroup dnn_objdetect DNN used for object detection */ namespace cv { namespace dnn_objdetect { //! @addtogroup dnn_objdetect //! @{ /** @brief Structure to hold the details pertaining to a single bounding box */ typedef struct { int xmin, xmax; int ymin, ymax; size_t class_idx; std::string label_name; double class_prob; } object; /** @brief A class to post process model predictions */ class CV_EXPORTS InferBbox { public: /** @brief Default constructer @param _delta_bbox Blob containing relative coordinates of bounding boxes @param _class_scores Blob containing the probability values of each class @param _conf_scores Blob containing the confidence scores */ InferBbox(Mat _delta_bbox, Mat _class_scores, Mat _conf_scores); /** @brief Filters the bounding boxes. */ void filter(double thresh = 0.8); /** @brief Vector which holds the final detections of the model */ std::vector detections; protected: /** @brief Transform relative coordinates from ConvDet to bounding box coordinates @param bboxes Vector to hold the predicted bounding boxes */ void transform_bboxes(std::vector > *bboxes); /** @brief Computes final probability values of each bounding box @param final_probs Vector to hold the probability values */ void final_probability_dist(std::vector > *final_probs); /** @brief Transform bounding boxes from [x, y, h, w] to [xmin, ymin, xmax, ymax] @param pre Vector conatining initial co-ordinates @param post Vector containing the transformed co-ordinates */ void transform_bboxes_inv(std::vector > *pre, std::vector > *post); /** @brief Ensures that the bounding box values are within image boundaries @param min_max_boxes Vector containing bounding boxes of the form [xmin, ymin, xmax, ymax] */ void assert_predictions(std::vector > *min_max_boxes); /** @brief Filter top `n` predictions @param probs Final probability values of bounding boxes @param boxes Predicted bounding box co-ordinates @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes @param top_n_idxs Containes class indices of top `n` bounding boxes @param top_n_probs Contains probability values of top `n` bounding boxes */ void filter_top_n(std::vector > *probs, std::vector > *boxes, std::vector > &top_n_boxes, std::vector &top_n_idxs, std::vector &top_n_probs); /** @brief Wrapper to apply Non-Maximal Supression @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes @param top_n_idxs Containes class indices of top `n` bounding boxes @param top_n_probs Contains probability values of top `n` bounding boxes */ void nms_wrapper(std::vector > &top_n_boxes, std::vector &top_n_idxs, std::vector &top_n_probs); /** @brief Applies Non-Maximal Supression @param boxes Bounding box co-ordinates belonging to one class @param probs Probability values of boxes belonging to one class */ std::vector non_maximal_suppression(std::vector > *boxes, std::vector *probs); /** @brief Computes intersection over union of bounding boxes @param boxes Vector of bounding box co-ordinates @param base_box Base box wrt which IOU is calculated @param iou Vector to store IOU values */ void intersection_over_union(std::vector > *boxes, std::vector *base_box, std::vector *iou); static inline bool comparator (std::pair l1, std::pair l2) { return l1.first > l2.first; } private: Mat delta_bbox; Mat class_scores; Mat conf_scores; unsigned int image_width; unsigned int image_height; unsigned int W, H; std::vector > anchors_values; std::vector > anchor_center; std::vector > anchor_shapes; std::vector label_map; unsigned int num_classes; unsigned int anchors_per_grid; size_t anchors; double intersection_thresh; double nms_intersection_thresh; size_t n_top_detections; double epsilon; }; //! @} } // namespace dnn_objdetect } // namespace cv #endif