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

C# Onnx P2PNet 人群检测和计数

效果

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Onnx_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;string model_path;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;Mat image;Mat result_image;SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;List<NamedOnnxValue> input_ontainer;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;StringBuilder sb = new StringBuilder();float confThreshold = 0.5f;float[] mean = { 0.485f, 0.456f, 0.406f };float[] std = { 0.229f, 0.224f, 0.225f };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){startupPath = Application.StartupPath + "\\model\\";model_path = startupPath + "SHTechA.onnx";// 创建输出会话options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行// 创建推理模型类,读取本地模型文件onnx_session = new InferenceSession(model_path, options);// 创建输入容器input_ontainer = new List<NamedOnnxValue>();}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();//图片image = new Mat(image_path);//将图片转为RGB通道Mat image_rgb = new Mat();Cv2.CvtColor(image, image_rgb, ColorConversionCodes.BGR2RGB);Mat resize_image = new Mat();int srch = image.Rows, srcw = image.Cols;int new_width = srcw / 128 * 128;int new_height = srch / 128 * 128;// 输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, new_height, new_width });Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(new_width, new_height));//输入Tensorfor (int y = 0; y < resize_image.Height; y++){for (int x = 0; x < resize_image.Width; x++){input_tensor[0, 0, y, x] = (resize_image.At<Vec3b>(y, x)[0] / 255f - mean[0]) / std[0];input_tensor[0, 1, y, x] = (resize_image.At<Vec3b>(y, x)[1] / 255f - mean[1]) / std[1];input_tensor[0, 2, y, x] = (resize_image.At<Vec3b>(y, x)[2] / 255f - mean[2]) / std[2];}}//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);dt2 = DateTime.Now;//将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();List<int> pyramid_levels = new List<int>(1) { 3 };List<float> all_anchor_points = new List<float>();Common.generate_anchor_points(resize_image.Cols, resize_image.Rows, pyramid_levels, 2, 2, all_anchor_points);var pscore = results_onnxvalue[0].AsTensor<float>().ToArray();var pcoord = results_onnxvalue[1].AsTensor<float>().ToArray();int num_proposal = pscore.Length;List<CrowdPoint> crowd_points = new List<CrowdPoint>();for (int i = 0; i < num_proposal; i++){if (pscore[i] >= confThreshold){float x = (pcoord[i] + all_anchor_points[i * 2]) / (float)resize_image.Width * (float)image.Width;float y = (pcoord[i + 1] + all_anchor_points[i * 2 + 1]) / (float)resize_image.Height * (float)image.Height;crowd_points.Add(new CrowdPoint(new OpenCvSharp.Point(x, y), pscore[i]));}}result_image = image.Clone();sb.Clear();sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");sb.AppendLine("------------------------------");sb.AppendLine("人数:" + crowd_points.Count);for (int i = 0; i < crowd_points.Count; i++){Cv2.Circle(result_image, crowd_points[i].pt.X, crowd_points[i].pt.Y, 2, new Scalar(0, 0, 255), -1);//Cv2.PutText(result_image, (i+1).ToString()+"-" + crowd_points[i].prob.ToString("0.00"), crowd_points[i].pt, HersheyFonts.HersheySimplex, 1.0, new Scalar(0, 255, 0), 2); ;sb.AppendLine((i + 1).ToString() + "-" + crowd_points[i].prob.ToString("0.00"));}pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());textBox1.Text = sb.ToString();}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}

下载

源码下载

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

相关文章:

  • idea提交代码一直提示 log into gitee
  • ATECLOUD如何进行电源模块各项性能指标的测试?
  • Mysql查询训练——50道题
  • 学习笔记|正态分布|图形法|偏度和峰度|非参数检验法|《小白爱上SPSS》课程:SPSS第三讲 | 正态分布怎么检验?看这篇文章就够了
  • Android NDK开发详解之ndk-build 脚本
  • 应用于智慧矿山的皮带跑偏视频分析AI算法
  • vue3 UI组件优化之element-plus按需导入
  • 如何创建 Spring Boot 项目
  • 【经验分享】openGauss容灾集群搭建
  • 互联网应用架构的演进(八大架构的演进过程)
  • ROS自学笔记二十六:导航中激光雷达消息
  • 分类模型的评价指标
  • 第五章 I/O管理 八、缓冲区管理
  • 笔记软件推荐!亲测好用的8款笔记软件!
  • MPJQueryWrapper 用法
  • 50元买来的iPhone手机刷机经验
  • 数据结构学习笔记——链式表示中的双链表及循环单/双链表
  • DC电源模块去除输出电源中的高频噪声及杂波
  • 【驱动开发】注册字符设备使用gpio设备树节点控制led三盏灯的亮灭
  • 面向制造企业的持续发展,2023数字化工单管理系统创新篇章-亿发
  • mysql 元数据锁 MDL读锁与MDL写锁
  • 批量预处理哨兵2影像
  • Unity地面交互效果——2、动态法线贴图实现轨迹效果
  • 视频剪辑达人教您:如何运用嵌套合并技巧制作固定片尾
  • 【腾讯云 TDSQL-C Serverless 产品体验】TDSQL-C MySQL Serverless最佳实践
  • SQLyog连接数据库报plugin caching_sha2_password could not be loaded......解决方案
  • linux应急排查
  • Apache POI及easyExcel读取及写入excel文件
  • 为什么写作
  • python基于VGG19实现图像风格迁移