• C# OpenVino 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]
    ---------------------------------------------------------------

    项目

    代码

    // 图片缩放
    Mat 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);

    byte[] image_data = max_image.ImEncode(".bmp");
    //存储byte的长度
    ulong image_size = Convert.ToUInt64(image_data.Length);
    // 加载推理图片数据
    core.load_input_data("images", image_data, image_size, 1);

    dt1 = DateTime.Now;
    // 模型推理
    core.infer();
    dt2 = DateTime.Now;

    // 读取推理结果
    result_array = core.read_infer_result("output0", 8400 * 56);

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using OpenCvSharp;
    10. namespace OpenVino_Yolov8_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. StringBuilder sb = new StringBuilder();
    25. Core core;
    26. Mat image;
    27. PoseResult result_pro;
    28. private void button1_Click(object sender, EventArgs e)
    29. {
    30. OpenFileDialog ofd = new OpenFileDialog();
    31. ofd.Filter = fileFilter;
    32. if (ofd.ShowDialog() != DialogResult.OK) return;
    33. pictureBox1.Image = null;
    34. image_path = ofd.FileName;
    35. pictureBox1.Image = new Bitmap(image_path);
    36. textBox1.Text = "";
    37. image = new Mat(image_path);
    38. }
    39. private void button2_Click(object sender, EventArgs e)
    40. {
    41. if (image_path == "")
    42. {
    43. return;
    44. }
    45. // 配置图片数据
    46. Mat image = new Mat(image_path);
    47. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    48. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    49. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    50. image.CopyTo(new Mat(max_image, roi));
    51. float[] result_array = new float[8400 * 56];
    52. float[] factors = new float[2];
    53. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    54. byte[] image_data = max_image.ImEncode(".bmp");
    55. //存储byte的长度
    56. ulong image_size = Convert.ToUInt64(image_data.Length);
    57. // 加载推理图片数据
    58. core.load_input_data("images", image_data, image_size, 1);
    59. dt1 = DateTime.Now;
    60. // 模型推理
    61. core.infer();
    62. dt2 = DateTime.Now;
    63. // 读取推理结果
    64. result_array = core.read_infer_result<float>("output0", 8400 * 56);
    65. result_pro = new PoseResult(factors);
    66. Mat result_image = result_pro.draw_result(result_pro.process_result(result_array), image.Clone());
    67. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    68. textBox1.Text = "耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    69. }
    70. private void Form1_Load(object sender, EventArgs e)
    71. {
    72. startupPath = System.Windows.Forms.Application.StartupPath;
    73. model_path = startupPath + "\\yolov8n-pose.onnx";
    74. core = new Core(model_path, "CPU");
    75. }
    76. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    77. {
    78. core.delet();
    79. }
    80. }
    81. }

    下载 

    源码下载

    说明

    1、运行路径中不能包含中文,否则模型加载不成功,程序无法运行

    2、如果提示“无法加载 DLL“OpenVinoSharpExtern.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”,请使用depends22_x64等工具查找缺失的依赖库并添加。

    也可下载源码自己本地编译,下载地址:https://download.csdn.net/download/lw112190/88806702

    实在自己搞不定的,可是使用Sdcb.OpenVINO,地址:https://lw112190.blog.csdn.net/article/details/136051868

     

  • 相关阅读:
    物体分类__pytorch
    realloc函数应用&IO泄露体验
    c# HttpClient 获取cookie
    如何在三维地球上加载obj、fbx、ifc、dae、3ds、gltf/glb模型?
    Android App启动优化之启动框架
    股票振幅榜查询易语言代码
    shell三剑客之grep
    岛屿的周长
    【技术积累】《MongoDB实战》笔记(1)
    Charles基础使用指南
  • 原文地址:https://blog.csdn.net/lw112190/article/details/132844624