機(jī)器視覺(jué)-使用C#進(jìn)行Yolov8推理
Windows 窗體應(yīng)用可以使用一些現(xiàn)成的C#類(lèi)庫(kù)實(shí)現(xiàn)yolov8的predict功能, 本文使用https://github.com/dme-compunet/YoloV8 項(xiàng)目的nuget包, 最新版是 https://github.com/dme-compunet/YoloSharp. 集成方法非常簡(jiǎn)單, 但發(fā)現(xiàn)這種方式預(yù)測(cè)準(zhǔn)確度下降了很多, 看來(lái)還是使用Python API預(yù)測(cè)更好一些.
GPU版環(huán)境準(zhǔn)備
- 選定 Onnx runtime 版本, https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements
- 參考Onnx runtime 版本, 安裝兼容的 CUDA 版本, 推薦 v11.x
- 參考Onnx runtime 版本, 安裝兼容的 cuDNN 版本
- 如果需要檢測(cè)視頻, 需要下載 FFMPEG, 安裝后需要將 FFmpeg 和 ffprobe 加到Path環(huán)境變量中.
- nuget 安裝 Microsoft.ML.OnnxRuntime.Gpu
- nuget 安裝 YoloV8.Gpu
CPU版環(huán)境準(zhǔn)備
- nuget 安裝 Microsoft.ML.OnnxRuntime
- nuget 安裝 YoloV8
Onnx 模型導(dǎo)出
Onnx runtime 當(dāng)前僅支持 Opset 15版, 所以導(dǎo)出時(shí)需要增加 --Opset=15 參數(shù).
yolo export model=yolov8m.pt format=onnx opset=15
yolo export model=yolov8m.pt format=onnx opset=15 simplify=False dynamic=False imgsz=640
代碼
使用 yolo.exe 推理的代碼:
yolo predict model=D:\my_workspace\py_code\yolo8\Scripts\yolov8m.pt source=D:\my_workspace\source\opencv\yolov8\WinFormsApp1\bus.jpg
yolo predict model=D:\my_workspace\source\opencv\yolov8\WinFormsApp1\yolov8m.onnx source=D:\my_workspace\source\opencv\yolov8\WinFormsApp1\bus.jpg
使用 OnnxRuntime 的 C# 代碼:
using Compunet.YoloV8;
using Compunet.YoloV8.Data;
using Compunet.YoloV8.Metadata;
using Compunet.YoloV8.Plotting;
using SixLabors.ImageSharp;
using System.Diagnostics;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var imageFile = "D:\\my_workspace\\source\\opencv\\yolov8\\WinFormsApp1\\bus.jpg";
var newImageFile = "D:\\my_workspace\\source\\opencv\\yolov8\\WinFormsApp1\\bus_onnx_csharp.jpg";
var modelFile = "D:\\my_workspace\\source\\opencv\\yolov8\\WinFormsApp1\\yolov8m.onnx";
//創(chuàng)建模型并預(yù)測(cè)
using var predictor = new YoloV8(modelFile);
predictor.Parameters.SuppressParallelInference = true;
predictor.Parameters.KeepOriginalAspectRatio = true;
//predictor.Parameters.Confidence = 0.1F;
//predictor.Parameters.IoU = 0.3F;
DetectionResult result = predictor.Detect(imageFile);
//展現(xiàn)預(yù)測(cè)結(jié)果對(duì)象中的信息
foreach (BoundingBox box in result.Boxes)
{
SixLabors.ImageSharp.Rectangle rect = box.Bounds;
var confidence = box.Confidence;
YoloV8Class yoloV8Class = box.Class;
Debug.WriteLine("====================");
Debug.WriteLine(yoloV8Class.Id);
Debug.WriteLine(yoloV8Class.Name);
Debug.WriteLine(confidence);
Debug.WriteLine(rect);
}
//plot label
using var image = SixLabors.ImageSharp.Image.Load(imageFile);
using var ploted = result.PlotImage(image);
ploted.Save(newImageFile);
}
}
}
準(zhǔn)確度對(duì)比
Yolo使用原生PT預(yù)測(cè)準(zhǔn)確度和Yolo使用Onnx相當(dāng), 但 OnnxRuntime+C#準(zhǔn)確度下降很多. 下面都是使用yolov8m模型的結(jié)果,

更詳細(xì)的安裝和使用說(shuō)明可參考下面.net 項(xiàng)目
https://github.com/dme-compunet/YoloV8 https://github.com/dme-compunet/YoloSharp
https://github.com/sstainba/Yolov8.Net
https://github.com/NickSwardh/YoloDotNet , 這個(gè)類(lèi)庫(kù)比上面dme-compunet的類(lèi)庫(kù)更好, 內(nèi)存使用更高效, 速度更快, confidence 精度更好, dme-compunet 貌似會(huì)有內(nèi)存泄漏問(wèn)題.
https://github.com/Kayzwer/yolo-cs

浙公網(wǎng)安備 33010602011771號(hào)