基于openpose的引体向上的识别计数统计项目(3)CPoseRender类设计与实现

本文主要是介绍基于openpose的引体向上的识别计数统计项目(3)CPoseRender类设计与实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CPoseRender 主要是为了简化openpose中的调用方式进行简化重写,只需要opencv的参数即可使用。

1、CPoseRender 声明

#pragma once#include "opencv2/core.hpp"#include "CPoseClassify.h"#include "Keypoints.h"namespace Utils {
// fastmath, taken from op
//
// Use op::round/max/min for basic types (int, char, long, float, double, etc). Never with classes!
// `std::` alternatives uses 'const T&' instead of 'const T' as argument.
// E.g., std::round is really slow (~300 ms vs ~10 ms when I individually apply it to each element of a whole
// image array// VERY IMPORTANT: These fast functions does NOT work for negative integer numbers.
// E.g., positiveIntRound(-180.f) = -179.// Round functions
// Signed
template<typename T>
inline char positiveCharRound(const T a)
{return char(a + 0.5f);
}template<typename T>
inline signed char positiveSCharRound(const T a)
{return (signed char)(a + 0.5f);
}template<typename T>
inline int positiveIntRound(const T a)
{return int(a + 0.5f);
}template<typename T>
inline long positiveLongRound(const T a)
{return long(a + 0.5f);
}template<typename T>
inline long long positiveLongLongRound(const T a)
{return (long long)(a + 0.5f);
}// Unsigned
template<typename T>
inline unsigned char uCharRound(const T a)
{return (unsigned char)(a + 0.5f);
}template<typename T>
inline unsigned int uIntRound(const T a)
{return (unsigned int)(a + 0.5f);
}template<typename T>
inline unsigned long ulongRound(const T a)
{return (unsigned long)(a + 0.5f);
}template<typename T>
inline unsigned long long uLongLongRound(const T a)
{return (unsigned long long)(a + 0.5f);
}// Max/min functions
template<typename T>
inline T fastMax(const T a, const T b)
{return (a > b ? a : b);
}template<typename T>
inline T fastMin(const T a, const T b)
{return (a < b ? a : b);
}template<class T>
inline T fastTruncate(T value, T min = 0, T max = 1)
{return fastMin(max, fastMax(min, value));
}
//
} // namespace Utilsclass CPoseRender
{
public:CPoseRender(PoseModel poseModel);~CPoseRender();void rendPose(cv::Mat& frame, const cv::Mat& poseData, float renderThreshold = 0.05);private:const PoseModel poseModel;
};

2、CPoseRender 实现

