首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OnnxRuntime MPCount 人群计数

  • 25-02-19 03:41
  • 4007
  • 5384
blog.csdn.net

C# OnnxRuntime MPCount 人群计数

目录

说明

效果

模型信息

项目

代码

下载


说明

官网地址:https://github.com/Shimmer93/MPCount

代码实现参考:https://github.com/hpc203/MPCount-onnxrun

效果

C# OnnxRuntime MPCount 人群计数

模型信息

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[-1, 1, -1, -1]
name:c
tensor:Float[-1, 1, -1, -1]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;

namespace Onnx_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;
        SessionOptions options;
        InferenceSession onnx_session;
        List input_container;

        int unit_size = 16;
        int log_para = 1000;

        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;
            }

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";
            Application.DoEvents();

            //读图片
            image = new Mat(image_path);

            Mat x = new Mat();
            int left = 0;
            int top = 0;
            int right = 0;
            int bottom = 0;
            Common.preprocess(unit_size,image, x, ref left, ref top, ref right, ref bottom);

            int[] input_shape_ = new int[] { 1, 3, x.Rows, x.Cols };
            x.ConvertTo(x, MatType.CV_32FC3, 1 / 127.5, -1.0);

            float[] input_image = Common.ExtractMat(x);
            x.Release();

            Tensor input_tensor = new DenseTensor(input_image, input_shape_);
            List input_container2 = new List
                {
                    NamedOnnxValue.CreateFromTensor("input", input_tensor)
                };
            dt1 = DateTime.Now;
            var result_infer = onnx_session.Run(input_container2).ToArray();
            dt2 = DateTime.Now;

            var ort_outputs = result_infer.ToArray();
            ReadOnlySpan out_shape = ort_outputs[0].AsTensor().Dimensions;
            int outHeight = out_shape[2];
            int outWidth = out_shape[3];
            float[] pred = ort_outputs[0].AsTensor().ToArray();

            Mat result = new Mat(outHeight, outWidth, MatType.CV_32FC1, pred);
            Rect crop_roi = new Rect(left, top, outWidth - right - left, outHeight - top - bottom);

            Mat result_map = Mat.Zeros(new OpenCvSharp.Size(outWidth, outHeight), MatType.CV_32FC1);
            result.CopyTo(new Mat(result_map, crop_roi));

            int people_count = (int)(Cv2.Sum(result_map)[0] / log_para);

            Mat drawimg = Common.draw_result(image, result_map, people_count);

            pictureBox2.Image = new Bitmap(drawimg.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

            button2.Enabled = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;
            model_path = "model/MPCount_qnrf.onnx";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

            // 创建输入容器
            input_container = new List();

            image_path = "test_img/0.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);

        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        SaveFileDialog sdf = new SaveFileDialog();
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }

                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
    }
}

  1. using Microsoft.ML.OnnxRuntime;
  2. using Microsoft.ML.OnnxRuntime.Tensors;
  3. using OpenCvSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using System.Windows.Forms;
  10. namespace Onnx_Demo
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  19. string image_path = "";
  20. string startupPath;
  21. DateTime dt1 = DateTime.Now;
  22. DateTime dt2 = DateTime.Now;
  23. string model_path;
  24. Mat image;
  25. SessionOptions options;
  26. InferenceSession onnx_session;
  27. List<NamedOnnxValue> input_container;
  28. int unit_size = 16;
  29. int log_para = 1000;
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32. OpenFileDialog ofd = new OpenFileDialog();
  33. ofd.Filter = fileFilter;
  34. if (ofd.ShowDialog() != DialogResult.OK) return;
  35. pictureBox1.Image = null;
  36. image_path = ofd.FileName;
  37. pictureBox1.Image = new Bitmap(image_path);
  38. textBox1.Text = "";
  39. image = new Mat(image_path);
  40. pictureBox2.Image = null;
  41. }
  42. private void button2_Click(object sender, EventArgs e)
  43. {
  44. if (image_path == "")
  45. {
  46. return;
  47. }
  48. button2.Enabled = false;
  49. pictureBox2.Image = null;
  50. textBox1.Text = "";
  51. Application.DoEvents();
  52. //读图片
  53. image = new Mat(image_path);
  54. Mat x = new Mat();
  55. int left = 0;
  56. int top = 0;
  57. int right = 0;
  58. int bottom = 0;
  59. Common.preprocess(unit_size,image, x, ref left, ref top, ref right, ref bottom);
  60. int[] input_shape_ = new int[] { 1, 3, x.Rows, x.Cols };
  61. x.ConvertTo(x, MatType.CV_32FC3, 1 / 127.5, -1.0);
  62. float[] input_image = Common.ExtractMat(x);
  63. x.Release();
  64. Tensor<float> input_tensor = new DenseTensor<float>(input_image, input_shape_);
  65. List<NamedOnnxValue> input_container2 = new List<NamedOnnxValue>
  66. {
  67. NamedOnnxValue.CreateFromTensor("input", input_tensor)
  68. };
  69. dt1 = DateTime.Now;
  70. var result_infer = onnx_session.Run(input_container2).ToArray();
  71. dt2 = DateTime.Now;
  72. var ort_outputs = result_infer.ToArray();
  73. ReadOnlySpan<int> out_shape = ort_outputs[0].AsTensor<float>().Dimensions;
  74. int outHeight = out_shape[2];
  75. int outWidth = out_shape[3];
  76. float[] pred = ort_outputs[0].AsTensor<float>().ToArray();
  77. Mat result = new Mat(outHeight, outWidth, MatType.CV_32FC1, pred);
  78. Rect crop_roi = new Rect(left, top, outWidth - right - left, outHeight - top - bottom);
  79. Mat result_map = Mat.Zeros(new OpenCvSharp.Size(outWidth, outHeight), MatType.CV_32FC1);
  80. result.CopyTo(new Mat(result_map, crop_roi));
  81. int people_count = (int)(Cv2.Sum(result_map)[0] / log_para);
  82. Mat drawimg = Common.draw_result(image, result_map, people_count);
  83. pictureBox2.Image = new Bitmap(drawimg.ToMemoryStream());
  84. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  85. button2.Enabled = true;
  86. }
  87. private void Form1_Load(object sender, EventArgs e)
  88. {
  89. startupPath = System.Windows.Forms.Application.StartupPath;
  90. model_path = "model/MPCount_qnrf.onnx";
  91. // 创建输出会话,用于输出模型读取信息
  92. options = new SessionOptions();
  93. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
  94. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
  95. // 创建推理模型类,读取本地模型文件
  96. onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
  97. // 创建输入容器
  98. input_container = new List<NamedOnnxValue>();
  99. image_path = "test_img/0.jpg";
  100. pictureBox1.Image = new Bitmap(image_path);
  101. image = new Mat(image_path);
  102. }
  103. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  104. {
  105. Common.ShowNormalImg(pictureBox1.Image);
  106. }
  107. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  108. {
  109. Common.ShowNormalImg(pictureBox2.Image);
  110. }
  111. SaveFileDialog sdf = new SaveFileDialog();
  112. private void button3_Click(object sender, EventArgs e)
  113. {
  114. if (pictureBox2.Image == null)
  115. {
  116. return;
  117. }
  118. Bitmap output = new Bitmap(pictureBox2.Image);
  119. sdf.Title = "保存";
  120. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
  121. if (sdf.ShowDialog() == DialogResult.OK)
  122. {
  123. switch (sdf.FilterIndex)
  124. {
  125. case 1:
  126. {
  127. output.Save(sdf.FileName, ImageFormat.Jpeg);
  128. break;
  129. }
  130. case 2:
  131. {
  132. output.Save(sdf.FileName, ImageFormat.Png);
  133. break;
  134. }
  135. case 3:
  136. {
  137. output.Save(sdf.FileName, ImageFormat.Bmp);
  138. break;
  139. }
  140. case 4:
  141. {
  142. output.Save(sdf.FileName, ImageFormat.Emf);
  143. break;
  144. }
  145. case 5:
  146. {
  147. output.Save(sdf.FileName, ImageFormat.Exif);
  148. break;
  149. }
  150. case 6:
  151. {
  152. output.Save(sdf.FileName, ImageFormat.Gif);
  153. break;
  154. }
  155. case 7:
  156. {
  157. output.Save(sdf.FileName, ImageFormat.Icon);
  158. break;
  159. }
  160. case 8:
  161. {
  162. output.Save(sdf.FileName, ImageFormat.Tiff);
  163. break;
  164. }
  165. case 9:
  166. {
  167. output.Save(sdf.FileName, ImageFormat.Wmf);
  168. break;
  169. }
  170. }
  171. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  172. }
  173. }
  174. }
  175. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/141092803"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top