Files
ANSCustomModels/ANSCustomFireNSmokeDetection/vendor/core/detection.hpp
T

78 lines
2.8 KiB
C++
Raw Normal View History

#pragma once
// core/detection — the detection currency of the pipeline.
//
// Coordinates are NORMALIZED [0,1] of the source frame, always. ANSCORE mixed
// pixel- and crop-relative coordinates and the ALPRChecker false-merge bug
// (§6.3) was the price; one coordinate space ends that class of bug. Pixel
// mapping happens only at the edges (pre-processing in, overlay/evidence out).
#include <cstdint>
#include <string>
#include <vector>
namespace ansmind {
struct RectF {
float x = 0, y = 0, w = 0, h = 0; // top-left + size, normalized
float area() const { return w > 0 && h > 0 ? w * h : 0.f; }
float iou(const RectF& o) const {
const float x1 = x > o.x ? x : o.x;
const float y1 = y > o.y ? y : o.y;
const float x2 = (x + w) < (o.x + o.w) ? (x + w) : (o.x + o.w);
const float y2 = (y + h) < (o.y + o.h) ? (y + h) : (o.y + o.h);
if (x2 <= x1 || y2 <= y1) return 0.f;
const float inter = (x2 - x1) * (y2 - y1);
return inter / (area() + o.area() - inter);
}
RectF clamped() const {
RectF r = *this;
if (r.x < 0) { r.w += r.x; r.x = 0; }
if (r.y < 0) { r.h += r.y; r.y = 0; }
if (r.x + r.w > 1) r.w = 1 - r.x;
if (r.y + r.h > 1) r.h = 1 - r.y;
if (r.w < 0) r.w = 0;
if (r.h < 0) r.h = 0;
return r;
}
};
// A resolved attribute attached by the TrackResolver (§6.2) — identity,
// plate, helmet… Value "unknown" is first-class (the stranger case).
struct DetAttr {
std::string key;
std::string value;
float confidence = 0;
};
struct Detection {
RectF box;
float confidence = 0;
int classId = -1;
std::string className;
std::string nodeId; // which graph node produced it
uint64_t trackId = 0; // 0 = untracked
int parentIndex = -1; // RegionNode: index of the parent detection
std::vector<DetAttr> attrs; // TrackResolver output; rules bind to these
// Landmarks in NORMALIZED frame coords (x0,y0,…x4,y4 for SCRFD's 5-point
// faces; empty when the detector has none). The embed node aligns on
// these — the ANSFR accuracy lever.
std::vector<float> kps;
// Per-keypoint confidence, parallel to kps (one per x,y pair; empty when
// the detector gives none — SCRFD's five landmarks carry no score). Pose
// models (YOLOv8-pose, RF-DETR keypoints) fill it: consumers hide a joint
// the model could not find rather than drawing it at a guessed spot.
std::vector<float> kpConf;
// Instance mask, CROPPED TO THE BOX at the seg head's proto resolution
// (0/255 bytes, row-major maskW×maskH; empty for plain detectors).
// Deliberately small — a proto crop is ≤160px per side — consumers
// upscale to the box when they need pixels.
std::vector<uint8_t> mask;
uint16_t maskW = 0, maskH = 0;
std::vector<float> embedding; // engine-internal transient (embed nodes)
};
using DetectionSet = std::vector<Detection>;
} // namespace ansmind