Python YOLO 坐姿检测

本项目使用 Python、YOLO Pose 和 OpenCV 实现一个简单的 AI 坐姿检测工具。通过电脑摄像头实时识别人体姿态,提取耳朵、肩膀、髋部等关键点坐标,并根据身体倾斜角度和头部位置判断当前坐姿是否正常。当检测到明显的身体前倾或弯腰时,程序会显示异常提示。项目从 YOLO 人体骨架识别入手,逐步实现关键点提取和坐姿判断,帮助初学者了解人体姿态识别的基本原理。
1
下载文件&依赖




12
- 输入命令 pip install ultralytics opencv-python numpy 下载依赖 如图1
- 然后进入 https://github.com/ultralytics/assets/releases 这个网址 下载yolo11n-pose.pt 下载好的在附件
- 然后我们把下载好的放到项目目录下
yolo11n-pose.pt
5.97MB
2
显示人体骨架

我们通过yolo 预训练好的模型 让他 通过电脑摄像头识别人体,并实时显示人体骨架。
代码如下 效果如图
import cv2
from ultralytics import YOLO
# 加载 YOLO 姿态识别模型
model = YOLO("yolo11n-pose.pt")
# 打开电脑摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取摄像头画面
ret, frame = cap.read()
if not ret:
break
# YOLO 人体姿态识别
results = model(frame)
# 绘制人体关键点和骨架
frame = results[0].plot()
# 显示画面
cv2.imshow("YOLO Posture Detection", frame)
# 按 Q 键退出
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# 释放摄像头
cap.release()
cv2.destroyAllWindows()
3
关键点




12
这一步是从 YOLO Pose 的识别结果中,获取人体各个部位的坐标,为后续坐姿判断提供数据。
YOLO Pose 可以识别人体的 17 个关键点,例如耳朵、肩膀、手肘、髋部和膝盖等。通过关键点编号,可以提取指定部位的 X、Y 坐标。 关键点函数如图1所示
代码如下
import cv2
from ultralytics import YOLO
# 加载 YOLO 姿态识别模型
model = YOLO("yolo11n-pose.pt")
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 人体姿态识别
results = model(frame)
# 获取检测结果
result = results[0]
# 绘制人体骨架
frame = result.plot()
# 获取人体关键点
if result.keypoints is not None:
for person in result.keypoints.data:
# 提取耳朵、肩膀、髋部坐标
left_ear = person[3][:2]
right_ear = person[4][:2]
left_shoulder = person[5][:2]
right_shoulder = person[6][:2]
left_hip = person[11][:2]
right_hip = person[12][:2]
# 打印关键点坐标
print("左肩:", left_shoulder.tolist())
print("右肩:", right_shoulder.tolist())
print("左髋:", left_hip.tolist())
print("右髋:", right_hip.tolist())
# 显示画面
cv2.imshow("YOLO Posture Detection", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
4
识别





123
我们先采用侧面摄像头检测,通过三个关键部位进行判断:耳朵:判断头部是否明显前倾。肩膀:判断上半身是否前倾。髋部:作为身体姿态的参考位置。
基本思路是计算耳朵、肩膀和髋部之间的位置关系,根据设定的角度阈值判断坐姿是否异常。
我们首先创建一个 posture.py 这个类是用于判断坐姿情况的 代码如下
import math
from sympy import false
def check_posture(person):
# 获取耳朵、肩膀、髋部坐标
ear = person[3][:2]
shoulder = person[5][:2]
hip = person[11][:2]
# 计算肩膀与髋部之间的坐标差
dx = shoulder[0] - hip[0]
dy = shoulder[1] - hip[1]
# 计算身体倾斜角度
angle = math.degrees(math.atan2(abs(dx), abs(dy)))
# 计算耳朵与肩膀之间的水平距离
head_offset = abs(ear[0] - shoulder[0])
# 计算头部前倾比例
body_length = math.sqrt(dx ** 2 + dy ** 2)
head_ratio = head_offset / body_length
# 判断坐姿
if angle > 15 or head_ratio > 0.25:
return False
return True
然后把main.py 进行调用他再进行判断 代码如下
import cv2
from ultralytics import YOLO
from posture import check_posture
# 加载 YOLO 姿态识别模型
model = YOLO("yolo11n-pose.pt")
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# 人体姿态识别
results = model(frame)
# 获取检测结果
result = results[0]
# 绘制人体骨架
frame = result.plot()
# 遍历检测到的每个人
if result.keypoints is not None:
for person in result.keypoints.data:
# 判断坐姿
posture = check_posture(person)
# 设置显示文字和颜色
if posture:
text = "Good Posture"
color = (0, 255, 0)
else:
text = "Bad Posture"
color = (0, 0, 255)
# 显示判断结果
cv2.putText(
frame,
text,
(30, 50),
cv2.FONT_HERSHEY_SIMPLEX,
1,
color,
2
)
# 显示画面
cv2.imshow("YOLO Posture Detection", frame)
# 按 Q 键退出
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# 释放摄像头
cap.release()
cv2.destroyAllWindows()
效果看视频
0
0
0
qq空间
微博
复制链接
分享 更多相关项目
猜你喜欢
评论/提问(已发布 0 条)
0