Skip to main content

qualia_vision/tracker/
score_tier_of.rs

1//! ByteTrack-style detection score tier classification.
2//!
3//! High-score detections drive first association + track birth.
4//! Low-score detections only recover unmatched tracks (second association).
5//! Below `low` is rejected for association.
6
7/// Detection confidence band for two-stage association.
8#[repr(u8)]
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum ScoreTier {
11    /// `score >= high` — primary match + may spawn tracks.
12    High = 0,
13    /// `low <= score < high` — secondary match only (no birth).
14    Low = 1,
15    /// `score < low` — ignored for association.
16    Reject = 2,
17}
18
19/// Classify a packed detection score into a ByteTrack tier.
20///
21/// When `high <= low`, the Low band collapses: scores `>= high` are High,
22/// everything else Reject (deterministic fail-closed for misconfigured thresholds).
23#[inline]
24pub fn score_tier_of(score_u16: u16, high_score_u16: u16, low_score_u16: u16) -> ScoreTier {
25    if score_u16 >= high_score_u16 {
26        ScoreTier::High
27    } else if high_score_u16 > low_score_u16 && score_u16 >= low_score_u16 {
28        ScoreTier::Low
29    } else {
30        ScoreTier::Reject
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn tiers_partition_scores() {
40        assert_eq!(score_tier_of(50_000, 32_768, 6_554), ScoreTier::High);
41        assert_eq!(score_tier_of(32_768, 32_768, 6_554), ScoreTier::High);
42        assert_eq!(score_tier_of(10_000, 32_768, 6_554), ScoreTier::Low);
43        assert_eq!(score_tier_of(6_554, 32_768, 6_554), ScoreTier::Low);
44        assert_eq!(score_tier_of(100, 32_768, 6_554), ScoreTier::Reject);
45    }
46
47    #[test]
48    fn collapsed_thresholds_reject_low_band() {
49        assert_eq!(score_tier_of(40_000, 10_000, 10_000), ScoreTier::High);
50        assert_eq!(score_tier_of(9_999, 10_000, 10_000), ScoreTier::Reject);
51    }
52}