This project provides a Rust implementation of YOLOv8 segmentation using the ONNX Runtime inference engine. It is based on the YOLOv8 example from the pykeio/ort project.
- Rust programming language
- Microsoft ONNX Runtime v1.18.0 or later
- yolov8m-seg pt model
Run the following commands to install the required packages:
pip install onnxruntime-gpu # Install GPU version
pip install onnxruntime # Install CPU version
pip install ultralytics
-
Download the model
Visit the Segment Models section in the Ultralytics YOLO documentation to download the desired model. For example, you can download the YOLOv8m-seg model.
-
Convert the model to ONNX format
Run the following command to convert the
YOLOv8m-seg.pt
model toonnx
format:yolo task=segment mode=export model=yolov8m-seg.pt format=onnx
This command will convert the YOLOv8m-seg model from PyTorch format (
.pt
) to ONNX format (.onnx
).
-
Download and install the ONNX Runtime shared library from the official releases page. Make sure to choose the appropriate version for your operating system.
-
Set the
LD_LIBRARY_PATH
environment variable to the directory containing the ONNX Runtime shared library. For example:export LD_LIBRARY_PATH=/path/to/onnxruntime/lib:$LD_LIBRARY_PATH
Clone this repository:
git clone https://github.com/your-username/yolov8seg-ort-rs.git
cd yolov8seg-ort-rs
- Edit src/main.rs
Replace path/to/your/model.onnx
with the path to your YOLOv8 in Line:129.
const YOLOV8M_FILE_PATH: &str = "/home/ubuntu/projects/ort/yolov8m-seg.onnx";
And image path
in Line:139 with the path to the input image.
let original_img = image::open(Path::new(env!("CARGO_MANIFEST_DIR")).join("data").join("baseball.jpg")).unwrap();
- Run the YOLOv8 segmentation inference:
./target/release/yolov8seg-ort-rs
Replace path/to/your/model.onnx
with the path to your YOLOv8 segmentation model and path/to/your/image.jpg
with the path to the input image.
- The segmentation results will be saved as an image file named
output.jpg
in the current directory.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.
- pykeio/ort - Rust wrapper for ONNX Runtime
- Microsoft ONNX Runtime - Open-source inference engine for ONNX models