【算法介绍】
在C# WinForms应用中部署YOLOv11-Pose姿态估计ONNX模型是一项具有挑战性的任务。YOLOv11-Pose结合了YOLO(You Only Look Once)的高效物体检测算法和Pose Estimation(姿态估计)专注于识别人体关键点的能力,能在多种计算平台上实时处理人体姿态数据。
由于YOLOv11通常是用PyTorch等深度学习框架实现的,而OpenCV本身并不直接支持加载和运行PyTorch模型,因此需要先将PyTorch模型转换为ONNX格式,然后使用OpenCV的DNN模块加载ONNX模型。
部署过程中,需要确保开发环境已经安装了OpenCV 4.x(带有DNN模块)和必要的C#编译器。具体步骤包括加载ONNX模型、预处理输入图像、将预处理后的图像输入到模型中获取检测结果、对检测结果进行后处理等。由于YOLOv11是一个复杂的模型,其输出可能包含多个层的信息,因此需要仔细解析模型输出,并根据YOLOv11的具体实现进行后处理。
用户可以通过WinForms界面上传图像,应用程序则利用YOLOv11-Pose模型进行姿态估计,并在图像上标注出人体关键点的位置和类别。总的来说,虽然使用纯OpenCV部署YOLOv11-Pose ONNX模型需要深入理解相关领域的知识,但通过合理的步骤和优化,可以在C# WinForms应用中实现高效的人体姿态估计功能。
【效果展示】
【实现部分代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
namespace FIRC
{
public partial class Form1 : Form
{
Mat src = new Mat();
Yolov11PoseManager ypm = new Yolov11PoseManager();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
src = Cv2.ImRead(openFileDialog.FileName);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
}
}
private void button2_Click(object sender, EventArgs e)
{
if(pictureBox1.Image==null)
{
return;
}
Stopwatch sw = new Stopwatch();
sw.Start();
var result = ypm.Inference(src);
sw.Stop();
this.Text = "耗时" + sw.Elapsed.TotalSeconds + "秒";
var resultMat = ypm.DrawImage(src,result);
pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
}
private void Form1_Load(object sender, EventArgs e)
{
ypm.LoadWeights(Application.StartupPath+ "\\weights\\yolo11n-pose.onnx");
}
private void btn_video_Click(object sender, EventArgs e)
{
var detector = new Yolov11PoseManager();
detector.LoadWeights(Application.StartupPath + "\\weights\\yolo11-pose.onnx");
VideoCapture capture = new VideoCapture(Application.StartupPath+ "\\images\\test.mp4");
if (!capture.IsOpened())
{
Console.WriteLine("video not open!");
return;
}
Mat frame = new Mat();
var sw = new Stopwatch();
int fps = 0;
while (true)
{
capture.Read(frame);
if (frame.Empty())
{
Console.WriteLine("data is empty!");
break;
}
sw.Start();
var result = detector.Inference(frame);
var resultImg = detector.DrawImage(frame,result);
sw.Stop();
fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
sw.Reset();
Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
//显示结果
Cv2.ImShow("Result", resultImg);
int key = Cv2.WaitKey(10);
if (key == 27)
break;
}
capture.Release();
}
}
}
【视频演示】
【源码下载】
https://download.csdn.net/download/FL1623863129/89852169
【测试环境】
vs2019
netframework4.7.2
opencvsharp4.8.0
onnxruntime1.16.3