当前位置: 首页 > 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/

http://www.lryc.cn/news/540652.html

相关文章:

  • 51c大模型~合集69
  • 2025寒假周报4
  • 自学Java-AI结合GUI开发一个石头迷阵的游戏
  • buuctf-[极客大挑战 2019]Knife题解
  • Spring MVC 对象转换器:初级开发者入门指南
  • 语音直播交友app出海:语音直播交友系统软件源码搭建国际化发展技术层面分析
  • Web Scraper,强大的浏览器爬虫插件!
  • EasyRTC:基于WebRTC与P2P技术,开启智能硬件音视频交互的全新时代
  • go 定时任务 gocron timer
  • uniapp引入uview组件库(可以引用多个组件)
  • MySQL主从架构
  • 科普mfc100.dll丢失怎么办?有没有简单的方法修复mfc100.dll文件
  • 论文笔记:How Much Can Time-related Features Enhance Time Series Forecasting?
  • Qt学习(六) 软件启动界面 ,注册表使用 ,QT绘图, 视图和窗口绘图,Graphics View绘图框架:简易CAD
  • JavaScript系列(80)--WebAssembly 基础入门
  • 蓝桥杯刷题2.21|笔记
  • 053 性能压测 单机锁 setnx
  • 【天线】IFA天线知识点摘抄
  • Mysql视图有什么作用?你是否使用过视图?
  • 【vue项目如何利用event-stream实现文字流式输出效果】
  • 微信问题总结(onpageshow ,popstate事件)
  • 【Gin-Web】Bluebell社区项目梳理3:社区相关接口开发
  • Unity 聊天气泡根据文本内容适配
  • 对学习编程语言的一些理解
  • MySQL MHA 部署全攻略:从零搭建高可用数据库架构
  • windows怎样查看系统信息(处理器等)
  • 007 HBuilderX提示IDE service port disabled. To use CLI Call, open IDE
  • 计算机网络之TCP的可靠传输
  • Python爬虫系列教程之第十四篇:爬虫项目部署、调度与监控系统
  • 线程与进程的深入解析及 Linux 线程编程