#include "CPoseRender.h"#include <vector>
#include <array>//#include "openpose/utilities/fastMath.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"namespace {#define POSE_BODY_25_PAIRS_RENDER \1,8,   1,2,   1,5,   2,3,   3,4,   5,6,   6,7,   8,9,   9,10,  10,11, 8,12,  12,13, 13,14,  1,0,   0,15, 15,17,  0,16, 16,18,   14,19,19,20,14,21, 11,22,22,23,11,24
#define POSE_BODY_25_SCALES_RENDER 1
#define POSE_BODY_25_COLORS_RENDER \255.f,     0.f,    85.f, \255.f,     0.f,     0.f, \255.f,    85.f,     0.f, \255.f,   170.f,     0.f, \255.f,   255.f,     0.f, \170.f,   255.f,     0.f, \85.f,   255.f,     0.f, \0.f,   255.f,     0.f, \255.f,     0.f,     0.f, \0.f,   255.f,    85.f, \0.f,   255.f,   170.f, \0.f,   255.f,   255.f, \0.f,   170.f,   255.f, \0.f,    85.f,   255.f, \0.f,     0.f,   255.f, \255.f,     0.f,   170.f, \170.f,     0.f,   255.f, \255.f,     0.f,   255.f, \85.f,     0.f,   255.f, \0.f,     0.f,   255.f, \0.f,     0.f,   255.f, \0.f,     0.f,   255.f, \0.f,   255.f,   255.f, \0.f,   255.f,   255.f, \0.f,   255.f,   255.fconst std::array<std::vector<unsigned int>, (int)PoseModel::Size> POSE_BODY_PART_PAIRS_RENDER{std::vector<unsigned int>{POSE_BODY_25_PAIRS_RENDER},       // BODY_25
};const std::array<std::vector<unsigned int>, (int)PoseModel::Size> POSE_BODY_PART_PAIRS{// BODY_25std::vector<unsigned int>{1,8, 1,2, 1,5, 2,3, 3,4, 5,6, 6,7, 8,9, 9,10, 10,11, 8,12, 12,13, 13,14, 1,0,  0,15, 15,17, 0,16, 16,18,  2,17, 5,18,  14,19,19,20,14,21, 11,22,22,23,11,24}
};const std::array<std::vector<float>, (int)PoseModel::Size> POSE_SCALES{std::vector<float>{POSE_BODY_25_SCALES_RENDER},       // BODY_25
};const std::array<std::vector<float>, (int)PoseModel::Size> POSE_COLORS{std::vector<float>{POSE_BODY_25_COLORS_RENDER},       // BODY_25
};void renderOpenPoseKeypoints(cv::Mat & frame, const cv::Mat & poseData, PoseModel poseModel, const float threshold)
{const auto thicknessCircleRatio = 1.f / 75.f;const auto thicknessLineRatioWRTCircle = 0.75f;const auto& pairs = POSE_BODY_PART_PAIRS_RENDER.at((int)poseModel);const auto& poseScales = POSE_SCALES.at((int)poseModel);const auto& colors = POSE_COLORS.at((int)(int)poseModel);//-------------------------------------------------------------------------const auto width = frame.size[1];const auto height = frame.size[0];const auto area = width * height;// Parametersconst auto lineType = 8;const auto shift = 0;const auto numberColors = colors.size();const auto numberScales = poseScales.size();const auto thresholdRectangle = float(0.1);const auto numberKeypoints = poseData.cols; //keypoints.getSize(1);// Keypointsfor(auto person = 0; person < poseData.rows; person++) {const auto personRectangle = Utils::getKeypointsRectangle(poseData, person, thresholdRectangle);if(personRectangle.area() > 0) {const auto ratioAreas = Utils::fastMin(float(1), Utils::fastMax(personRectangle.width / (float)width, personRectangle.height / (float)height));// Size-dependent variablesconst auto thicknessRatio = Utils::fastMax(Utils::positiveIntRound(std::sqrt(area)* thicknessCircleRatio * ratioAreas), 2);// Negative thickness in cv::circle means that a filled circle is to be drawn.const auto thicknessCircle = Utils::fastMax(1, (ratioAreas > float(0.05) ? thicknessRatio : -1));const auto thicknessLine = Utils::fastMax(1, Utils::positiveIntRound(thicknessRatio * thicknessLineRatioWRTCircle));const auto radius = thicknessRatio / 2;// Draw linesfor(auto pair = 0u; pair < pairs.size(); pair += 2) {const auto index1 = (person * numberKeypoints + pairs[pair]) * poseData.channels();const auto index2 = (person * numberKeypoints + pairs[pair + 1]) * poseData.channels();if(poseData.at<float>(index1 + 2) > threshold && poseData.at<float>(index2 + 2) > threshold) {const auto thicknessLineScaled = Utils::positiveIntRound(thicknessLine * poseScales[pairs[pair + 1] % numberScales]);const auto colorIndex = pairs[pair + 1] * 3; // Before: colorIndex = pair/2*3;const cv::Scalar color{colors[(colorIndex + 2) % numberColors],colors[(colorIndex + 1) % numberColors],colors[colorIndex % numberColors]};const cv::Point keypoint1{Utils::positiveIntRound(poseData.at<float>(index1)), Utils::positiveIntRound(poseData.at<float>(index1+1))};const cv::Point keypoint2{Utils::positiveIntRound(poseData.at<float>(index2)), Utils::positiveIntRound(poseData.at<float>(index2 + 1))};cv::line(frame, keypoint1, keypoint2, color, thicknessLineScaled, lineType, shift);}}// Draw circlesfor(auto part = 0; part < numberKeypoints; part++) {const auto faceIndex = (person * numberKeypoints + part) * frame.channels();if(poseData.at<float>(faceIndex + 2) > threshold) {const auto radiusScaled = Utils::positiveIntRound(radius * poseScales[part % numberScales]);const auto thicknessCircleScaled = Utils::positiveIntRound(thicknessCircle * poseScales[part % numberScales]);const auto colorIndex = part * 3;const cv::Scalar color{colors[(colorIndex + 2) % numberColors],colors[(colorIndex + 1) % numberColors],colors[colorIndex % numberColors]};const cv::Point center{Utils::positiveIntRound(poseData.at<float>(faceIndex)),Utils::positiveIntRound(poseData.at<float>(faceIndex + 1))};cv::circle(frame, center, radiusScaled, color, thicknessCircleScaled, lineType, shift);}}}}
}} // namespaceCPoseRender::CPoseRender(PoseModel poseModel):poseModel(poseModel)
{
}CPoseRender::~CPoseRender()
{
}void CPoseRender::rendPose(cv::Mat & frame, const cv::Mat & poseData, float renderThreshold)
{if(frame.empty() || poseData.empty())return;renderOpenPoseKeypoints(frame, poseData, poseModel, renderThreshold);
}

这篇关于基于openpose的引体向上的识别计数统计项目(3)CPoseRender类设计与实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/788470

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too