1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| #ifndef MEDIAPIPE_CALCULATORS_DETECTION_NMS_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_DETECTION_NMS_CALCULATOR_H_
#include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/formats/detection.pb.h"
namespace mediapipe {
class NMSCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("DETECTIONS").Set<std::vector<Detection>>(); cc->Outputs().Tag("DETECTIONS").Set<std::vector<Detection>>(); cc->Options<NMSOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { const auto& options = cc->Options<NMSOptions>(); iou_threshold_ = options.iou_threshold(); max_detections_ = options.max_detections(); sort_by_ = options.sort_by(); LOG(INFO) << "NMSCalculator initialized: " << "iou_threshold=" << iou_threshold_ << ", max_detections=" << max_detections_; return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { if (cc->Inputs().Tag("DETECTIONS").IsEmpty()) { return absl::OkStatus(); }
const auto& detections = cc->Inputs().Tag("DETECTIONS").Get<std::vector<Detection>>();
if (detections.empty()) { return absl::OkStatus(); }
std::vector<int> indices(detections.size()); std::iota(indices.begin(), indices.end(), 0); switch (sort_by_) { case NMSOptions::SCORE: std::sort(indices.begin(), indices.end(), [&detections](int a, int b) { return detections[a].score() > detections[b].score(); }); break; case NMSOptions::AREA: std::sort(indices.begin(), indices.end(), [&detections](int a, int b) { float area_a = (detections[a].xmax() - detections[a].xmin()) * (detections[a].ymax() - detections[a].ymin()); float area_b = (detections[b].xmax() - detections[b].xmin()) * (detections[b].ymax() - detections[b].ymin()); return area_a > area_b; }); break; case NMSOptions::NONE: break; }
std::vector<bool> suppressed(detections.size(), false); std::vector<Detection> result;
for (int idx : indices) { if (suppressed[idx]) continue; result.push_back(detections[idx]); if (result.size() >= max_detections_) { break; } for (int j : indices) { if (suppressed[j]) continue; float iou = ComputeIOU(detections[idx], detections[j]); if (iou > iou_threshold_) { suppressed[j] = true; } } }
LOG(INFO) << "NMS: " << detections.size() << " → " << result.size();
cc->Outputs().Tag("DETECTIONS").AddPacket( MakePacket<std::vector<Detection>>(result).At(cc->InputTimestamp()));
return absl::OkStatus(); }
private: float iou_threshold_ = 0.45f; int max_detections_ = 100; NMSOptions::SortBy sort_by_ = NMSOptions::SCORE; float ComputeIOU(const Detection& a, const Detection& b) { float x1 = std::max(a.xmin(), b.xmin()); float y1 = std::max(a.ymin(), b.ymin()); float x2 = std::min(a.xmax(), b.xmax()); float y2 = std::min(a.ymax(), b.ymax()); float intersection = std::max(0.0f, x2 - x1) * std::max(0.0f, y2 - y1); float area_a = (a.xmax() - a.xmin()) * (a.ymax() - a.ymin()); float area_b = (b.xmax() - b.xmin()) * (b.ymax() - b.ymin()); float union_area = area_a + area_b - intersection; if (union_area == 0) return 0.0f; return intersection / union_area; } float SoftNMS(const std::vector<Detection>& detections, float iou_threshold, float sigma = 0.5, float score_threshold = 0.5) { std::vector<float> scores; for (const auto& det : detections) { scores.push_back(det.score()); } std::vector<bool> suppressed(detections.size(), false); std::vector<float> weights = scores; for (int i = 0; i < detections.size(); ++i) { if (suppressed[i]) continue; for (int j = 0; j < detections.size(); ++j) { if (i == j || suppressed[j]) continue; float iou = ComputeIOU(detections[i], detections[j]); if (iou > iou_threshold) { float weight = exp(-(iou * iou) / sigma); weights[j] *= weight; } } } for (int i = 0; i < detections.size(); ++i) { detections[i].set_score(std::min(detections[i].score(), weights[i])); } return 0; } };
REGISTER_CALCULATOR(NMSCalculator);
}
#endif
|