基于ONNXRuntime C#实现的高性能YOLO推理框架

发布时间:2026/6/28 5:04:58
基于ONNXRuntime C#实现的高性能YOLO推理框架 功能特色支持所有YOLO任务目标检测、图片分类、实例分割、姿势估计、OBB支持多种部署方案CPU, CUDA / TensorRT, OpenVINO, CoreML, DirectML可以批量检测图片性能大幅优于单线程串行执行图像处理使用OpenCVSharp推理引擎ONNX Runtime 是一个跨平台的机器学习模型加速器示例DemoObject DetectionImage ClassificationInstance SegmentationPose EstimationOBB Detection使用示例1 导出模型为onnx格式from ultralytics import YOLO # Load a model model YOLO(path/to/best.pt) # Export the model to ONNX format model.export(formatonnx)2 YoloSharpOnnx初始化安装Nuget 包YoloSharpOnnx, OnnxRuntime, OpenCvSharp4.runtimeCPU推理dotnet add package YoloSharpOnnx dotnet add package OpenCvSharp4.runtime.win dotnet add package Microsoft.ML.OnnxRuntimeusing YoloSharp yolo new YoloSharp(new ExecutionProviderCPU(yolo11n.onnx));CoreML推理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));CUDA/TensorRT推理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));DirectML推理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));OpenVINO Inferencedotnet 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));基本的API加载模型并进行预测using Mat image Cv2.ImRead(bus.jpg); using YoloSharp yolo new YoloSharp(new ExecutionProviderCPU(yolo11n.onnx)); ListDetectionResult res yolo.RunDetect(image); yolo.DrawDetections(image,res); Cv2.ImWrite(bus_res.jpg, image); string printString res.Summary(); Console.WriteLine(printString);性能测试APIusing 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);批量处理API