[C#]使用onnxruntime部署yolov11-onnx实例分割模型

【官方框架地址】

https://github.com/ultralytics/ultralytics.git

【算法介绍】

在C#中使用ONNX Runtime部署YOLOv11-ONNX实例分割模型,涉及到模型的加载、数据预处理、模型推理和后处理几个关键步骤。

首先,需要确保已经安装了ONNX Runtime的NuGet包,它提供了在C#中加载和运行ONNX模型的功能。

其次,加载YOLOv11-ONNX模型。这通常涉及到指定模型的路径,并创建一个InferenceSession对象,该对象将用于后续的推理。

接下来,进行数据预处理。YOLO模型通常要求输入图像具有特定的尺寸和格式。因此,需要使用适当的图像处理库(如OpenCV或System.Drawing)来调整图像的大小、归一化像素值,并将其转换为模型所需的张量格式。

然后,进行模型推理。将预处理后的数据传递给InferenceSession对象,并调用其Run方法来执行推理。这将返回模型的输出,通常是一个包含检测框、类别置信度和实例分割信息的张量。

最后,进行后处理。解析模型的输出,提取有用的信息(如检测框的坐标、类别和实例分割掩码),并根据需要进行进一步的处理或可视化。

请注意,YOLOv11的具体实现和输出格式可能与上述描述有所不同。因此,在实际部署时,需要参考YOLOv11的文档和ONNX Runtime的API文档来确保正确理解和处理模型的输出。

【效果展示】

【实现部分代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;

namespace FIRC
{
    public partial class Form1 : Form
    {
        Mat src = new Mat();
        Yolov11Manager ym = new Yolov11Manager();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
              
                src = Cv2.ImRead(openFileDialog.FileName);
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);


            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image==null)
            {
                return;
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = ym.Inference(src);
            sw.Stop();
            this.Text = "耗时" + sw.Elapsed.TotalSeconds + "秒";
            var resultMat = ym.DrawImage(src,result);
            pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ym.LoadWeights(Application.StartupPath+ "\\weights\\yolo11n-seg.onnx", Application.StartupPath + "\\weights\\labels.txt");

        }

        private void btn_video_Click(object sender, EventArgs e)
        {
            var detector = new Yolov11Manager();
            detector.LoadWeights(Application.StartupPath + "\\weights\\yolo11n-seg.onnx", Application.StartupPath + "\\weights\\labels.txt");
            VideoCapture capture = new VideoCapture(0);
            if (!capture.IsOpened())
            {
                Console.WriteLine("video not open!");
                return;
            }
            Mat frame = new Mat();
            var sw = new Stopwatch();
            int fps = 0;
            while (true)
            {

                capture.Read(frame);
                if (frame.Empty())
                {
                    Console.WriteLine("data is empty!");
                    break;
                }
                sw.Start();
                var result = detector.Inference(frame);
                var resultImg = detector.DrawImage(frame,result);
                sw.Stop();
                fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
                sw.Reset();
                Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
                //显示结果
                Cv2.ImShow("Result", resultImg);
                int key = Cv2.WaitKey(10);
                if (key == 27)
                    break;
            }

            capture.Release();
  
        }
    }
}

【视频演示】 

C#使用onnxruntime部署yolov11-onnx实例分割模型_哔哩哔哩_bilibili【测试环境】vs2019net framework4.7.2opencvsharp4.8.0onnxruntime1.16.3 更多信息或者源码下载参考博文:https://blog.csdn.net/FL1623863129/article/details/142727953, 视频播放量 0、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 未来自主研究中心, 作者简介 未来自主研究中心,相关视频:C++使用纯opencv部署yolov11-seg实例分割onnx模型,C#使用纯opencvsharp部署yolov8-onnx图像分类模型,YOLO最新版本V11 本地一键部署 解压即用 视觉检测大模型尝鲜版 集成环境依赖 WEBUI可视化界面,使用C#部署yolov8-seg的实例分割的tensorrt模型,使用python部署yolov10的onnx模型,使用C#的winform部署yolov8的onnx实例分割模型,2024最新Tkinter教程(Python GUI),C#部署官方yolov8-obb旋转框检测的onnx模型,将yolov10封装成一个类几句调用完成目标检测任务,使用python转换pt并部署yolov10的tensorrt模型icon-default.png?t=O83Ahttps://www.bilibili.com/video/BV1yu1qYaE5K/
【源码下载】

https://download.csdn.net/download/FL1623863129/89852006
【测试环境】

vs2019

net framework4.7.2

opencvsharp4.8.0

onnxruntime1.16.3 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值