视频检测
哑铃弯举动作计数器实现解析
该Python代码实现了一个基于计算机视觉的哑铃弯举动作计数器,使用OpenCV和自定义姿势检测模块(PoseModule)来实时检测和计数健身动作。
核心功能实现
初始化视频捕获设备,支持摄像头或视频文件输入
cap = cv2.VideoCapture(0) if not video_path else cv2.VideoCapture(video_path)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, display_size[0])
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, display_size[1])
姿势检测器初始化,配置检测参数
self.detector = pm.PoseDetector(
mode=self.mode,
up_body=self.up_body,
smooth=self.smooth,
detection_con=self.min_detection_conf,
track_con=self.min_tracking_conf
)
关键技术点
人体姿势关键点检测使用MediaPipe解决方案,通过21个3D关键点构建人体姿势模型。手臂角度计算公式: [ \theta = \arctan2(y_3 - y_2, x_3 - x_2) - \arctan2(y_1 - y_2, x_1 - x_2) ]
动作计数逻辑基于角度阈值判断:
if angle > self.config["rep_threshold"] and self.direction == 0:
self.count += 0.5
self.direction = 1
elif angle < self.config["rep_threshold"] and self.direction == 1:
self.count += 0.5
self.direction = 0
交互功能实现
键盘控制功能包括:
- 暂停/继续检测(q键)
- 重置计数器(r键)
- 切换左右手检测(a键)
- 调整检测阈值(+/-键)
- 平滑模式切换(s键)
性能优化策略
视频处理采用逐帧分析,使用时间戳计算FPS:
cTime = time.time()
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
显示优化包括关键点绘制和计数信息叠加:
cv2.putText(img, f"次数: {int(self.count)}", (50, 100),
self.config["font"], 3, (255, 0, 0), 3)
cv2.putText(img, f"FPS: {int(fps)}", (50, 150),
self.config["font"], 2, (255, 0, 0), 2)
应用场景扩展
该系统可扩展应用于多种健身动作计数:
- 深蹲动作检测
- 俯卧撑计数
- 引体向上监测
- 瑜伽姿势纠正
通过调整关键点组合和角度阈值,可以适配不同健身动作的计数需求。

被折叠的 条评论
为什么被折叠?



