|
| 1 | +use anyhow::Result; |
| 2 | +use usls::{models::RTDETR, Annotator, Config, DataLoader}; |
| 3 | + |
| 4 | +#[derive(argh::FromArgs)] |
| 5 | +/// Example |
| 6 | +struct Args { |
| 7 | + /// model file |
| 8 | + #[argh(option)] |
| 9 | + model: Option<String>, |
| 10 | + |
| 11 | + /// device |
| 12 | + #[argh(option, default = "String::from(\"cpu:0\")")] |
| 13 | + device: String, |
| 14 | + |
| 15 | + /// dtype |
| 16 | + #[argh(option, default = "String::from(\"fp16\")")] |
| 17 | + dtype: String, |
| 18 | +} |
| 19 | + |
| 20 | +fn main() -> Result<()> { |
| 21 | + tracing_subscriber::fmt() |
| 22 | + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 23 | + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339()) |
| 24 | + .init(); |
| 25 | + |
| 26 | + let args: Args = argh::from_env(); |
| 27 | + |
| 28 | + // config |
| 29 | + let config = match &args.model { |
| 30 | + Some(m) => Config::deimv2().with_model_file(m), |
| 31 | + None => Config::deim_v2_n_coco(), |
| 32 | + } |
| 33 | + .with_model_dtype(args.dtype.parse()?) |
| 34 | + .with_device_all(args.device.parse()?) |
| 35 | + .commit()?; |
| 36 | + let mut model = RTDETR::new(config)?; |
| 37 | + |
| 38 | + // load |
| 39 | + let xs = DataLoader::try_read_n(&["./assets/bus.jpg"])?; |
| 40 | + |
| 41 | + // run |
| 42 | + let ys = model.forward(&xs)?; |
| 43 | + println!("{:?}", ys); |
| 44 | + |
| 45 | + // annotate |
| 46 | + let annotator = Annotator::default(); |
| 47 | + for (x, y) in xs.iter().zip(ys.iter()) { |
| 48 | + annotator.annotate(x, y)?.save(format!( |
| 49 | + "{}.jpg", |
| 50 | + usls::Dir::Current |
| 51 | + .base_dir_with_subs(&["runs", model.spec()])? |
| 52 | + .join(usls::timestamp(None)) |
| 53 | + .display(), |
| 54 | + ))?; |
| 55 | + } |
| 56 | + |
| 57 | + usls::perf(false); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
0 commit comments