C# Onnx E2Pose人体关键点检测

2024-06-06 11:12

本文主要是介绍C# Onnx E2Pose人体关键点检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C# Onnx E2Pose人体关键点检测

 

目录

效果

模型信息

项目

代码

下载


效果

模型信息

Inputs
-------------------------
name:inputimg
tensor:Float[1, 3, 512, 512]
---------------------------------------------------------------

Outputs
-------------------------
name:kvxy/concat
tensor:Float[1, 341, 17, 3]
name:pv/concat
tensor:Float[1, 341, 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;
        Mat result_image;
        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_container;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;
        Tensor<float> result_tensors;
        int inpHeight, inpWidth;
        float confThreshold;

        int[] connect_list = { 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 6, 5, 7, 7, 9, 6, 8, 8, 10, 5, 11, 6, 12, 11, 12, 11, 13, 13, 15, 12, 14, 14, 16 };

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

        unsafe 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);

            //将图片转为RGB通道
            Mat image_rgb = new Mat();
            Cv2.CvtColor(image, image_rgb, ColorConversionCodes.BGR2RGB);

            Cv2.Resize(image_rgb, image_rgb, new OpenCvSharp.Size(inpHeight, inpWidth));

            //输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 3, inpHeight, inpWidth });
            for (int y = 0; y < image_rgb.Height; y++)
            {
                for (int x = 0; x < image_rgb.Width; x++)
                {
                    input_tensor[0, 0, y, x] = image_rgb.At<Vec3b>(y, x)[0];
                    input_tensor[0, 1, y, x] = image_rgb.At<Vec3b>(y, x)[1];
                    input_tensor[0, 2, y, x] = image_rgb.At<Vec3b>(y, x)[2];
                }
            }

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("inputimg", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);
            dt2 = DateTime.Now;

            // 将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            float[] kpt = results_onnxvalue[0].AsTensor<float>().ToArray();
            float[] pv = results_onnxvalue[1].AsTensor<float>().ToArray();

            float[] temp = new float[51];

            int num_proposal = 341;
            int num_pts = 17;
            int len = num_pts * 3;

            List<List<int>> results = new List<List<int>>();
            for (int i = 0; i < num_proposal; i++)
            {
                Array.Copy(kpt, i * 51, temp, 0, 51);

                if (pv[i] >= confThreshold)
                {
                    List<int> human_pts = new List<int>();
                    for (int ii = 0; ii < num_pts * 2; ii++)
                    {
                        human_pts.Add(0);
                    }

                    for (int j = 0; j < num_pts; j++)
                    {
                        float score = temp[j * 3] * 2;
                        if (score >= confThreshold)
                        {
                            float x = temp[j * 3 + 1] * image.Cols;
                            float y = temp[j * 3 + 2] * image.Rows;
                            human_pts[j * 2] = (int)x;
                            human_pts[j * 2 + 1] = (int)y;
                        }
                    }
                    results.Add(human_pts);
                }
            }

            result_image = image.Clone();
            int start_x = 0;
            int start_y = 0;
            int end_x = 0;
            int end_y = 0;

            for (int i = 0; i < results.Count; ++i)
            {
                for (int j = 0; j < num_pts; j++)
                {
                    int cx = results[i][j * 2];
                    int cy = results[i][j * 2 + 1];
                    if (cx > 0 && cy > 0)
                    {
                        Cv2.Circle(result_image, new OpenCvSharp.Point(cx, cy), 3, new Scalar(0, 0, 255), -1, LineTypes.AntiAlias);
                    }

                    start_x = results[i][connect_list[j * 2] * 2];
                    start_y = results[i][connect_list[j * 2] * 2 + 1];
                    end_x = results[i][connect_list[j * 2 + 1] * 2];
                    end_y = results[i][connect_list[j * 2 + 1] * 2 + 1];

                    if (start_x > 0 && start_y > 0 && end_x > 0 && end_y > 0)
                    {
                        Cv2.Line(result_image, new OpenCvSharp.Point(start_x, start_y), new OpenCvSharp.Point(end_x, end_y), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);
                    }
                }

                start_x = results[i][connect_list[num_pts * 2] * 2];
                start_y = results[i][connect_list[num_pts * 2] * 2 + 1];
                end_x = results[i][connect_list[num_pts * 2 + 1] * 2];
                end_y = results[i][connect_list[num_pts * 2 + 1] * 2 + 1];

                if (start_x > 0 && start_y > 0 && end_x > 0 && end_y > 0)
                {
                    Cv2.Line(result_image, new OpenCvSharp.Point(start_x, start_y), new OpenCvSharp.Point(end_x, end_y), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);
                }
            }

            pictureBox2.Image = new Bitmap(result_image.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/e2epose_resnet50_1x3x512x512.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<NamedOnnxValue>();

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

            inpWidth = 512;
            inpHeight = 512;

            confThreshold = 0.5f;

        }

        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";
            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;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
    }
}

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;Mat result_image;SessionOptions options;InferenceSession onnx_session;Tensor<float> input_tensor;List<NamedOnnxValue> input_container;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;Tensor<float> result_tensors;int inpHeight, inpWidth;float confThreshold;int[] connect_list = { 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 6, 5, 7, 7, 9, 6, 8, 8, 10, 5, 11, 6, 12, 11, 12, 11, 13, 13, 15, 12, 14, 14, 16 };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;}unsafe 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);//将图片转为RGB通道Mat image_rgb = new Mat();Cv2.CvtColor(image, image_rgb, ColorConversionCodes.BGR2RGB);Cv2.Resize(image_rgb, image_rgb, new OpenCvSharp.Size(inpHeight, inpWidth));//输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, inpHeight, inpWidth });for (int y = 0; y < image_rgb.Height; y++){for (int x = 0; x < image_rgb.Width; x++){input_tensor[0, 0, y, x] = image_rgb.At<Vec3b>(y, x)[0];input_tensor[0, 1, y, x] = image_rgb.At<Vec3b>(y, x)[1];input_tensor[0, 2, y, x] = image_rgb.At<Vec3b>(y, x)[2];}}//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("inputimg", input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);dt2 = DateTime.Now;// 将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();float[] kpt = results_onnxvalue[0].AsTensor<float>().ToArray();float[] pv = results_onnxvalue[1].AsTensor<float>().ToArray();float[] temp = new float[51];int num_proposal = 341;int num_pts = 17;int len = num_pts * 3;List<List<int>> results = new List<List<int>>();for (int i = 0; i < num_proposal; i++){Array.Copy(kpt, i * 51, temp, 0, 51);if (pv[i] >= confThreshold){List<int> human_pts = new List<int>();for (int ii = 0; ii < num_pts * 2; ii++){human_pts.Add(0);}for (int j = 0; j < num_pts; j++){float score = temp[j * 3] * 2;if (score >= confThreshold){float x = temp[j * 3 + 1] * image.Cols;float y = temp[j * 3 + 2] * image.Rows;human_pts[j * 2] = (int)x;human_pts[j * 2 + 1] = (int)y;}}results.Add(human_pts);}}result_image = image.Clone();int start_x = 0;int start_y = 0;int end_x = 0;int end_y = 0;for (int i = 0; i < results.Count; ++i){for (int j = 0; j < num_pts; j++){int cx = results[i][j * 2];int cy = results[i][j * 2 + 1];if (cx > 0 && cy > 0){Cv2.Circle(result_image, new OpenCvSharp.Point(cx, cy), 3, new Scalar(0, 0, 255), -1, LineTypes.AntiAlias);}start_x = results[i][connect_list[j * 2] * 2];start_y = results[i][connect_list[j * 2] * 2 + 1];end_x = results[i][connect_list[j * 2 + 1] * 2];end_y = results[i][connect_list[j * 2 + 1] * 2 + 1];if (start_x > 0 && start_y > 0 && end_x > 0 && end_y > 0){Cv2.Line(result_image, new OpenCvSharp.Point(start_x, start_y), new OpenCvSharp.Point(end_x, end_y), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);}}start_x = results[i][connect_list[num_pts * 2] * 2];start_y = results[i][connect_list[num_pts * 2] * 2 + 1];end_x = results[i][connect_list[num_pts * 2 + 1] * 2];end_y = results[i][connect_list[num_pts * 2 + 1] * 2 + 1];if (start_x > 0 && start_y > 0 && end_x > 0 && end_y > 0){Cv2.Line(result_image, new OpenCvSharp.Point(start_x, start_y), new OpenCvSharp.Point(end_x, end_y), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);}}pictureBox2.Image = new Bitmap(result_image.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/e2epose_resnet50_1x3x512x512.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<NamedOnnxValue>();image_path = "test_img/1.jpg";pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);inpWidth = 512;inpHeight = 512;confThreshold = 0.5f;}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";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;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}}
}

下载

源码下载

这篇关于C# Onnx E2Pose人体关键点检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035945

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

C#多线程编程中导致死锁的常见陷阱和避免方法

《C#多线程编程中导致死锁的常见陷阱和避免方法》在C#多线程编程中,死锁(Deadlock)是一种常见的、令人头疼的错误,死锁通常发生在多个线程试图获取多个资源的锁时,导致相互等待对方释放资源,最终形... 目录引言1. 什么是死锁?死锁的典型条件:2. 导致死锁的常见原因2.1 锁的顺序问题错误示例:不同

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