C# winform部署yolo26-obb旋转框检测的onnx模型演示源码+模型+说明

yolo26已经正式发布了,因此使用C#代码实现YOLO26-obb旋转框检测的onnx模型部署,首先看yolo11n-obb网络结构,发现输出shape是1x20x21504

再来看看yolo26n-obb网络结构输出,输出shape是1x300x7

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

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

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

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

测试环境:

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();
        Yolo26ObbManager ym = new Yolo26ObbManager();
        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-obb.onnx");

        }

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

相关推荐
MATLAB代码顾问6 小时前
5大智能算法优化标准测试函数对比(Python实现)
开发语言·python
工程师0077 小时前
C# 装箱、拆箱 底层原理
c#·装箱和拆箱
万粉变现经纪人7 小时前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
清风明月一壶酒8 小时前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
其实防守也摸鱼8 小时前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
小郑加油9 小时前
python学习Day12:pandas安装与实际运用
开发语言·python·学习
AC赳赳老秦9 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
KuaCpp9 小时前
C++面向对象(速过复习版)
开发语言·c++
wbs_scy9 小时前
Linux线程同步与互斥(三):线程同步深度解析之POSIX 信号量与环形队列生产者消费者模型,从原理到源码彻底吃透
java·开发语言
2zcode10 小时前
基于MATLAB元胞自动机(CA)的AZ80A镁合金动态再结晶(DRX)过程模拟
开发语言·matlab·动态再结晶