• C# Onnx Yolov8 Pose 姿态识别


    目录

    效果

    模型信息

    项目

    代码

    下载 


    效果

    模型信息

    Model Properties
    -------------------------
    date:2023-09-07T17:11:43.091306
    description:Ultralytics YOLOv8n-pose model trained on /usr/src/app/ultralytics/datasets/coco-pose.yaml
    author:Ultralytics
    kpt_shape:[17, 3]
    task:pose
    license:AGPL-3.0 https://ultralytics.com/license
    version:8.0.172
    stride:32
    batch:1
    imgsz:[640, 640]
    names:{0: 'person'}
    ---------------------------------------------------------------

    Inputs
    -------------------------
    name:images
    tensor:Float[1, 3, 640, 640]
    ---------------------------------------------------------------

    Outputs
    -------------------------
    name:output0
    tensor:Float[1, 56, 8400]
    ---------------------------------------------------------------

    项目

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    Microsoft.ML.OnnxRuntime 1.16.2

    代码

    // 配置图片数据
    image = new Mat(image_path);
    int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    image.CopyTo(new Mat(max_image, roi));

    float[] result_array = new float[8400 * 56];
    float[] factors = new float[2];
    factors[0] = factors[1] = (float)(max_image_length / 640.0);

    // 将图片转为RGB通道
    Mat image_rgb = new Mat();
    Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
    Mat resize_image = new Mat();
    Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));

    // 输入Tensor
    // input_tensor = new DenseTensor(new[] { 1, 3, 640, 640 });
    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(y, x)[0] / 255f;
            input_tensor[0, 1, y, x] = resize_image.At(y, x)[1] / 255f;
            input_tensor[0, 2, y, x] = resize_image.At(y, x)[2] / 255f;
        }
    }

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

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

    dt2 = DateTime.Now;

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

    // 读取第一个节点输出并转为Tensor数据
    result_tensors = results_onnxvalue[0].AsTensor();

    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.ComponentModel;
    7. using System.Data;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Text;
    11. using System.Windows.Forms;
    12. using static System.Net.Mime.MediaTypeNames;
    13. namespace Onnx_Yolov8_Demo
    14. {
    15. public partial class Form1 : Form
    16. {
    17. public Form1()
    18. {
    19. InitializeComponent();
    20. }
    21. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    22. string image_path = "";
    23. string startupPath;
    24. string classer_path;
    25. DateTime dt1 = DateTime.Now;
    26. DateTime dt2 = DateTime.Now;
    27. string model_path;
    28. Mat image;
    29. PoseResult result_pro;
    30. Mat result_image;
    31. SessionOptions options;
    32. InferenceSession onnx_session;
    33. Tensor<float> input_tensor;
    34. List<NamedOnnxValue> input_ontainer;
    35. IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
    36. DisposableNamedOnnxValue[] results_onnxvalue;
    37. Tensor<float> result_tensors;
    38. private void button1_Click(object sender, EventArgs e)
    39. {
    40. OpenFileDialog ofd = new OpenFileDialog();
    41. ofd.Filter = fileFilter;
    42. if (ofd.ShowDialog() != DialogResult.OK) return;
    43. pictureBox1.Image = null;
    44. image_path = ofd.FileName;
    45. pictureBox1.Image = new Bitmap(image_path);
    46. textBox1.Text = "";
    47. image = new Mat(image_path);
    48. pictureBox2.Image = null;
    49. }
    50. private void button2_Click(object sender, EventArgs e)
    51. {
    52. if (image_path == "")
    53. {
    54. return;
    55. }
    56. // 配置图片数据
    57. image = new Mat(image_path);
    58. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    59. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    60. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    61. image.CopyTo(new Mat(max_image, roi));
    62. float[] result_array = new float[8400 * 56];
    63. float[] factors = new float[2];
    64. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    65. // 将图片转为RGB通道
    66. Mat image_rgb = new Mat();
    67. Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
    68. Mat resize_image = new Mat();
    69. Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
    70. // 输入Tensor
    71. // input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
    72. for (int y = 0; y < resize_image.Height; y++)
    73. {
    74. for (int x = 0; x < resize_image.Width; x++)
    75. {
    76. input_tensor[0, 0, y, x] = resize_image.At<Vec3b>(y, x)[0] / 255f;
    77. input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1] / 255f;
    78. input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2] / 255f;
    79. }
    80. }
    81. //input_tensor 放入一个输入参数的容器,并指定名称
    82. input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));
    83. dt1 = DateTime.Now;
    84. //运行 Inference 并获取结果
    85. result_infer = onnx_session.Run(input_ontainer);
    86. dt2 = DateTime.Now;
    87. // 将输出结果转为DisposableNamedOnnxValue数组
    88. results_onnxvalue = result_infer.ToArray();
    89. // 读取第一个节点输出并转为Tensor数据
    90. result_tensors = results_onnxvalue[0].AsTensor<float>();
    91. result_array = result_tensors.ToArray();
    92. resize_image.Dispose();
    93. image_rgb.Dispose();
    94. PoseResult result_pro = new PoseResult(factors);
    95. result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());
    96. if (!result_image.Empty())
    97. {
    98. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    99. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    100. }
    101. else
    102. {
    103. textBox1.Text = "无信息";
    104. }
    105. }
    106. private void Form1_Load(object sender, EventArgs e)
    107. {
    108. startupPath = System.Windows.Forms.Application.StartupPath;
    109. model_path = startupPath + "\\yolov8n-pose.onnx";
    110. classer_path = startupPath + "\\yolov8-detect-lable.txt";
    111. // 创建输出会话,用于输出模型读取信息
    112. options = new SessionOptions();
    113. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    114. // 设置为CPU上运行
    115. options.AppendExecutionProvider_CPU(0);
    116. // 创建推理模型类,读取本地模型文件
    117. onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
    118. // 输入Tensor
    119. input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
    120. // 创建输入容器
    121. input_ontainer = new List<NamedOnnxValue>();
    122. }
    123. }
    124. }

    下载 

    Demo下载

  • 相关阅读:
    算法通关村第18关【青铜】| 回溯
    kubeadm升级k8s集群
    java设计模式1,单一职责原则
    SkeyeGisMap地图扩展(五) 自定义形状
    Linux本地docker一键部署traefik+内网穿透工具实现远程访问Web UI管理界面
    在微信小程序上做一个「博客园年度总结」:小程序部分交互效果实现
    [附源码]Python计算机毕业设计Django春晓学堂管理系统
    在职读博|中国社科院-英国斯特大学灵合作办学双证管理学博士
    数据结构-单链表(增删查改)
    事件总线EventBus
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132845722