机器视觉-使用C#进行Yolov8推理

Windows 窗体应用可以使用一些现成的C#类库实现yolov8的predict功能, 本文使用https://github.com/dme-compunet/YoloV8 项目的nuget包.
集成方法非常简单, 但发现这种方式预测准确度下降了很多, 看来还是使用Python API预测更好一些.

GPU版环境准备

  1. 选定 Onnx runtime 版本, https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements
  2. 参考Onnx runtime 版本, 安装兼容的 CUDA 版本, 推荐 v11.x
  3. 参考Onnx runtime 版本, 安装兼容的 cuDNN 版本
  4. 如果需要检测视频, 需要下载 FFMPEG, 安装后需要将 FFmpeg 和 ffprobe 加到Path环境变量中.
  5. nuget 安装 Microsoft.ML.OnnxRuntime.Gpu
  6. nuget 安装 YoloV8.Gpu

CPU版环境准备

  1. nuget 安装 Microsoft.ML.OnnxRuntime
  2. nuget 安装 YoloV8

Onnx 模型导出

Onnx runtime 当前仅支持 Opset 15版, 所以导出时需要增加 --Opset=15 参数.

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

            //创建模型并预测
            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);

            //展现预测结果对象中的信息
            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);
        }
    }
}

准确度对比

Yolo使用原生PT预测准确度和Yolo使用Onnx相当, 但 OnnxRuntime+C#准确度下降很多. 下面都是使用yolov8m模型的结果,

更详细的安装和使用说明可参考下面.net 项目

https://github.com/dme-compunet/YoloV8
https://github.com/sstainba/Yolov8.Net
https://github.com/NickSwardh/YoloDotNet

posted @ 2024-02-17 19:11  harrychinese  阅读(2835)  评论(1编辑  收藏  举报