C# Onnx P2PNet 人群检测和计数

效果

项目

代码

cs 复制代码
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;

            // 输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 3, new_height, new_width });

            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(new_width, new_height));

            //输入Tensor
            for (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);
        }
    }
}

下载

源码下载