C# OpenCvSharp Yolov8 Pose 姿态识别

news/2024/12/2 4:28:48/

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
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 + "\\yolov8n-pose.onnx";classer_path = startupPath + "\\yolov8-detect-lable.txt";//初始化网络类,读取本地模型opencv_net = CvDnn.ReadNetFromOnnx(model_path);result_array = new float[8400 * 56];factors = new float[2];result_pro = new PoseResult(factors);}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, 56, 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 = "无信息";}}}
}

Demo下载


http://www.ppmy.cn/news/1140171.html

相关文章

FastAPI学习-23.异常处理器 exception_handler

前言 通常我们可以通过 raise 抛出一个 HTTPException 异常&#xff0c;请求参数不合法会抛出RequestValidationError 异常&#xff0c;这是最常见的2种异常。 HTTPException 异常 向客户端返回 HTTP 错误响应&#xff0c;可以使用 raise 触发 HTTPException。 from fastap…

LCR 133.位 1 的个数

​​题目来源&#xff1a; leetcode题目&#xff0c;网址&#xff1a;LCR 133. 位 1 的个数 - 力扣&#xff08;LeetCode&#xff09; 解题思路&#xff1a; 重复利用 n&(n-1) 将 n 最右边一个 1 消除直至 n 为 0&#xff0c;返回循环次数即可。 解题代码&#xff1a; p…

论文阅读-- A simple transmit diversity technique for wireless communications

一种简单的无线通信发射分集技术 论文信息&#xff1a; Alamouti S M. A simple transmit diversity technique for wireless communications[J]. IEEE Journal on selected areas in communications, 1998, 16(8): 1451-1458. 创新性&#xff1a; 提出了一种新的发射分集方…

ESP32设备驱动-TFT_eSPI显示中文

TFT_eSPI显示中文 文章目录 TFT_eSPI显示中文1、安装TFT_eSPI库2、创建字库3、生成字库头文件4、使用字库本文将详细介绍如何使用TFT_eSPI显示中文。 1、安装TFT_eSPI库 2、创建字库 TFT_eSPI字体工具使用Processing软件创建字体。 下载并安装Processing:https://processin…

C++ 提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数

#include <iostream>using namespace std;int main() {string str;cout << "请输入一个字符串>>>" << endl;getline(cin, str);int size str.size();int xiaoxie 0;int daxie 0;int shuzi 0;int kongge 0;int qita 0;for(int i0;i < …

机器视觉工程师,公司设置奖金,真的为了奖励你吗?其实和你没关系

​据说某家大厂&#xff0c;超额罚款&#xff0c;有奖有罚很正常&#xff0c;但是我觉得你罚款代理商员工就不一样了&#xff0c;把代理商当成你的员工&#xff0c;我就觉得这些大厂的脑回路有问题。 有人从来没听说过项目奖金&#xff0c;更没有奖金。那么为什么设置奖金呢&a…

nginx的配置文件概述及简单demo(二)

默认配置文件 当安装完nginx后&#xff0c;它的目录下通常有默认的配置文件 #user nobody; worker_processes 1;#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connection…

Mybatis整理

Mybatis 定义 Mybatis是一个半ORM&#xff08;对象关系映射&#xff09;框架&#xff0c;它内部封装了JDBC&#xff0c;加载驱动、创建连接、创建statement等繁杂的过程&#xff0c;开发者开发时只需要关注如何编写SQL语句&#xff0c;可以严格控制sql执行性能&#xff0c;灵…