|
| 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 | +} |
0 commit comments