1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
// SPDX-License-Identifier: Apache-2.0
//! Multi-object tracking for EdgeFirst inference pipelines.
//!
//! This crate provides the [`Tracker`] trait and a concrete implementation,
//! [`ByteTrack`], which associates per-frame detections
//! into persistent tracks using two-pass IoU matching and Kalman-smoothed
//! trajectory prediction.
//!
//! # Quick start
//!
//! ```rust
//! use edgefirst_tracker::{bytetrack::ByteTrackBuilder, Tracker, MockDetection};
//!
//! let mut tracker = ByteTrackBuilder::new().build();
//!
//! // Timestamps are nanoseconds; here we use small integers for clarity.
//! let frame1 = vec![MockDetection::new([0.1, 0.1, 0.5, 0.5], 0.9, 0)];
//! let infos = tracker.update(&frame1, 1_000_000);
//!
//! // Each element corresponds to the detection at the same index; `Some` means
//! // the detection was either matched to an existing track or started a new one.
//! if let Some(info) = infos[0] {
//! println!("track {} created at t={}", info.uuid, info.created);
//! }
//! ```
//!
//! # Terminology
//!
//! - **Tracklet** — an internal state machine (Kalman filter + metadata) that
//! follows one object across frames.
//! - **Detection** — a single-frame bounding box with confidence score and
//! class label, produced by a model decoder.
//! - **Track** — the public view of a tracklet: a [`TrackInfo`] value returned
//! by [`Tracker::update`] or [`Tracker::get_active_tracks`].
//!
//! # Architecture
//!
//! See [`crates/tracker/ARCHITECTURE.md`](https://github.com/EdgeFirstAI/hal/blob/main/crates/tracker/ARCHITECTURE.md)
//! for the full design description, Mermaid class diagram, tracking flow, and
//! Kalman filter state documentation.
use Debug;
pub use Uuid;
pub use ;
/// A bounding box produced by a detection model, suitable for use with a [`Tracker`].
///
/// Implement this trait on your model's output type to feed it directly into
/// [`ByteTrack`] without copying. The tracker crate has
/// no dependency on `edgefirst-decoder`; any type that can report an XYXY box,
/// a confidence score, and a class label satisfies this contract.
///
/// # Coordinates
///
/// `bbox()` must return `[xmin, ymin, xmax, ymax]` in normalised `[0, 1]`
/// coordinates or in pixel coordinates — the tracker does not prescribe units,
/// but all detections passed to a single [`Tracker::update`] call must use the
/// same coordinate space.
///
/// # Example
///
/// ```rust
/// use edgefirst_tracker::DetectionBox;
///
/// #[derive(Debug, Clone)]
/// struct MyDetection { bbox: [f32; 4], score: f32, label: usize }
///
/// impl DetectionBox for MyDetection {
/// fn bbox(&self) -> [f32; 4] { self.bbox }
/// fn score(&self) -> f32 { self.score }
/// fn label(&self) -> usize { self.label }
/// }
/// ```
/// A minimal detection box for unit tests and doc examples.
///
/// This type is **not** intended for production use. It is `#[doc(hidden)]` in
/// the public docs but available as `edgefirst_tracker::MockDetection` so test
/// code in downstream crates does not need to define its own dummy type.
/// Per-track metadata returned by [`Tracker::update`] and [`Tracker::get_active_tracks`].
///
/// Every active track carries a stable [`Uuid`] assigned at track creation and
/// preserved until the track is deleted. Callers can use the UUID to correlate
/// tracks across frames or to drive per-track rendering (e.g. per-UUID colour
/// assignment in `ColorMode::Track`).
///
/// # Timestamps
///
/// `created` and `last_updated` store the raw `timestamp` argument passed to
/// [`Tracker::update`] at the corresponding event. The tracker does not
/// interpret these values — they are caller-supplied nanoseconds in typical
/// usage, but any monotonically increasing unit works.
/// A [`TrackInfo`] paired with the most recent raw detection box for that track.
///
/// Returned by [`Tracker::get_active_tracks`] for callers that need both the
/// Kalman-smoothed position (in `info.tracked_location`) and the unsmoothed,
/// model-output bounding box (`last_box`) — for example, to draw the exact
/// predicted anchor alongside the smoothed track centre.
/// Multi-object tracker interface.
///
/// Implementations associate a stream of per-frame detection boxes into
/// persistent tracks. The tracker owns all internal state (tracklet filters,
/// timestamps, IDs) and is mutable — `update` takes `&mut self`.
///
/// # Contract
///
/// - The returned `Vec` from `update` is parallel to the input `boxes` slice:
/// `result[i]` is `Some(info)` when detection `i` was matched to a track or
/// spawned a new one, and `None` when it was too low-confidence to initiate or
/// continue a track.
/// - `get_active_tracks` returns every tracklet that has not yet expired,
/// including those not matched in the most recent frame (they remain alive
/// until `extra_lifespan` elapses without a match).
/// - Callers must not share a single `Tracker` across concurrent threads without
/// external synchronization — `update` takes `&mut self`.