C# winform部署yolo26-seg实例分割的onnx模型演示源码+模型+说明

yolo26已经正式发布了,因此使用C#代码实现YOLO26-seg实例分割部署,首先看yolov11-seg网络结构,发现输出shape是1x116x8400

再来看看yolo26-seg网络结构输出,输出shape是1x300x38

可见yolo11和yolo26输出是不一样的是不能共用代码。

模型使用官方yolo26n-seg.pt转换成的onnx,转换命令

yolo export model=yolo26n-seg.pt format=onnx opset=12

如果你是自己训练的模型可以替换即可,但是需要yolo26-seg框架才行

测试环境:

vs2019

CPU推理,无需安装cuda+cudnn

onnxruntime==1.22.1

opecvsharp==4.11.0

.net framework4.8.0

ultralytics==8.4.0

实现界面代码和调用代码:

复制代码
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
    {
        OpenCvSharp.Mat src = new OpenCvSharp.Mat();
        Yolo26SegManager ym = new Yolo26SegManager();
        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\\yolo26n-seg.onnx", Application.StartupPath + "\\weights\\labels.txt");

        }

        private void btn_video_Click(object sender, EventArgs e)
        {
            var detector = new Yolo26SegManager();
            detector.LoadWeights(Application.StartupPath + "\\weights\\yolo26n-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();
  
        }
    }
}

最后测试效果:

源码地址:https://download.csdn.net/download/FL1623863129/92562482

相关推荐
Bonnie3731 天前
云边端一体化架构:三大组件(云、边、端)的分工与协同逻辑
人工智能·程序人生·云原生·架构·个人开发
gorgeous(๑>؂<๑)1 天前
【CVPR26-雷涛-陕西科技大学陕西省人工智能联合实验室】SPEGC:基于语义提示增强图聚类的医学图像分割持续测试时自适应
人工智能·科技·机器学习·数据挖掘·聚类
写点什么呢1 天前
Pytorch学习16_损失函数与反向传播
人工智能·pytorch·python·学习·pycharm
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-03-21)
人工智能·ai·大模型·github·ai教程
程序媛徐师姐1 天前
Python基于深度学习的声音识别青少年防沉迷系统【附源码、文档说明】
python·深度学习·声音识别青少年防沉迷系统·python声音识别·python青少年防沉迷系统·python深度学习声音识别·青少年防沉迷系统
特立独行的猫a1 天前
ESP32小智AI的WebSocket 调试工具的实现,小智AI后台交互过程揭秘(二、技术原理与实现过程详解 )
人工智能·websocket·网络协议·esp32·调试工具·小智ai
irpywp1 天前
构建生产级 AI Agent工作流
人工智能·github
月光有害1 天前
简单理解深度学习中的多种归一化方法
人工智能·深度学习
艾莉丝努力练剑1 天前
【Linux信号】Linux进程信号(上):信号产生方式和闹钟
linux·运维·服务器·c++·人工智能·ubuntu·云原生
Bonnie3731 天前
算力基建入门-AI时代,算力为何是数字底座
人工智能·程序人生·云原生·个人开发