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

C# OpenCvSharp DNN 部署yolov4目标检测

目录

效果

项目

代码

下载


效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;namespace OpenCvSharp_DNN_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;float confThreshold;float nmsThreshold;int inpHeight;int inpWidth;List<string> class_names;int num_class;Net opencv_net;Mat BN_image;Mat image;Mat result_image;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){confThreshold = 0.5f;nmsThreshold = 0.4f;inpHeight = 416;inpWidth = 416;opencv_net = CvDnn.ReadNetFromDarknet("model/yolov4.cfg", "model/yolov4.weights");class_names = new List<string>();StreamReader sr = new StreamReader("model/coco.names");string line;while ((line = sr.ReadLine()) != null){class_names.Add(line);}num_class = class_names.Count();image_path = "test_img/dog.jpg";pictureBox1.Image = new Bitmap(image_path);}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();image = new Mat(image_path);BN_image = CvDnn.BlobFromImage(image, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);//配置图片输入数据opencv_net.SetInput(BN_image);//模型推理,读取推理结果var outNames = opencv_net.GetUnconnectedOutLayersNames();var outs = outNames.Select(_ => new Mat()).ToArray();dt1 = DateTime.Now;opencv_net.Forward(outs, outNames);dt2 = DateTime.Now;List<int> classIds = new List<int>();List<float> confidences = new List<float>();List<Rect> boxes = new List<Rect>();for (int i = 0; i < outs.Length; ++i){float* data = (float*)outs[i].Data;for (int j = 0; j < outs[i].Rows; ++j, data += outs[i].Cols){Mat scores = outs[i].Row(j).ColRange(5, outs[i].Cols);double minVal, max_class_socre;OpenCvSharp.Point minLoc, classIdPoint;// Get the value and location of the maximum scoreCv2.MinMaxLoc(scores, out minVal, out max_class_socre, out minLoc, out classIdPoint);if (max_class_socre > confThreshold){int centerX = (int)(data[0] * image.Cols);int centerY = (int)(data[1] * image.Rows);int width = (int)(data[2] * image.Cols);int height = (int)(data[3] * image.Rows);int left = centerX - width / 2;int top = centerY - height / 2;classIds.Add(classIdPoint.X);confidences.Add((float)max_class_socre);boxes.Add(new Rect(left, top, width, height));}}}int[] indices;CvDnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, out indices);result_image = image.Clone();for (int i = 0; i < indices.Length; ++i){int idx = indices[i];Rect box = boxes[idx];Cv2.Rectangle(result_image, new OpenCvSharp.Point(box.X, box.Y), new OpenCvSharp.Point(box.X + box.Width, box.Y + box.Height), new Scalar(0, 0, 255), 2);string label = class_names[classIds[idx]] + ":" + confidences[idx].ToString("0.00");Cv2.PutText(result_image, label, new OpenCvSharp.Point(box.X, box.Y - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);}pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

源码下载

相关文章:

  • Linux 性能调优之存储设备调优
  • GADM 4.1 全球国家行政区划下载
  • 代码随想录算法训练营第39天(动态规划02● 62.不同路径 ● 63. 不同路径 II
  • 揭秘:IT行业有哪些证书含金量高?
  • Python算法题集_矩阵置零
  • app对接优量汇收益如何?
  • CSS 控制 video 标签的控制栏组件的显隐
  • 新零售的升维体验,摸索华为云GaussDB如何实现数据赋能
  • 推动海外云手机发展的几个因素
  • jbdc的简单了解
  • 滑动一整屏
  • LeetCode:9.回文数,对整数的反转操作
  • 通过无线打通两个路由器
  • 深入探讨Python中的装饰器技术
  • C语言贪吃蛇详解
  • Akka系列(七):Actor持久化之Akka persistence
  • Angular 响应式表单 基础例子
  • CentOS 7 防火墙操作
  • docker python 配置
  • Golang-长连接-状态推送
  • Java IO学习笔记一
  • JavaScript 奇技淫巧
  • LintCode 31. partitionArray 数组划分
  • Redis学习笔记 - pipline(流水线、管道)
  • Selenium实战教程系列(二)---元素定位
  • Vue学习第二天
  • 阿里云前端周刊 - 第 26 期
  • 程序员该如何有效的找工作?
  • 从0到1:PostCSS 插件开发最佳实践
  • 大整数乘法-表格法
  • 代理模式
  • 分布式事物理论与实践
  • 构建二叉树进行数值数组的去重及优化
  • 排序算法学习笔记
  • 盘点那些不知名却常用的 Git 操作
  • 手机端车牌号码键盘的vue组件
  • 一个JAVA程序员成长之路分享
  • 最近的计划
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • 函数计算新功能-----支持C#函数
  • 移动端高清、多屏适配方案
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​低代码平台的核心价值与优势
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • ###项目技术发展史
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #include<初见C语言之指针(5)>
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (day 12)JavaScript学习笔记(数组3)
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (第一天)包装对象、作用域、创建对象
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统