目录
效果
模型信息
Model Properties
-------------------------
date:2024-12-04T17:34:08.564548
description:Ultralytics YOLO11n-pose model trained on data.yaml
author:Ultralytics
version:8.3.5
kpt_shape:[21, 3]
task:pose
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[640, 640]
names:{0: 'hand'}
---------------------------------------------------------------
Inputs
-------------------------
name:images
tensor:Float[1, 3, 640, 640]
---------------------------------------------------------------
Outputs
-------------------------
name:output0
tensor:Float[1, 68, 8400]
---------------------------------------------------------------
项目
代码
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace OpenCvSharp_Yolov8_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string startupPath;
string classer_path;
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string model_path;
Mat image;
PoseResult result_pro;
Mat result_mat;
Mat result_image;
Mat result_mat_to_float;
Net opencv_net;
Mat BN_image;
float[] result_array;
float[] factors;
int max_image_length;
Mat max_image;
Rect roi;
Result result;
StringBuilder sb = new StringBuilder();
private void Form1_Load(object sender, EventArgs e)
{
startupPath = System.Windows.Forms.Application.StartupPath;
model_path = startupPath + "\\best.onnx";
classer_path = startupPath + "\\lable.txt";
//初始化网络类,读取本地模型
opencv_net = CvDnn.ReadNetFromOnnx(model_path);
result_array = new float[8400 * 68];
factors = new float[2];
result_pro = new PoseResult(factors);
//test.jpg
image_path = "test.jpg";
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
image = new Mat(image_path);
pictureBox2.Image = null;
}
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 = "";
image = new Mat(image_path);
pictureBox2.Image = null;
}
private void button2_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
//缩放图片
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, 68, MatType.CV_32F, result_mat.Data);
//将数据读取到数组中
result_mat_to_float.GetArray
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 = "无信息";
}
}
}
}
- using OpenCvSharp;
- using OpenCvSharp.Dnn;
- using System;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace OpenCvSharp_Yolov8_Demo
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
- string image_path = "";
- string startupPath;
- string classer_path;
-
- DateTime dt1 = DateTime.Now;
- DateTime dt2 = DateTime.Now;
- string model_path;
- Mat image;
-
- PoseResult result_pro;
- Mat result_mat;
- Mat result_image;
- Mat result_mat_to_float;
-
- Net opencv_net;
- Mat BN_image;
-
- float[] result_array;
- float[] factors;
-
- int max_image_length;
- Mat max_image;
- Rect roi;
-
- Result result;
- StringBuilder sb = new StringBuilder();
-
- private void Form1_Load(object sender, EventArgs e)
- {
- startupPath = System.Windows.Forms.Application.StartupPath;
- model_path = startupPath + "\\best.onnx";
- classer_path = startupPath + "\\lable.txt";
-
- //初始化网络类,读取本地模型
- opencv_net = CvDnn.ReadNetFromOnnx(model_path);
-
- result_array = new float[8400 * 68];
- factors = new float[2];
-
- result_pro = new PoseResult(factors);
-
- //test.jpg
- image_path = "test.jpg";
- pictureBox1.Image = new Bitmap(image_path);
- textBox1.Text = "";
- image = new Mat(image_path);
- pictureBox2.Image = null;
- }
-
- 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 = "";
- image = new Mat(image_path);
- pictureBox2.Image = null;
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- if (image_path == "")
- {
- return;
- }
-
- //缩放图片
- 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, 68, MatType.CV_32F, result_mat.Data);
-
- //将数据读取到数组中
- result_mat_to_float.GetArray<float>(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 = "无信息";
- }
-
- }
- }
- }
下载
参考
GitHub - chrismuntean/YOLO11n-pose-hands: Trained YOLO11n-pose model on hand keypoints


评论记录:
回复评论: