news 2026/3/26 13:40:12

[C#]winform使用纯opencvsharp部署yolo26-cls图像分类的onnx模型

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
[C#]winform使用纯opencvsharp部署yolo26-cls图像分类的onnx模型


【算法介绍】

使用纯OpenCvSharp部署YOLO26-CLS图像分类模型是一项复杂的任务,但可以通过以下步骤实现:

  1. 准备环境:首先,确保开发环境已安装OpenCvSharp和必要的.NET框架,如VS2019和.NET Framework 4.7.2。同时,需要YOLOv11的ONNX模型文件、配置文件(描述模型架构)和类别名称文件。
  2. 加载模型:使用OpenCvSharp的DNN模块加载YOLO26-CLS的ONNX模型。这通常涉及将模型文件路径传递给DNN模块的相关函数。
  3. 预处理图像:对输入图像进行预处理,如调整大小、归一化等,以满足模型的输入要求。
  4. 推理与后处理:将预处理后的图像输入到模型中,获取分类结果。对结果进行后处理,包括解析输出、应用非极大值抑制(如果需要)等,以获得最终的分类结果。
  5. 显示结果:将分类结果显示在界面上,可以通过OpenCvSharp的图像显示功能实现。

值得注意的是,YOLO26是一个复杂的模型,其输出可能包含多个层的信息,因此需要仔细解析模型输出,并根据YOLO26的具体实现进行后处理。此外,由于OpenCvSharp的DNN模块对ONNX的支持可能有限,某些YOLO26的特性可能无法在OpenCvSharp中直接实现。在这种情况下,可能需要寻找替代方案,如使用其他深度学习库来加载和运行模型,并通过C#接口与这些库进行交互。

总之,使用纯OpenCvSharp部署YOLO26-CLS图像分类模型需要深入理解YOLO26的模型架构、OpenCvSharp的DNN模块以及ONNX格式。

【效果展示】

【界面设计和调用模型代码】

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(); Yolo26ClsManager ym = new Yolo26ClsManager(); 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-cls.onnx", Application.StartupPath + "\\weights\\labels.txt"); } private void btn_video_Click(object sender, EventArgs e) { var detector = new Yolo26ClsManager(); detector.LoadWeights(Application.StartupPath + "\\weights\\yolo26n-cls.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(); } } }


【测试环境】

vs2019

net framework4.7.2

opencvsharp4.8.0

使用opencv作为推理引擎,CPU推理无需安装cuda+cudnn

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/24 7:50:37

在家也能批量做爆款短视频!MoneyPrinterTurbo+cpolar让你告别手动剪辑!

MoneyPrinterTurbo 是一款面向内容创作者的 AI 短视频自动化生成工具,核心功能覆盖从主题输入到成品输出的全流程 —— 依托通义千问、DeepSeek 等大模型自动撰写脚本,从 Pexels 抓取无版权高清素材,搭配多音色 TTS 配音和智能字幕生成&#…

作者头像 李华
网站建设 2026/3/25 17:01:21

什么是AI 智能体(Agent)

在当今的 AI 浪潮中,我们经常听到“Agent(智能体)”这个词。但实际上,一个能够自主解决问题的 AI Agent 到底是如何工作的?它不仅仅是一个聊天机器人,更是一个拥有“手脚”和“神经系统”的复杂架构。什么是…

作者头像 李华
网站建设 2026/3/23 1:10:26

React Native for OpenHarmony 实战:Vibration 震动反馈详解

React Native for OpenHarmony 实战:Vibration 震动反馈详解 摘要:本文深入探讨 React Native 的 Vibration 模块在 OpenHarmony 平台的实战应用。通过剖析震动反馈的技术原理、跨平台适配要点及性能优化策略,结合 6 个完整可运行的代码示例&…

作者头像 李华
网站建设 2026/3/20 12:40:04

React Native for OpenHarmony 实战:Audio 音频处理详解

React Native for OpenHarmony 实战:Audio 音频处理详解 摘要 本文深入探讨 React Native 在 OpenHarmony 平台上的音频处理解决方案。通过分析音频模块的底层架构、跨平台适配策略及实战代码,提供完整的音频播放、录制、可视化处理实现方案。文章包含…

作者头像 李华