使用YOLO进行俯卧撑监测与计数
在这篇博客中,我们将探讨如何使用YOLO(You Only Look Once)模型进行俯卧撑的监测与计数。YOLO是一种实时目标检测系统,能够快速识别视频中的物体。在这里,我们将利用YOLO的姿态估计功能来实现对俯卧撑的自动计数。
代码概述
关键点说明
俯卧撑监测与计数的Python代码片段:
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT,
cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("workouts.avi",
cv2.VideoWriter_fourcc(*"mp4v"),
fps, (w, h))
# Init AIGym
gym = solutions.AIGym(
show=True, # Display the frame
kpts=[6, 8, 10], # keypoints index for monitoring specific exercise
model="yolo11n-pose.pt", # Path to the YOLO11 pose estimation model
)
# Process video
while cap.isOpened():
success, im0 = cap.read()
if not success:
break
im0 = gym.monitor(im0)
video_writer.write(im0)
cv2.destroyAllWindows()
video_writer.release()
计数要点
1.关键点检测:代码中使用了YOLO的姿态估计模型yolo11n-pose.pt,通过检测人体的关键点(如肩膀、肘部、手腕等)来判断俯卧撑的动作是否完成。
2.动作识别:通过设定关键点的索引(如kpts=[6, 8, 10]),可以监测特定的动作。在俯卧撑中,通常需要监测肩膀、肘部和手腕的相对位置变化。
3.实时监测:代码中使用了cv2.VideoCapture来读取视频文件,并通过AIGym类的monitor方法实时处理每一帧视频。
深蹲检测代码
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,
cv2.CAP_PROP_FRAME_HEIGHT,
cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("workouts.avi",
cv2.VideoWriter_fourcc(*"mp4v"),
fps, (w, h))
# Init AIGym
gym = solutions.AIGym(
show=True, # Display the frame
kpts=[5, 11, 13], # keypoints index for monitoring specific exercise
model="yolo11n-pose.pt", # Path to the YOLO11 pose estimation model
# line_width=2, # Adjust the line width for bounding boxes
)
# Process video
while cap.isOpened():
success, im0 = cap.read()
if not success:
break
im0 = gym.monitor(im0)
video_writer.write(im0)
cv2.destroyAllWindows()
video_writer.release()
应用场景
- 健身房监控:可以在健身房中安装摄像头,实时监测会员的运动情况,帮助他们纠正动作并记录运动数据。
2.家庭健身:对于在家锻炼的人来说,这个系统可以作为一个虚拟教练,帮助他们保持正确的姿势并记录锻炼次数。
3.运动分析:教练可以使用该系统分析运动员的动作,提供更专业的指导和反馈。
总结
通过使用YOLO进行俯卧撑监测与计数,我们可以实现对运动的自动化监控和分析。这不仅提高了运动的安全性和有效性,还为运动爱好者提供了便利的工具。未来,我们可以进一步优化模型和算法,以支持更多种类的运动监测。