Skip to content

Monkeybin11/YoloSharpOnnx

 
 

Repository files navigation

YoloSharpOnnx

YoloSharpOnnx

YOLOv8-v26 C# .NET Version ONNX Runtime OpenCV GitHub license Release NuGet NuGet GitHub last commit GitHub commit activity

🚀a high performance, memory reuse, production-ready C# YOLO inference library for object detection base on OpenCV and ONNX Runtime.

Features

  • 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

Example Images:

Object Detection Result

Build Package

Release x64

Usage

1. Export model to ONNX format:

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')

2. Load the ONNX model with C#:

Install Nuget packages YoloSharpOnnx, OnnxRuntime, OpenCvSharp4.runtime

CPU Inference

dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime
using YoloSharp yolo = new YoloSharp(new ExecutionProviderCPU("yolo11n.onnx"));

CoreML Inference

dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.osx.10.15-x64
dotnet add package Microsoft.ML.OnnxRuntime
using YoloSharp yolo = new YoloSharp(new ExecutionProviderCoreML("yolo11n.onnx"));

CUDA/TensorRT Inference

dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.Gpu.Windows
using YoloSharp yolo = new YoloSharp(new ExecutionProviderCUDA("yolo11n.onnx",0));
using YoloSharp yolo = new YoloSharp(new ExecutionProviderTensorRT("yolo11n.onnx",0));

DirectML Inference

dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.DirectML
using YoloSharp yolo = new YoloSharp(new ExecutionProviderDirectML("yolo11n.onnx",0));

OpenVINO Inference

dotnet add package YoloSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Intel.ML.OnnxRuntime.OpenVino
using YoloSharp yolo = new YoloSharp(new ExecutionProviderOpenVINO("yolo11n.onnx", IntelDeviceType.NPU));

Use the following C# code to load the model and run basic prediction:

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)

YoloSharpOnnx performance testing api

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()}");

Config

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);

Asynchronous inference

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");
}

Batch processing images

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);
      
    }
}

Batch processing images foreach api

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}");
}

Performance Test

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

Performance Test Tool

YoloOnnxWinform

Performance Test PC

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

Performance Test Data

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

YoloSharp test result

Sequence inference time: 18.707s Memory Usage: 1374M

image

YoloDotNet test result

Sequence inference time: 17.665s Memory Usage: 169M

Batch inference time: 10.587s Memory Usage: 639M

image image

YoloSharpOnnx test result

Sequence inference time: 13.693s Memory Usage: 169M

Batch inference time: 2.980s Memory Usage: 601M

image image

Performance Test Result

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
YoloSharpOnnx YoloSharp YoloDotNet

The accuracy and performance of YoloSharpOnnx are the best !!!

Roadmap

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

Model Licensing & Responsibility

  • 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.

About

Yolo C# inference base on opencvsharp & onnx runtime

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C# 100.0%