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

C# OpenCvSharp Yolov8 Detect 目标检测

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace OpenCvSharp_Yolov8_Demo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;string classer_path;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;string model_path;Mat image;DetectionResult result_pro;Mat result_mat;Mat result_image;Mat result_mat_to_float;Net opencv_net;Mat BN_image;float[] result_array;float[] factors;int max_image_length;Mat max_image;Rect roi;Result result;StringBuilder sb = new StringBuilder();private void Form1_Load(object sender, EventArgs e){startupPath = System.Windows.Forms.Application.StartupPath;model_path = startupPath + "\\yolov8n.onnx";classer_path = startupPath + "\\yolov8-detect-lable.txt";//初始化网络类,读取本地模型opencv_net = CvDnn.ReadNetFromOnnx(model_path);result_array = new float[8400 * 84];factors = new float[2];}private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);textBox1.Text = "";image = new Mat(image_path);pictureBox2.Image = null;}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}//缩放图片max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);roi = new Rect(0, 0, image.Cols, image.Rows);image.CopyTo(new Mat(max_image, roi));factors[0] = factors[1] = (float)(max_image_length / 640.0);//数据归一化处理BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);//配置图片输入数据opencv_net.SetInput(BN_image);dt1 = DateTime.Now;//模型推理,读取推理结果result_mat = opencv_net.Forward();dt2 = DateTime.Now;//将推理结果转为float数据类型result_mat_to_float = new Mat(8400, 84, MatType.CV_32F, result_mat.Data);//将数据读取到数组中result_mat_to_float.GetArray<float>(out result_array);result_pro = new DetectionResult(classer_path, factors);result = result_pro.process_result(result_array);result_image = result_pro.draw_result(result, image.Clone());if (!result_image.Empty()){pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());sb.Clear();sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");sb.AppendLine("------------------------------");for (int i = 0; i < result.length; i++){sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})", result.classes[i], result.scores[i].ToString("0.00"), result.rects[i].TopLeft.X, result.rects[i].TopLeft.Y, result.rects[i].BottomRight.X, result.rects[i].BottomRight.Y));}textBox1.Text = sb.ToString();}else{textBox1.Text = "无信息";}}}
}

Demo下载 

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

相关文章:

  • rust数组
  • ubuntu | 安装NVIDIA套件:驱动、CUDA、cuDNN
  • JAVA学习笔记
  • 车载软件架构 —— 持续集成持续交付
  • c++ 二元运算符重载, 以加法为例
  • 基于 SpringBoot+Vue的电影影城管理系统,附源码,数据库
  • Docker实战技巧(二):Kubernetes基础操作实战
  • 计算机视觉与深度学习-循环神经网络与注意力机制-Attention(注意力机制)-【北邮鲁鹏】
  • Centos7安装wps无法打开及字体缺失的问题解决
  • 华为OD机试真题-会议接待-2023年OD统一考试(B卷)
  • mysql explain学习记录
  • 电压放大电路的作用有哪些(电压放大器)
  • 编译opencv-3.4.5 [交叉编译]
  • Canal 实现MySQL与Elasticsearch7数据同步
  • 网络安全攻防对抗之隐藏通信隧道技术整理
  • 读书笔记:多Transformer的双向编码器表示法(Bert)-2
  • Python 基于PyCharm断点调试
  • spring security auth2.0实现
  • MySQL(6)LOCK和MVCC
  • 最新IDE流行度最新排名(每月更新)
  • I2C的上拉电阻如何计算?
  • Centos下安装 oracle11g 博客2
  • 记一次逆向某医院挂号软件的经历
  • Canal实现Mysql数据同步至Redis、Elasticsearch
  • Kafka的消息传递保证和一致性
  • Docker 部署 Firefly III 服务
  • 配置OSPFv3基本功能 华为笔记
  • 【AI视野·今日Sound 声学论文速览 第九期】Thu, 21 Sep 2023
  • 数据结构-----堆(完全二叉树)
  • set/multiset容器、map容器