当前位置: 首页 > news >正文

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

yolov12官方框架:github.com/sunsmarterjie/yolov12

【测试环境】

vs2019

netframework4.7.2

opencvsharp4.8.0

onnxruntime==1.16.3

【效果展示】

【调用代码】

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();
        Yolov12Manager ym = new Yolov12Manager();
        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\\yolov12n.onnx", Application.StartupPath + "\\weights\\labels.txt");

        }

        private void btn_video_Click(object sender, EventArgs e)
        {
            var detector = new Yolov12Manager();
            detector.LoadWeights(Application.StartupPath + "\\weights\\yolov12n.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)首先依据官方安装教程或者其他网站给的安装教程,安装好yolov12环境

(2)下载模型:yolov12n.pt或者直接下载yolov12n.onnx
(3)导出onnx模型:yolo export model=yolov12n.pt format=onnx dynamic=False opset=12
(4)然后将yolov12.onnx模型放进FIRC\bin\x64\Debug\weights
最后运行项目选择x64 Debug即可,由于初次运行可能报错,如果报错请查看blog.csdn.net/FL1623863129/article/details/135424751
解决方法

【视频演示】

www.bilibili.com/video/BV1RVAbeqEXa/

相关文章:

  • 2024电子取证“獬豸杯”WP
  • Python标准库装饰器完全指南
  • 网站结构优化:加速搜索引擎收录的关键
  • docker独立部署milvus向量数据库
  • 【量化科普】Volatility,波动率
  • 已知点矩阵的三个顶点坐标、行列数和行列的间距,计算得出剩余所有点的坐标
  • 如何正确看待 AI 的推理能力?走出人类中心主义
  • 【系统架构设计师】需求工程
  • 文心智能体平台已全面接入DeepSeek模型,全部免费!
  • 2.21寒假作业
  • C++与Python:两种编程语言的区别
  • 程序代码篇---Python指明函数参数类型
  • 快速入门——Axios网络请求
  • Docker 的安全配置与优化(二)
  • 搭建RAG知识库的完整源码实现
  • 写出一个简单的JavaScript闭包示例,并解释为什么它形成了闭包。
  • QT基础八、与时间相关的UI控件
  • 【Kubernets】Kubernets资源类型Deployment详细介绍
  • 推动智驾普及,谁是自主品牌前视一体机计算方案市场TOP1?
  • 电力通信物联网应用,国密网关守护电力数据安全
  • 先做网站先备案/谷歌推广哪家好
  • 网上最可靠的保险平台/window优化大师官网
  • 做网站得花多少钱/网站推广入口
  • 郑州专业建网站/网店买卖有哪些平台
  • 七台河新闻联播/深圳最好的外贸seo培训
  • 网站租房做公寓/外贸推广平台怎么做