🚀a high performance, memory reuse, production-ready C# YOLO inference library for object detection base on OpenCV and ONNX Runtime.
- YOLO Task Object Detection
- Execution Provider CPU, CUDA / TensorRT, OpenVINO, CoreML, DirectML
- Batch processing images Preprocess and Inference are executed asynchronously with Producer/Consumer pattern
- High Performance Inference Memory reuse, GPU Inference with I/O Binding
- Image Processing OpenCvSharp4
- Inference Engine ONNX Runtime is a cross-platform inference and training machine-learning accelerator.
- YOLO Versions Includes support for: YOLOv8, YOLO11, YOLO26
Release x64
For convert the pre-trained PyTorch model to ONNX format, run the following Python code:
from ultralytics import YOLO
# Load a model
model = YOLO('path/to/best.pt')
# Export the model to ONNX format
model.export(format='onnx')Install Nuget packages YoloSharpOnnx, OnnxRuntime, OpenCvSharp4.runtime
dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntimeusing YoloSharp yolo = new YoloSharp(new ExecutionProviderCPU("yolo11n.onnx"));dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.osx.10.15-x64
dotnet add package Microsoft.ML.OnnxRuntimeusing YoloSharp yolo = new YoloSharp(new ExecutionProviderCoreML("yolo11n.onnx"));dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.Gpu.Windowsusing YoloSharp yolo = new YoloSharp(new ExecutionProviderCUDA("yolo11n.onnx",0));
using YoloSharp yolo = new YoloSharp(new ExecutionProviderTensorRT("yolo11n.onnx",0));dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.DirectMLusing YoloSharp yolo = new YoloSharp(new ExecutionProviderDirectML("yolo11n.onnx",0));dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Intel.ML.OnnxRuntime.OpenVinousing YoloSharp yolo = new YoloSharp(new ExecutionProviderOpenVINO("yolo11n.onnx", IntelDeviceType.NPU));using Mat image = Cv2.ImRead("bus.jpg");
using YoloSharp yolo = new YoloSharp(new ExecutionProviderCPU("yolo11n.onnx"));
List<DetectionResult> res = yolo.RunDetect(image);
yolo.DrawDetections(image,res);
Cv2.ImWrite("bus_res.jpg", image);
string printString = YoloUtils.GetResult(res);
Console.WriteLine(printString)using Mat image = Cv2.ImRead("bus.jpg");
using YoloSharp yolo = new YoloSharp(new ExecutionProviderDirectML("yolo11n.onnx",1));
var res = yolo.RunDetectWithTime(item.FullName);
Console.WriteLine($"{res.ToString()}, {res.SpeedResult.ToString()}");using Mat image = Cv2.ImRead("bus.jpg");
using YoloSharp yolo = new YoloSharp(new ExecutionProviderCPU("yolo11n.onnx"));
yolo.YoloConfiguration.IoU = 0.4f;
yolo.YoloConfiguration.Confidence = 0.3f;
yolo.YoloConfiguration.ResizeAlgorithm = InterpolationFlags.Linear;
yolo.YoloConfiguration.ImageExtsBatch = [".jpg", ".png"];
var res = yolo.RunDetect(image);private static async Task TestInferAsync()
{
string modelPath = @"D:\code\model\best.onnx";
string dir = @"D:\code\model\TestImages";
using var yolo = new YoloSharp(new ExecutionProviderDirectML(modelPath, 1));
System.Diagnostics.Stopwatch _stopwatchTotal = new System.Diagnostics.Stopwatch();
_stopwatchTotal.Start();
var files = Directory.GetFiles(dir);
using (var yoloAsync = yolo.CreateAsyncChannel())
{
for (int i = 0; i < files.Length; i++)
{
var res = await yoloAsync.RunDetectAsync(files[i]);
Console.WriteLine($"{i + 1} {YoloUtils.GetResult(res)}");
}
}
_stopwatchTotal.Stop();
var avg = _stopwatchTotal.ElapsedMilliseconds / files.Length;
Console.WriteLine($"total time:{_stopwatchTotal.Elapsed}, count:{files.Length} Infer avg time:{avg}ms");
}private static void TestBatchInfer()
{
string modelPath = @"D:\code\model\best.onnx";
string dir = @"D:\code\model\TestImages"
DirectoryInfo directory = new DirectoryInfo(dir);
var files = directory.GetFiles()
System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
_stopwatch.Start();
int num=files.Length;
using (YoloSharp yolo = new YoloSharp(new ExecutionProviderDirectML(modelPath, 0)))
{
yolo.BatchDetectItemCompleted += Yolo_BatchDetectCompleted
var list = yolo.RunBatchDetect(dir,new ProcessCallback(), ReceiveProcess, 30)
}
_stopwatch.Stop()
Console.WriteLine($"detect {num} images, time:{_stopwatch.Elapsed}");
private static void Yolo_BatchDetectCompleted(object? sender, DetectionBatchResult e)
{
string ans = YoloUtils.GetResult(e.Results);
Console.WriteLine(ans);
private static void ReceiveProcess(DetectionBatchResult e)
{
string res = YoloUtils.GetResult(e.Results)
}
internal class ProcessCallback : IBatchProcessCallback
{
public void ReceiveProcessResult(DetectionBatchResult e)
{
string res = YoloUtils.GetResult(e.Results);
}
}private static async Task TestBatchForeachInfer()
{
var files = Directory.GetFiles(dir);
System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
_stopwatch.Start();
int num = files.Length;
using (YoloSharp yolo = new YoloSharp(new ExecutionProviderDirectML(modelPath, _deviceId)))
{
yolo.YoloConfiguration.BatchPoolSize = 30;
await foreach (var item in yolo.BatchDetectForeachAsync(files.ToList()))
{
Console.WriteLine($"{item.ImagePath} {YoloUtils.GetResult(item.Results)}");
}
}
_stopwatch.Stop();
Console.WriteLine($"detect {num} images, time:{_stopwatch.Elapsed}");
}| Yolo C# inference library | Version | Image Processing library | Image Resize Algorithm | Sequence inference | Batch inference |
|---|---|---|---|---|---|
| YoloSharp | 6.1.0 | SixLabors.ImageSharp 3.1.12 | Triangle(Bilinear) | support | not support |
| YoloDotNet | 4.2.0 | SkiaSharp 3.119.1 | Linear(Bilinear) | support | support |
| YoloSharpOnnx | 1.3.3 | OpenCvSharp4 4.13.0.20260318 | Linear(Bilinear) | support | support |
| Hardware | Summary |
|---|---|
| Windows | Windows 10 OS Version 19045.6466 |
| CPU | AMD Ryzen 7 5800X 8-Core Processor 3.8GHz |
| Memory | DDR4 3200 MHz 32GB |
| GPU | AMD Radeom RX6800 16GB |
| Storage | SSD 2TB |
Images: 300 images (image size: 2480x3494)
Yolo Model: Yolo11n.onnx InputShape float32[1,3,1280,1280]
Inference Provider: DirectML Inference Microsoft.ML.OnnxRuntime 1.24.3
Sequence inference time: 18.707s Memory Usage: 1374M
Sequence inference time: 17.665s Memory Usage: 169M
Batch inference time: 10.587s Memory Usage: 639M
Sequence inference time: 13.693s Memory Usage: 169M
Batch inference time: 2.980s Memory Usage: 601M
| Yolo C# inference library | Version | Image Processing library | Image Resize Algorithm | Sequence inference (Time/Memory) | Batch inference (Time/Memory) |
|---|---|---|---|---|---|
| YoloSharp | 6.1.0 | SixLabors.ImageSharp 3.1.12 | Triangle(Bilinear) | 18.707s, 1374M | - |
| YoloDotNet | 4.2.0 | SkiaSharp 3.119.1 | Linear(Bilinear) | 17.665s, 169M | 10.587s, 639M |
| YoloSharpOnnx | 1.3.3 | OpenCvSharp4 4.13.0.20260318 | Linear(Bilinear) | 13.693s, 169M | 2.980s, 601M |
| YoloSharpOnnx | YoloSharp | YoloDotNet |
|---|---|---|
The accuracy and performance of YoloSharpOnnx are the best !!!
| Time | Feature |
|---|---|
| 2026-10 | Yolo task Image Classification |
| 2026-11 | Yolo task Instance Segmentation |
| 2026-11 | Yolo task Pose Estimation |
| 2026-12 | Yolo task OBB |
-
YoloSharpOnnx is licensed under the MIT License and provides an ONNX inference engine for YOLO models exported using Ultralytics YOLO tooling.
-
This project does not include, distribute, download, or bundle any pretrained models.
-
Users must supply their own ONNX models.
-
YOLO ONNX models produced using Ultralytics tooling are typically licensed under AGPL-3.0 or a separate commercial license from Ultralytics.
-
YoloSharpOnnx does not impose, modify, or transfer any license terms related to user-supplied models.
-
Users are solely responsible for ensuring that their use of any model complies with the applicable license terms, including requirements related to commercial use, distribution, or network deployment.