[Example] YOLO-Series(v5-11) ONNXRuntime Rust (#17311)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Jamjamjon 2024-11-02 20:06:07 +08:00 committed by GitHub
parent d28caa9a58
commit f95dc37311
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 362 additions and 29 deletions

View file

@ -12,7 +12,7 @@ clap = { version = "4.2.4", features = ["derive"] }
image = { version = "0.25.2"}
imageproc = { version = "0.25.0"}
ndarray = { version = "0.16" }
ort = { version = "2.0.0-rc.5", features = ["cuda", "tensorrt"]}
ort = { version = "2.0.0-rc.5", features = ["cuda", "tensorrt", "load-dynamic", "copy-dylibs", "half"]}
rusttype = { version = "0.9.3" }
anyhow = { version = "1.0.75" }
regex = { version = "1.5.4" }

View file

@ -7,7 +7,7 @@ This repository provides a Rust demo for performing YOLOv8 tasks like `Classific
- Add YOLOv8-OBB demo
- Update ONNXRuntime to 1.19.x
Newly updated YOLOv8 example code is located in this repository (https://github.com/jamjamjon/usls/tree/main/examples/yolo)
Newly updated YOLOv8 example code is located in [this repository](https://github.com/jamjamjon/usls/tree/main/examples/yolo)
## Features
@ -22,25 +22,16 @@ Newly updated YOLOv8 example code is located in this repository (https://github.
Please follow the Rust official installation. (https://www.rust-lang.org/tools/install)
### 2. Install ONNXRuntime
### 2. ONNXRuntime Linking
This repository use `ort` crate, which is ONNXRuntime wrapper for Rust. (https://docs.rs/ort/latest/ort/)
- #### For detailed setup instructions, refer to the [ORT documentation](https://ort.pyke.io/setup/linking).
You can follow the instruction with `ort` doc or simply do this:
- step1: Download ONNXRuntime(https://github.com/microsoft/onnxruntime/releases)
- setp2: Set environment variable `PATH` for linking.
On ubuntu, You can do like this:
```bash
vim ~/.bashrc
# Add the path of ONNXRUntime lib
export LD_LIBRARY_PATH=/home/qweasd/Documents/onnxruntime-linux-x64-gpu-1.16.3/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
source ~/.bashrc
```
- #### For Linux or macOS Users:
- Download the ONNX Runtime package from the [Releases page](https://github.com/microsoft/onnxruntime/releases).
- Set up the library path by exporting the `ORT_DYLIB_PATH` environment variable:
```shell
export ORT_DYLIB_PATH=/path/to/onnxruntime/lib/libonnxruntime.so.1.19.0
```
### 3. \[Optional\] Install CUDA & CuDNN & TensorRT

View file

@ -118,16 +118,15 @@ pub fn check_font(font: &str) -> rusttype::Font<'static> {
rusttype::Font::try_from_vec(buffer).unwrap()
}
use ab_glyph::FontArc;
pub fn load_font() -> FontArc{
pub fn load_font() -> FontArc {
use std::path::Path;
let font_path = Path::new("./font/Arial.ttf");
match font_path.try_exists() {
Ok(true) => {
let buffer = std::fs::read(font_path).unwrap();
FontArc::try_from_vec(buffer).unwrap()
},
}
Ok(false) => {
std::fs::create_dir_all("./font").unwrap();
println!("Downloading font...");
@ -136,7 +135,7 @@ pub fn load_font() -> FontArc{
.timeout(std::time::Duration::from_secs(500))
.call()
.unwrap_or_else(|err| panic!("> Failed to download font: {source_url}: {err:?}"));
// read to buffer
let mut buffer = vec![];
let total_size = resp
@ -153,9 +152,9 @@ pub fn load_font() -> FontArc{
fd.write_all(&buffer).unwrap();
println!("Font saved at: {:?}", font_path.display());
FontArc::try_from_vec(buffer).unwrap()
},
}
Err(e) => {
panic!("Failed to load font {}", e);
},
}
}
}
}

View file

@ -8,7 +8,7 @@ use rand::{thread_rng, Rng};
use std::path::PathBuf;
use crate::{
load_font, gen_time_string, non_max_suppression, Args, Batch, Bbox, Embedding, OrtBackend,
gen_time_string, load_font, non_max_suppression, Args, Batch, Bbox, Embedding, OrtBackend,
OrtConfig, OrtEP, Point2, YOLOResult, YOLOTask, SKELETON,
};
@ -40,7 +40,7 @@ impl YOLOv8 {
OrtEP::CUDA(config.device_id)
} else {
OrtEP::CPU
};
};
// batch
let batch = Batch {
@ -463,7 +463,7 @@ impl YOLOv8 {
image::Rgb(self.color_palette[bbox.id()].into()),
bbox.xmin() as i32,
(bbox.ymin() - legend_size as f32) as i32,
legend_size as f32,
legend_size as f32,
&font,
&legend,
);