|
| 1 | +use anyhow::Result; |
| 2 | +use usls::{ |
| 3 | + models::{RTMPose, YOLO}, |
| 4 | + Annotator, Config, DataLoader, Scale, Style, SKELETON_COCO_19, SKELETON_COLOR_COCO_19, |
| 5 | + SKELETON_COLOR_HALPE_27, SKELETON_HALPE_27, |
| 6 | +}; |
| 7 | + |
| 8 | +#[derive(argh::FromArgs)] |
| 9 | +/// Example |
| 10 | +struct Args { |
| 11 | + /// source: image, image folder, video stream |
| 12 | + #[argh(option, default = "String::from(\"./assets/bus.jpg\")")] |
| 13 | + source: String, |
| 14 | + |
| 15 | + /// device: cuda:0, cpu:0, ... |
| 16 | + #[argh(option, default = "String::from(\"cpu:0\")")] |
| 17 | + device: String, |
| 18 | + |
| 19 | + /// scale: t, s, m, l, x |
| 20 | + #[argh(option, default = "String::from(\"t\")")] |
| 21 | + scale: String, |
| 22 | + |
| 23 | + /// dtype: fp16, q8, q4, q4f16, ... |
| 24 | + #[argh(option, default = "String::from(\"auto\")")] |
| 25 | + dtype: String, |
| 26 | + |
| 27 | + /// is coco 17 keypoints or halpe 26 keypoints |
| 28 | + #[argh(option, default = "true")] |
| 29 | + is_coco: bool, |
| 30 | +} |
| 31 | + |
| 32 | +fn main() -> Result<()> { |
| 33 | + tracing_subscriber::fmt() |
| 34 | + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 35 | + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339()) |
| 36 | + .init(); |
| 37 | + let args: Args = argh::from_env(); |
| 38 | + |
| 39 | + // build YOLOv8 |
| 40 | + let yolo_config = Config::yolo_detect() |
| 41 | + .with_scale(Scale::N) |
| 42 | + .with_version(8.into()) |
| 43 | + .with_model_device(args.device.parse()?) |
| 44 | + .retain_classes(&[0]) // keep person class only |
| 45 | + .with_class_confs(&[0.5]) |
| 46 | + .commit()?; |
| 47 | + let mut yolo = YOLO::new(yolo_config)?; |
| 48 | + |
| 49 | + // build RTMPose |
| 50 | + let config = match args.scale.as_str() { |
| 51 | + "t" => match args.is_coco { |
| 52 | + true => Config::rtmpose_17_t(), |
| 53 | + false => Config::rtmpose_26_t(), |
| 54 | + }, |
| 55 | + "s" => match args.is_coco { |
| 56 | + true => Config::rtmpose_17_s(), |
| 57 | + false => Config::rtmpose_26_s(), |
| 58 | + }, |
| 59 | + "m" => match args.is_coco { |
| 60 | + true => Config::rtmpose_17_m(), |
| 61 | + false => Config::rtmpose_26_m(), |
| 62 | + }, |
| 63 | + "l" => match args.is_coco { |
| 64 | + true => Config::rtmpose_17_l(), |
| 65 | + false => Config::rtmpose_26_l(), |
| 66 | + }, |
| 67 | + "x" => match args.is_coco { |
| 68 | + true => Config::rtmpose_17_x(), |
| 69 | + false => Config::rtmpose_26_x(), |
| 70 | + }, |
| 71 | + _ => todo!(), |
| 72 | + } |
| 73 | + .with_model_dtype(args.dtype.parse()?) |
| 74 | + .with_model_device(args.device.parse()?) |
| 75 | + .commit()?; |
| 76 | + let mut rtmpose = RTMPose::new(config)?; |
| 77 | + |
| 78 | + // build annotator |
| 79 | + let annotator = Annotator::default() |
| 80 | + .with_hbb_style(Style::hbb().with_draw_fill(true)) |
| 81 | + .with_keypoint_style( |
| 82 | + Style::keypoint() |
| 83 | + .with_radius(4) |
| 84 | + .with_skeleton(if args.is_coco { |
| 85 | + (SKELETON_COCO_19, SKELETON_COLOR_COCO_19).into() |
| 86 | + } else { |
| 87 | + (SKELETON_HALPE_27, SKELETON_COLOR_HALPE_27).into() |
| 88 | + }) |
| 89 | + .show_id(false) |
| 90 | + .show_confidence(false) |
| 91 | + .show_name(false), |
| 92 | + ); |
| 93 | + |
| 94 | + // build dataloader |
| 95 | + let dl = DataLoader::new(&args.source)?.with_batch(1).build()?; |
| 96 | + |
| 97 | + // iterate |
| 98 | + for xs in &dl { |
| 99 | + // YOLO infer |
| 100 | + let ys_det = yolo.forward(&xs)?; |
| 101 | + |
| 102 | + // RTMPose infer |
| 103 | + for (x, y_det) in xs.iter().zip(ys_det.iter()) { |
| 104 | + let y = rtmpose.forward(x, y_det.hbbs())?; |
| 105 | + |
| 106 | + // Annotate |
| 107 | + annotator.annotate(x, &y)?.save(format!( |
| 108 | + "{}.jpg", |
| 109 | + usls::Dir::Current |
| 110 | + .base_dir_with_subs(&["runs", rtmpose.spec()])? |
| 111 | + .join(usls::timestamp(None)) |
| 112 | + .display(), |
| 113 | + ))? |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // summary |
| 118 | + yolo.summary(); |
| 119 | + rtmpose.summary(); |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
0 commit comments