use crate::kalman::KalmanState;
use sightloom_core::{ClassId, Rect, TrackId};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TrackState {
New,
Tracked,
Lost,
Removed,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Track {
pub id: TrackId,
pub state: TrackState,
pub class_id: Option<ClassId>,
pub kalman: KalmanState,
pub score: f32,
pub time_since_update: u32,
pub hits: u32,
pub start_frame: u64,
pub frame_id: u64,
}
impl Track {
#[must_use]
pub fn new(
id: TrackId,
rect: Rect,
score: f32,
class_id: Option<ClassId>,
frame_id: u64,
) -> Self {
Self {
id,
state: TrackState::New,
class_id,
kalman: KalmanState::initiate(rect),
score,
time_since_update: 0,
hits: 1,
start_frame: frame_id,
frame_id,
}
}
#[must_use]
pub fn predicted_bbox(self) -> Rect {
self.kalman.to_rect()
}
pub fn predict(&mut self) {
self.kalman.predict();
self.time_since_update = self.time_since_update.saturating_add(1);
}
pub fn update(&mut self, rect: Rect, score: f32, class_id: Option<ClassId>, frame_id: u64) {
self.kalman.update(rect);
self.score = score;
if class_id.is_some() {
self.class_id = class_id;
}
self.time_since_update = 0;
self.hits = self.hits.saturating_add(1);
self.frame_id = frame_id;
self.state = if self.hits >= 2 {
TrackState::Tracked
} else {
TrackState::New
};
}
pub fn mark_lost(&mut self) {
self.state = TrackState::Lost;
}
pub fn mark_removed(&mut self) {
self.state = TrackState::Removed;
}
}