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

相关推荐
HIT_Weston2 小时前
45、【Agent】【OpenCode】本地代理分析(请求&接收回调)
人工智能·agent·opencode
逻辑君3 小时前
认知神经科学研究报告【20260010】
人工智能·深度学习·神经网络·机器学习
星河耀银海3 小时前
远控体验分享:安全与实用性参考
人工智能·安全·微服务
企业架构师老王3 小时前
2026企业架构演进:科普Agent(龙虾)如何从“极客玩具”走向实在Agent规模化落地?
人工智能·ai·架构
GreenTea3 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
鬼先生_sir3 小时前
Spring AI Alibaba 1.1.2.2 完整知识点库
人工智能·ai·agent·源码解析·springai
深念Y4 小时前
豆包AI能力集成方案:基于会话管理的API网关设计
人工智能
龙文浩_4 小时前
Attention Mechanism: From Theory to Code
人工智能·深度学习·神经网络·学习·自然语言处理
ulimate_4 小时前
八卡算力、三个Baseline算法(WALLOSS、pi0、DreamZero)
人工智能
深小乐4 小时前
AI 周刊【2026.04.06-04.12】:Anthropic 藏起最强模型、AI 社会矛盾激化、"欢乐马"登顶
人工智能