• C# OpenCvSharp Yolov8 Pose 姿态识别


    目录

    效果

    项目

    模型信息

    代码

    下载 


    效果

    项目

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    Microsoft.ML.OnnxRuntime 1.16.2

    模型信息

    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]
    ---------------------------------------------------------------

    代码

    //缩放图片
    max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    roi = new Rect(0, 0, image.Cols, image.Rows);
    image.CopyTo(new Mat(max_image, roi));

    factors[0] = factors[1] = (float)(max_image_length / 640.0);

    //数据归一化处理
    BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);

    //配置图片输入数据
    opencv_net.SetInput(BN_image);

    dt1 = DateTime.Now;
    //模型推理,读取推理结果
    result_mat = opencv_net.Forward();
    dt2 = DateTime.Now;

    //将推理结果转为float数据类型
    result_mat_to_float = new Mat(8400, 56, MatType.CV_32F, result_mat.Data);

    //将数据读取到数组中
    result_mat_to_float.GetArray(out result_array);

    result = result_pro.process_result(result_array);

    result_image = result_pro.draw_result(result, image.Clone());

    if (!result_image.Empty())
    {
        pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        sb.Clear();
        sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
        sb.AppendLine("------------------------------");
        textBox1.Text = sb.ToString();
    }
    else
    {
        textBox1.Text = "无信息";
    }

    1. using OpenCvSharp;
    2. using OpenCvSharp.Dnn;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.ComponentModel;
    6. using System.Data;
    7. using System.Drawing;
    8. using System.Linq;
    9. using System.Text;
    10. using System.Windows.Forms;
    11. namespace OpenCvSharp_Yolov8_Demo
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    20. string image_path = "";
    21. string startupPath;
    22. string classer_path;
    23. DateTime dt1 = DateTime.Now;
    24. DateTime dt2 = DateTime.Now;
    25. string model_path;
    26. Mat image;
    27. PoseResult result_pro;
    28. Mat result_mat;
    29. Mat result_image;
    30. Mat result_mat_to_float;
    31. Net opencv_net;
    32. Mat BN_image;
    33. float[] result_array;
    34. float[] factors;
    35. int max_image_length;
    36. Mat max_image;
    37. Rect roi;
    38. Result result;
    39. StringBuilder sb = new StringBuilder();
    40. private void Form1_Load(object sender, EventArgs e)
    41. {
    42. startupPath = System.Windows.Forms.Application.StartupPath;
    43. model_path = startupPath + "\\yolov8n-pose.onnx";
    44. classer_path = startupPath + "\\yolov8-detect-lable.txt";
    45. //初始化网络类,读取本地模型
    46. opencv_net = CvDnn.ReadNetFromOnnx(model_path);
    47. result_array = new float[8400 * 56];
    48. factors = new float[2];
    49. result_pro = new PoseResult(factors);
    50. }
    51. private void button1_Click(object sender, EventArgs e)
    52. {
    53. OpenFileDialog ofd = new OpenFileDialog();
    54. ofd.Filter = fileFilter;
    55. if (ofd.ShowDialog() != DialogResult.OK) return;
    56. pictureBox1.Image = null;
    57. image_path = ofd.FileName;
    58. pictureBox1.Image = new Bitmap(image_path);
    59. textBox1.Text = "";
    60. image = new Mat(image_path);
    61. pictureBox2.Image = null;
    62. }
    63. private void button2_Click(object sender, EventArgs e)
    64. {
    65. if (image_path == "")
    66. {
    67. return;
    68. }
    69. //缩放图片
    70. max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    71. max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    72. roi = new Rect(0, 0, image.Cols, image.Rows);
    73. image.CopyTo(new Mat(max_image, roi));
    74. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    75. //数据归一化处理
    76. BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(640, 640), new Scalar(0, 0, 0), true, false);
    77. //配置图片输入数据
    78. opencv_net.SetInput(BN_image);
    79. dt1 = DateTime.Now;
    80. //模型推理,读取推理结果
    81. result_mat = opencv_net.Forward();
    82. dt2 = DateTime.Now;
    83. //将推理结果转为float数据类型
    84. result_mat_to_float = new Mat(8400, 56, MatType.CV_32F, result_mat.Data);
    85. //将数据读取到数组中
    86. result_mat_to_float.GetArray<float>(out result_array);
    87. result = result_pro.process_result(result_array);
    88. result_image = result_pro.draw_result(result, image.Clone());
    89. if (!result_image.Empty())
    90. {
    91. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    92. sb.Clear();
    93. sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
    94. sb.AppendLine("------------------------------");
    95. textBox1.Text = sb.ToString();
    96. }
    97. else
    98. {
    99. textBox1.Text = "无信息";
    100. }
    101. }
    102. }
    103. }

    下载 

    Demo下载

  • 相关阅读:
    【VPX302】基于3U VPX总线架构的高性能数据预处理平台
    vue-element学习(一)
    Arouter源码系列之拦截器原理详解
    本地mysql服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
    国际贸易详解:国际贸易主要有哪些分类标准和运输方式
    LinkWeChat 私域管理平台基于企业微信的开源 SCRM
    【数据库查询表结构】
    基于粒子群算法的电力系统无功优化研究(IEEE14节点)(Matlab代码实现)
    决策树和随机森林
    15. SAP ABAP OData 服务里 EntityType 和 EntitySet 的区别
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133632260