17 constexpr
int box_elements = 4;
19 constexpr
int confidence_elements = 1;
28 float iou(
const Box& box1,
const Box& box2)
33 if (area1 <= 0 || area2 <= 0)
39 const auto y_min_intersection = std::max<float>(box1.
ymin, box2.
ymin);
40 const auto x_min_intersection = std::max<float>(box1.
xmin, box2.
xmin);
41 const auto y_max_intersection = std::min<float>(box1.
ymax, box2.
ymax);
42 const auto x_max_intersection = std::min<float>(box1.
xmax, box2.
xmax);
43 const auto area_intersection =
44 std::max<float>(y_max_intersection - y_min_intersection, 0.0f) *
45 std::max<float>(x_max_intersection - x_min_intersection, 0.0f);
46 overlap = area_intersection / (area1 + area2 - area_intersection);
51 std::vector<Detection> convert_to_detections(
const NMSConfig& config,
52 const std::vector<float>& detected_boxes)
54 const size_t element_step =
static_cast<size_t>(
55 box_elements + confidence_elements + config.
num_classes);
56 std::vector<Detection> detections;
58 for (
unsigned int i = 0; i < config.
num_boxes; ++i)
60 const float* cur_box = &detected_boxes[i * element_step];
64 det.
box = {cur_box[0], cur_box[0] + cur_box[2], cur_box[1],
65 cur_box[1] + cur_box[3]};
68 for (
unsigned int c = 0; c < config.
num_classes; ++c)
70 const float class_prob = det.
confidence * cur_box[5 + c];
76 detections.emplace_back(std::move(det));
84 const std::vector<Detection>& detections)
86 for (
const auto& detection : detections)
88 for (
unsigned int c = 0; c < detection.classes.size(); ++c)
90 if (detection.classes[c] != 0.0f)
92 os << c <<
" " << detection.classes[c] <<
" " << detection.box.xmin
93 <<
" " << detection.box.ymin <<
" " << detection.box.xmax <<
" " 94 << detection.box.ymax << std::endl;
101 const std::vector<float>& detected_boxes) {
103 std::vector<Detection> detections =
104 convert_to_detections(config, detected_boxes);
106 const unsigned int num_detections =
static_cast<unsigned int>(detections.size());
107 for (
unsigned int c = 0; c < config.
num_classes; ++c)
110 std::sort(detections.begin(), detections.begin() +
static_cast<std::ptrdiff_t
>(num_detections),
116 for (
unsigned int d = 0; d < num_detections; ++d)
119 if (detections[d].classes[c] == 0.f)
125 const Box& box1 = detections[d].box;
126 for (
unsigned int b = d + 1; b < num_detections; ++b)
128 const Box& box2 = detections[b].box;
131 detections[b].classes[c] = 0.f;
float confidence
Confidence of detection.
float ymin
Y-pos position of the low left coordinate.
unsigned int num_boxes
Number of detected boxes.
float xmin
X-pos position of the low left coordinate.
float xmax
X-pos position of the top right coordinate.
void print_detection(std::ostream &os, const std::vector< Detection > &detections)
Print identified yolo detections.
std::vector< float > classes
Probability of classes.
Box representation structure.
float ymax
Y-pos position of the top right coordinate.
float iou_threshold
Inclusion threshold for Intersection-Over-Union.
std::vector< Detection > nms(const NMSConfig &config, const std::vector< float > &detected_boxes)
Perform Non-Maxima Supression on a list of given detections.
Non Maxima Suprresion configuration meta-data.
float confidence_threshold
Inclusion confidence threshold for a box.
unsigned int num_classes
Number of classes in the detected boxes.