Skip to content

Commit 6c337b2

Browse files
authored
Add ByteTracker (#153)
1 parent 2e7af17 commit 6c337b2

17 files changed

Lines changed: 2842 additions & 32 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "usls"
33
edition = "2021"
4-
version = "0.1.5"
4+
version = "0.1.6"
55
rust-version = "1.85"
66
description = "A Rust library integrated with ONNXRuntime, providing a collection of ML models."
77
repository = "https://github.com/jamjamjon/usls"

examples/imshow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() -> anyhow::Result<()> {
3030
viewer.imshow(&images[0])?;
3131

3232
// check out key event
33-
if let Some(key) = viewer.wait_key(1) {
33+
if let Some(key) = viewer.wait_key(1000) {
3434
if key == usls::Key::Escape {
3535
break;
3636
}

examples/yolo-track/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Quick Start
2+
3+
```shell
4+
cargo run -r -F video -F cuda --example yolo-track -- --device cuda --source 'video.mp4' --classes 0
5+
```

examples/yolo-track/main.rs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
use anyhow::Result;
2+
3+
use usls::{
4+
models::YOLO, Annotator, ByteTracker, Config, DataLoader, Style, Viewer, SKELETON_COCO_19,
5+
SKELETON_COLOR_COCO_19,
6+
};
7+
8+
#[derive(argh::FromArgs, Debug)]
9+
/// YOLO Example
10+
struct Args {
11+
/// model file(.onnx)
12+
#[argh(option)]
13+
model: Option<String>,
14+
15+
/// source: image, image folder, video stream
16+
#[argh(option, default = "String::from(\"./assets/bus.jpg\")")]
17+
source: String,
18+
19+
/// model dtype
20+
#[argh(option, default = "String::from(\"auto\")")]
21+
dtype: String,
22+
23+
/// task: det, seg, pose, classify, obb
24+
#[argh(option, default = "String::from(\"det\")")]
25+
task: String,
26+
27+
/// version
28+
#[argh(option, default = "8.0")]
29+
ver: f32,
30+
31+
/// device: cuda, cpu, mps
32+
#[argh(option, default = "String::from(\"cpu:0\")")]
33+
device: String,
34+
35+
/// scale: n, s, m, l, x
36+
#[argh(option, default = "String::from(\"m\")")]
37+
scale: String,
38+
39+
/// batch size
40+
#[argh(option, default = "1")]
41+
batch_size: usize,
42+
43+
/// min batch size: For TensorRT
44+
#[argh(option, default = "1")]
45+
min_batch_size: usize,
46+
47+
/// max Batch size: For TensorRT
48+
#[argh(option, default = "4")]
49+
max_batch_size: usize,
50+
51+
/// min image width: For TensorRT
52+
#[argh(option, default = "224")]
53+
min_image_width: isize,
54+
55+
/// image width: For TensorRT
56+
#[argh(option, default = "640")]
57+
image_width: isize,
58+
59+
/// max image width: For TensorRT
60+
#[argh(option, default = "1280")]
61+
max_image_width: isize,
62+
63+
/// min image height: For TensorRT
64+
#[argh(option, default = "224")]
65+
min_image_height: isize,
66+
67+
/// image height: For TensorRT
68+
#[argh(option, default = "640")]
69+
image_height: isize,
70+
71+
/// max image height: For TensorRT
72+
#[argh(option, default = "1280")]
73+
max_image_height: isize,
74+
75+
/// confidences
76+
#[argh(option)]
77+
confs: Vec<f32>,
78+
79+
/// retain classes
80+
#[argh(option)]
81+
classes: Vec<usize>,
82+
}
83+
84+
fn main() -> Result<()> {
85+
tracing_subscriber::fmt()
86+
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
87+
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339())
88+
.init();
89+
let args: Args = argh::from_env();
90+
let config = Config::yolo()
91+
.with_model_file(&args.model.unwrap_or_default())
92+
.with_task(args.task.parse()?)
93+
.with_version(args.ver.try_into()?)
94+
.with_scale(args.scale.parse()?)
95+
.with_model_dtype(args.dtype.parse()?)
96+
.with_model_device(args.device.parse()?)
97+
.with_model_ixx(
98+
0,
99+
0,
100+
(args.min_batch_size, args.batch_size, args.max_batch_size).into(),
101+
)
102+
.with_model_ixx(
103+
0,
104+
2,
105+
(
106+
args.min_image_height,
107+
args.image_height,
108+
args.max_image_height,
109+
)
110+
.into(),
111+
)
112+
.with_model_ixx(
113+
0,
114+
3,
115+
(args.min_image_width, args.image_width, args.max_image_width).into(),
116+
)
117+
.with_class_confs(if args.confs.is_empty() {
118+
&[0.25]
119+
} else {
120+
&args.confs
121+
})
122+
.retain_classes(&args.classes);
123+
124+
// build model
125+
let mut model = YOLO::new(config.commit()?)?;
126+
127+
// build dataloader
128+
let dl = DataLoader::new(&args.source)?
129+
.with_batch(model.batch() as _)
130+
.build()?;
131+
132+
// build annotator
133+
let annotator = Annotator::default()
134+
.with_obb_style(Style::obb().with_draw_fill(true))
135+
.with_hbb_style(
136+
Style::hbb()
137+
.with_draw_fill(true)
138+
.with_palette(&usls::Color::palette_coco_80()),
139+
)
140+
.with_keypoint_style(
141+
Style::keypoint()
142+
.with_skeleton((SKELETON_COCO_19, SKELETON_COLOR_COCO_19).into())
143+
.show_confidence(false)
144+
.show_id(true)
145+
.show_name(false),
146+
)
147+
.with_mask_style(Style::mask().with_draw_mask_polygon_largest(true));
148+
149+
// build viewer and tracker
150+
let mut viewer = Viewer::default().with_window_scale(1.);
151+
let mut tracker = ByteTracker::default().with_max_age(60);
152+
153+
// run & annotate
154+
for xs in &dl {
155+
// check out window
156+
if viewer.is_window_exist() && !viewer.is_window_open() {
157+
break;
158+
}
159+
160+
let ys = model.forward(&xs)?;
161+
// println!("ys: {:?}", ys);
162+
163+
if let Some(hbbs) = ys[0].hbbs() {
164+
let tracked_hbbs = tracker.update(hbbs)?;
165+
println!("tracked_hbbs: {:?}", tracked_hbbs);
166+
167+
// annnotate
168+
let image_annotated = annotator.annotate(&xs[0], &tracked_hbbs)?;
169+
170+
// imshow
171+
viewer.imshow(&image_annotated)?;
172+
} else {
173+
continue;
174+
}
175+
176+
// check out key event
177+
if let Some(key) = viewer.wait_key(10) {
178+
if key == usls::Key::Escape {
179+
break;
180+
}
181+
}
182+
}
183+
184+
usls::perf(false);
185+
186+
Ok(())
187+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ pub mod core;
5454
pub mod models;
5555
#[macro_use]
5656
mod results;
57+
pub mod mot;
5758
pub mod viz;
5859

5960
pub use core::*;
6061
#[cfg(feature = "viewer")]
6162
pub use minifb::Key;
6263
#[cfg(any(feature = "ort-download-binaries", feature = "ort-load-dynamic"))]
6364
pub use models::*;
65+
pub use mot::*;
6466
pub use results::*;
6567
pub use viz::*;

0 commit comments

Comments
 (0)