[C#]C# winform部署yolov11目标检测的onnx模型

yolov11官方框架:https://github.com/ultralytics/ultralytics

【测试环境】

vs2019

netframework4.7.2

opencvsharp4.8.0

onnxruntime==1.16.2

【效果展示】

【实现部分代码】

复制代码
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(result,src);
            pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ym.LoadWeights(Application.StartupPath+ "\\weights\\yolo11n.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.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(result,frame);
                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();
  
        }
    }
}

【运行步骤】

(1)首先依据官方安装教程或者其他网站给的安装教程,安装好yolov8环境

(2)下载模型:https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.pt

(3)导出onnx模型:yolo export model=yolo11n.pt format=onnx dynamic=False opset=12

(4)然后将yolo11.onnx模型放进FIRC\bin\x64\Debug\weights

最后运行项目选择x64 Debug即可,由于初次运行可能报错,如果报错请查看https://blog.csdn.net/FL1623863129/article/details/135424751

解决方法

【视频演示】

C# winform部署yolov11目标检测的onnx模型_哔哩哔哩_bilibili【测试环境】vs2019netframework4.7.2opencvsharp4.8.0onnxruntime==1.16.2更多实现细节和源码下载参考博文:https://blog.csdn.net/FL1623863129/article/details/142688383, 视频播放量 1、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 未来自主研究中心, 作者简介 未来自主研究中心,相关视频:C# winform部署yolov10的onnx模型,YOLOv8 目标检测模型 原理解析,目标检测领如何快速水一篇论文?迪哥给你梳理最佳学习路径,快速拿结果毕业!YOLO全系列、DTER模型、R-CNN系列目标检测算法全详解!,一颗CV视觉AI领域的重磅炸弹!仅更改一行代码就让YOLOV11成为了最成熟、效果最好的目标检测模型!,不愧是GitHub大佬!半天就教会了我YOLO、SSD、FasterRCNN、FastRCNN、SPPNet、RCNN等六大目标检测算法!深度学习/物体检测,【2024】最全目标检测课程,带你从零开始入门YOLO、R-CNN、Faster-RCNN,小学生都看懂了!人工智能/YOLOv10/v9/v8/v7/v6,这可能是B站最完整的Transformer讲解了!一口气学完DETR⽬标检测、DETR项⽬源码解读、项⽬源码debug逐⾏解读、注意⼒机制的作⽤分析-人工智能,C#使用纯opencvsharp部署yolov8-onnx图像分类模型,用C#部署yolov8的tensorrt模型进行目标检测winform最快检测速度,C++使用纯opencv部署yolov9的onnx模型https://www.bilibili.com/video/BV1ic4jehE4C/?vd_source=989ae2b903ea1b5acebbe2c4c4a635ee

【完整源码下载】

https://download.csdn.net/download/FL1623863129/89836753

相关推荐
dapeng28701 小时前
分布式系统容错设计
开发语言·c++·算法
qq_417695051 小时前
代码热修复技术
开发语言·c++·算法
badhope6 小时前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园7 小时前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈7 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl7 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628887 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
IT猿手8 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
AI科技星8 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
sunwenjian8868 小时前
Java进阶——IO 流
java·开发语言·python