Add adaptive padding to improve tracking

This commit is contained in:
2026-05-14 22:01:07 +10:00
parent a43a99834a
commit 26f9b07ac1
2 changed files with 91 additions and 19 deletions
+74 -16
View File
@@ -594,18 +594,52 @@ namespace ANSCENTER
}
}
// Step 1: Convert Object[] → TrackerObject[]
// Adaptive padding: tiny objects produce near-zero inter-frame IoU,
// so ByteTrack/IoU matching drops them. We pad small boxes only
// when feeding the tracker; detections[i].box is never modified
// and the un-padded size is recovered for ghost rendering.
// Thresholds are internal by design — no user-facing knob.
constexpr int SMALL_THRESHOLD_PX = 32; // below this: full pad
constexpr int LARGE_THRESHOLD_PX = 96; // at/above this: no pad
constexpr float MAX_PAD_RATIO = 0.5f; // applied to smallest
auto computePadRatio = [](int maxDim) -> float {
if (maxDim >= LARGE_THRESHOLD_PX) return 0.0f;
if (maxDim <= SMALL_THRESHOLD_PX) return MAX_PAD_RATIO;
float t = static_cast<float>(LARGE_THRESHOLD_PX - maxDim) /
static_cast<float>(LARGE_THRESHOLD_PX - SMALL_THRESHOLD_PX);
return MAX_PAD_RATIO * t;
};
auto padBox = [](const cv::Rect& b, float r) -> cv::Rect {
if (r <= 0.0f) return b;
int pw = static_cast<int>(b.width * r);
int ph = static_cast<int>(b.height * r);
return cv::Rect(b.x - pw, b.y - ph,
b.width + 2 * pw, b.height + 2 * ph);
};
// Step 1: Convert Object[] → TrackerObject[] using adaptive padding.
// paddedBoxes[i] mirrors detections[i] so step-3 matching happens
// in padded space without altering the original detection boxes.
std::vector<TrackerObject> trackerInput;
std::vector<cv::Rect> paddedBoxes;
trackerInput.reserve(detections.size());
paddedBoxes.reserve(detections.size());
int paddedCount = 0;
for (const auto& obj : detections) {
int maxDim = std::max(obj.box.width, obj.box.height);
float ratio = computePadRatio(maxDim);
cv::Rect padded = padBox(obj.box, ratio);
paddedBoxes.push_back(padded);
if (ratio > 0.0f) ++paddedCount;
TrackerObject tobj{};
tobj.track_id = 0;
tobj.class_id = obj.classId;
tobj.prob = obj.confidence;
tobj.x = static_cast<float>(obj.box.x);
tobj.y = static_cast<float>(obj.box.y);
tobj.width = static_cast<float>(obj.box.width);
tobj.height = static_cast<float>(obj.box.height);
tobj.x = static_cast<float>(padded.x);
tobj.y = static_cast<float>(padded.y);
tobj.width = static_cast<float>(padded.width);
tobj.height = static_cast<float>(padded.height);
tobj.left = tobj.x;
tobj.top = tobj.y;
tobj.right = tobj.x + tobj.width;
@@ -613,20 +647,26 @@ namespace ANSCENTER
tobj.object_id = camera_id;
trackerInput.push_back(tobj);
}
ANS_DBG("ANSOD_Tracker",
"this=%p cam=%s ApplyTracking dets=%d padded=%d",
(void*)this, camera_id.c_str(),
(int)detections.size(), paddedCount);
// Step 2: Run per-camera tracker
// Step 2: Run per-camera tracker (operates in padded space)
auto trackerOutput = camera._tracker->UpdateTracker(0, trackerInput);
// Step 3: IoU-match tracked objects back to detections, assign trackId
// Step 3: IoU-match tracker output to detections in padded space,
// then assign trackId to the matching detection. detections[i].box
// remains the original un-padded box.
for (const auto& tobj : trackerOutput) {
cv::Rect trkBox(static_cast<int>(tobj.x), static_cast<int>(tobj.y),
static_cast<int>(tobj.width), static_cast<int>(tobj.height));
float bestIoU = 0.f;
int bestIdx = -1;
for (int i = 0; i < static_cast<int>(detections.size()); ++i) {
cv::Rect inter = trkBox & detections[i].box;
for (int i = 0; i < static_cast<int>(paddedBoxes.size()); ++i) {
cv::Rect inter = trkBox & paddedBoxes[i];
float interArea = static_cast<float>(inter.area());
float unionArea = static_cast<float>(trkBox.area() + detections[i].box.area()) - interArea;
float unionArea = static_cast<float>(trkBox.area() + paddedBoxes[i].area()) - interArea;
float iou = (unionArea > 0.f) ? interArea / unionArea : 0.f;
if (iou > bestIoU) {
bestIoU = iou;
@@ -638,15 +678,16 @@ namespace ANSCENTER
}
}
// Step 4: If stabilization enabled, inject ghost objects for unmatched tracker predictions
// Step 4: If stabilization enabled, inject ghost objects for unmatched
// tracker predictions. Ghost.box uses the last-known un-padded size
// from track history at the tracker's predicted center — so users
// never see the inflated padded box that lives inside the tracker.
if (_stabilizationEnabled) {
// Collect trackIds already assigned to real detections
std::set<int> matchedTrackIds;
for (const auto& det : detections) {
if (det.trackId > 0) matchedTrackIds.insert(det.trackId);
}
// Tracker-predicted objects not matched to any detection are "ghosts"
for (const auto& tobj : trackerOutput) {
if (tobj.track_id <= 0) continue;
if (matchedTrackIds.count(tobj.track_id) > 0) continue;
@@ -655,9 +696,26 @@ namespace ANSCENTER
ghost.trackId = tobj.track_id;
ghost.classId = tobj.class_id;
ghost.confidence = tobj.prob;
ghost.box = cv::Rect(
static_cast<int>(tobj.x), static_cast<int>(tobj.y),
static_cast<int>(tobj.width), static_cast<int>(tobj.height));
// Prefer history's un-padded size; fall back to tracker box
// only when no history exists (brand-new track that ghosts
// before any confirmed real detection — rare edge case).
auto histIt = camera._trackHistories.find(tobj.track_id);
if (histIt != camera._trackHistories.end() &&
histIt->second.lastBox.width > 0 &&
histIt->second.lastBox.height > 0) {
const cv::Rect& last = histIt->second.lastBox;
int cx = static_cast<int>(tobj.x + tobj.width * 0.5f);
int cy = static_cast<int>(tobj.y + tobj.height * 0.5f);
ghost.box = cv::Rect(cx - last.width / 2,
cy - last.height / 2,
last.width, last.height);
}
else {
ghost.box = cv::Rect(
static_cast<int>(tobj.x), static_cast<int>(tobj.y),
static_cast<int>(tobj.width), static_cast<int>(tobj.height));
}
ghost.cameraId = camera_id;
TagStabilized(ghost.extraInfo);
detections.push_back(ghost);
@@ -2196,11 +2196,25 @@ int VideoDetectorEngine_CPP(std::string modelFilePath, std::string videoFilePath
int RTMPOSETest_CPP() {
int modelType = 32; // ONNXRTMPOSE (use 33 for RTRTMPOSE)
std::string modelFilePath = "E:\\Programs\\DemoAssets\\ANSAIModels\\PoseEstimationModels\\ANSRTMPOSE.zip";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\rtmpose.mp4";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\ANSOffice.mp4";// "E:\\Programs\\DemoAssets\\Videos\\rtmpose.mp4";
VideoDetectorEngine_CPP(modelFilePath, videoFile, modelType);
return 0;
}
// YOLO OD via the direct cv::Mat / typed-Object path. Use this to verify
// trackIds reach the overlay — the JPEG/JSON variant strips track_id in
// VectorDetectionToJsonString (ANSEngineCommon.cpp:1122-1123), so the
// JSON overlay always shows ":0". This _CPP path reads obj.trackId
// directly so adaptive-padding tracker output is visible.
int YOLO26ODYolo11Test_CPP() {
std::string modelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_VehicleDetection_v2.0.zip";
std::string videoFile = "C:\\ProgramData\\ANSCENTER\\Shared\\road.mp4";
int modelType = 3; // auto-detect (matches YOLO26ODYolo11Test)
int modelDetectionType = 1; // object detection
VideoDetectorEngine_CPP(modelFilePath, videoFile, modelType, modelDetectionType);
return 0;
}
int RTMPOSETest() {
int modelType = 33; // ONNX YOLO (30) RT YOLO (31)
std::string onnxclmodelFilePath = "E:\\Programs\\DemoAssets\\ANSAIModels\\PoseEstimationModels\\ANSRTMPOSE.zip";
@@ -2215,8 +2229,8 @@ int main()
//ALPROD_Test();
//FireNSmokeTest();
//ElasticGPUTest();
RTMPOSETest();
RTMPOSETest_CPP();
//RTMPOSETest();
//YOLO26SEGYolo11Test();
//YOLO26POSEYolo11Test();
//YOLO26CLYolo11Test();