sightloom_tracking/
track.rs1use crate::kalman::KalmanState;
4use sightloom_core::{ClassId, Rect, TrackId};
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum TrackState {
9 New,
11 Tracked,
13 Lost,
15 Removed,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq)]
21pub struct Track {
22 pub id: TrackId,
24 pub state: TrackState,
26 pub class_id: Option<ClassId>,
28 pub kalman: KalmanState,
30 pub score: f32,
32 pub time_since_update: u32,
34 pub hits: u32,
36 pub start_frame: u64,
38 pub frame_id: u64,
40}
41
42impl Track {
43 #[must_use]
45 pub fn new(
46 id: TrackId,
47 rect: Rect,
48 score: f32,
49 class_id: Option<ClassId>,
50 frame_id: u64,
51 ) -> Self {
52 Self {
53 id,
54 state: TrackState::New,
55 class_id,
56 kalman: KalmanState::initiate(rect),
57 score,
58 time_since_update: 0,
59 hits: 1,
60 start_frame: frame_id,
61 frame_id,
62 }
63 }
64
65 #[must_use]
67 pub fn predicted_bbox(self) -> Rect {
68 self.kalman.to_rect()
69 }
70
71 pub fn predict(&mut self) {
73 self.kalman.predict();
74 self.time_since_update = self.time_since_update.saturating_add(1);
75 }
76
77 pub fn update(&mut self, rect: Rect, score: f32, class_id: Option<ClassId>, frame_id: u64) {
79 self.kalman.update(rect);
80 self.score = score;
81 if class_id.is_some() {
82 self.class_id = class_id;
83 }
84 self.time_since_update = 0;
85 self.hits = self.hits.saturating_add(1);
86 self.frame_id = frame_id;
87 self.state = if self.hits >= 2 {
88 TrackState::Tracked
89 } else {
90 TrackState::New
91 };
92 }
93
94 pub fn mark_lost(&mut self) {
96 self.state = TrackState::Lost;
97 }
98
99 pub fn mark_removed(&mut self) {
101 self.state = TrackState::Removed;
102 }
103}