基于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

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解