import cv2
import numpy as np
from ultralytics import YOLO
# 加载预训练的 YOLOv8-Pose 模型
model = YOLO('yolov8n-pose.pt')
def detect_fall(frame):
# 进行姿态估计
results = model(frame)
# 遍历检测到的人体
for result in results:
if result.keypoints is not None and len(result.keypoints) > 0:
keypoints = result.keypoints.data.cpu().numpy()
for kpts in keypoints:
# 检查关键点数组是否为空
if len(kpts) > 0:
# 提取关键点
nose = kpts[0]
neck = kpts[1]
r_shoulder = kpts[2]
l_shoulder = kpts[5]
r_hip = kpts[8]
l_hip = kpts[11]
# 计算身体的倾斜角度
if neck[2] > 0.5 and r_hip[2] > 0.5 and