C# OpenVino Yolov8 Pose 姿态识别

目录

效果

模型信息

项目

代码

下载 


效果

C# OpenVino Yolov8 Pose 姿态识别_第1张图片

模型信息

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

项目

C# OpenVino Yolov8 Pose 姿态识别_第2张图片

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenVino_Yolov8_Pose
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string model_path;
        string classer_path;

        Mat src;

        StringBuilder sb = new StringBuilder();
        PoseResult result_pro;
        Result result;

        CompiledModel cm;
        InferRequest ir;
        Shape inputShape;

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model\\yolov8n-pose.onnx";
            classer_path = "model\\lable.txt";

            Model rawModel = OVCore.Shared.ReadModel(model_path);

            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }

            cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            ir = cm.CreateInferRequest();
            inputShape = cm.Inputs.Primary.Shape;

            image_path = "test_img\\demo_2.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;

            result_pro = new PoseResult();
        }

        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 = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;
        }



        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();
            button2.Enabled = false;
            Application.DoEvents();

            Stopwatch stopwatch = new Stopwatch();

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

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

            float[] factors = new float[2];
            factors[0] = factors[1] = (float)(max_image_length / 640.0);
            result_pro.scales = factors;

            resize_image.ConvertTo(resize_image, MatType.CV_32FC3, 1.0 / 255);

            float[] input_tensor_data = Common.ExtractMat(resize_image);

            resize_image.Dispose();
            image_rgb.Dispose();

            using (Tensor input_x = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 640, 640)))
            {
                ir.Inputs[0] = input_x;
            }

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();
            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            using (Tensor output = ir.Outputs[0])
            {
                float[] result_array = output.GetData().ToArray();

                result = result_pro.process_result(result_array);

                double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
                stopwatch.Stop();
                double totalTime = preprocessTime + inferTime + postprocessTime;

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

                sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
                sb.AppendLine($"Infer: {inferTime:F2}ms");
                sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
                sb.AppendLine($"Total: {totalTime:F2}ms");
                sb.AppendLine("---------------------------------------");

                for (int i = 0; i < result.length; i++)
                {
                    sb.AppendLine("person " + result.scores[i].ToString("P2"));
                }

                if (pictureBox2.Image != null)
                {
                    pictureBox2.Image.Dispose();
                }

                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                textBox1.Text = sb.ToString();
            }

            image.Dispose();
            button2.Enabled = true;
        }

    }
}

下载 

可执行程序exe下载

源码下载

想尝试自行编译OpenVinoSharp的,博客地址:https://lw112190.blog.csdn.net/article/details/132844624

你可能感兴趣的:(人工智能,c#,openvino,YOLO)