From 52ec13a8e33ea3e8beec168e6116ecc2add68280 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Thu, 14 May 2026 19:17:09 +1000 Subject: [PATCH] improve pose --- modules/ANSODEngine/ANSONNXRTMPOSE.cpp | 48 +++++++++++++++++++ modules/ANSODEngine/ANSRTRTMPOSE.cpp | 48 +++++++++++++++++++ .../ANSODEngine-UnitTest.cpp | 4 +- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/modules/ANSODEngine/ANSONNXRTMPOSE.cpp b/modules/ANSODEngine/ANSONNXRTMPOSE.cpp index e5548d5..ae4ab88 100644 --- a/modules/ANSODEngine/ANSONNXRTMPOSE.cpp +++ b/modules/ANSODEngine/ANSONNXRTMPOSE.cpp @@ -6,6 +6,43 @@ #include #include +namespace { + // ── IoU dedup for end-to-end YOLO person detector output ────────────── + // + // Ultralytics E2E ONNX exports bake NMS into the graph, but its IoU + // threshold is set at export time (default ≈ 0.7) and we can't override + // it from outside. For a top-down pose pipeline that's too lax — pairs + // of overlapping bboxes survive and we end up running RTMPose twice on + // the same person. + // + // This filter walks the detector's output (already sorted by confidence + // in the E2E format), keeping each box only if its IoU with every + // already-kept box is below kPersonDedupIoU. O(N²) is fine because N is + // capped at 300 and typically <10 in practice. + constexpr float kPersonDedupIoU = 0.45f; + + inline float boxIoU(const cv::Rect& a, const cv::Rect& b) { + const cv::Rect inter = a & b; + if (inter.area() <= 0) return 0.f; + const int uni = a.area() + b.area() - inter.area(); + return uni > 0 ? static_cast(inter.area()) / static_cast(uni) : 0.f; + } + + std::vector dedupByIoU( + const std::vector& persons, float iouThr) { + std::vector kept; + kept.reserve(persons.size()); + for (const auto& p : persons) { + bool isDup = false; + for (const auto& k : kept) { + if (boxIoU(p.box, k.box) > iouThr) { isDup = true; break; } + } + if (!isDup) kept.push_back(p); + } + return kept; + } +} // anonymous namespace + namespace ANSCENTER { // ==================================================================== @@ -690,6 +727,17 @@ namespace ANSCENTER { return {}; } + // IoU dedup — Ultralytics E2E NMS in person.onnx is too lax + // (training-time IoU≈0.7 default), letting near-duplicate + // boxes through. Drop duplicates BEFORE running pose so we + // don't pay for two RTMPose inferences on the same subject. + const size_t beforeDedup = persons.size(); + persons = dedupByIoU(persons, kPersonDedupIoU); + if (persons.size() != beforeDedup) { + ANS_DBG("ANSONNXRTMPOSE", "DetectObjects: dedup %zu -> %zu (IoU>%g) cam='%s'", + beforeDedup, persons.size(), kPersonDedupIoU, camera_id.c_str()); + } + std::vector out; out.reserve(persons.size()); for (const auto& person : persons) { diff --git a/modules/ANSODEngine/ANSRTRTMPOSE.cpp b/modules/ANSODEngine/ANSRTRTMPOSE.cpp index 5939481..1bf28d4 100644 --- a/modules/ANSODEngine/ANSRTRTMPOSE.cpp +++ b/modules/ANSODEngine/ANSRTRTMPOSE.cpp @@ -6,6 +6,43 @@ #include #include +namespace { + // ── IoU dedup for end-to-end YOLO person detector output ────────────── + // + // Ultralytics E2E ONNX exports bake NMS into the graph, but its IoU + // threshold is set at export time (default ≈ 0.7) and we can't override + // it from outside. For a top-down pose pipeline that's too lax — pairs + // of overlapping bboxes survive and we end up running RTMPose twice on + // the same person. + // + // This filter walks the detector's output (already sorted by confidence + // in the E2E format), keeping each box only if its IoU with every + // already-kept box is below kPersonDedupIoU. O(N²) is fine because N is + // capped at 300 and typically <10 in practice. + constexpr float kPersonDedupIoU = 0.45f; + + inline float boxIoU(const cv::Rect& a, const cv::Rect& b) { + const cv::Rect inter = a & b; + if (inter.area() <= 0) return 0.f; + const int uni = a.area() + b.area() - inter.area(); + return uni > 0 ? static_cast(inter.area()) / static_cast(uni) : 0.f; + } + + std::vector dedupByIoU( + const std::vector& persons, float iouThr) { + std::vector kept; + kept.reserve(persons.size()); + for (const auto& p : persons) { + bool isDup = false; + for (const auto& k : kept) { + if (boxIoU(p.box, k.box) > iouThr) { isDup = true; break; } + } + if (!isDup) kept.push_back(p); + } + return kept; + } +} // anonymous namespace + namespace ANSCENTER { // ==================================================================== @@ -411,6 +448,17 @@ namespace ANSCENTER { return {}; } + // IoU dedup — Ultralytics E2E NMS in person.onnx is too lax + // (training-time IoU≈0.7 default), letting near-duplicate + // boxes through. Drop duplicates BEFORE running pose so we + // don't pay for two RTMPose inferences on the same subject. + const size_t beforeDedup = persons.size(); + persons = dedupByIoU(persons, kPersonDedupIoU); + if (persons.size() != beforeDedup) { + ANS_DBG("ANSRTRTMPOSE", "RunInference: dedup %zu -> %zu (IoU>%g) cam='%s'", + beforeDedup, persons.size(), kPersonDedupIoU, camera_id.c_str()); + } + std::vector results; results.reserve(persons.size()); for (const auto& person : persons) { diff --git a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp index b8ccc98..ea0b817 100644 --- a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp +++ b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp @@ -1997,9 +1997,9 @@ int FallDetectorTest() { } int RTMPOSETest() { - int modelType = 33; // ONNX YOLO (30) RT YOLO (31) + int modelType = 32; // ONNX YOLO (30) RT YOLO (31) std::string onnxclmodelFilePath = "E:\\Programs\\DemoAssets\\ANSAIModels\\PoseEstimationModels\\ANSRTMPOSE.zip"; - std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\train.mp4"; + std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\rtmpose.mp4";//"E:\\Programs\\DemoAssets\\Videos\\train.mp4"; VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx return 0; }