import cv2
from ultralytics import YOLO
# 加载模型
model = YOLO(model=r"D:\desktop\fall detection\ultralytics-main\mymodel\yolov8n-pose.pt")
# 视频文件路径
video_path = r"D:\desktop\杂\code\test\1.mp4"
# 打开视频文件
cap = cv2.VideoCapture(video_path)
# 获取视频帧的宽度和高度
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# 创建显示窗口
cv2.namedWindow("YOLOV8", cv2.WINDOW_NORMAL)
while cap.isOpened():
# 获取帧
res, frame = cap.read()
# 如果读取成功
if res:
# 正向推理
results = model(frame)
for result in results:
keypoints = result.keypoints
for i, keypoint in enumerate(keypoints):
num_keypoints = keypoint.xyn.numel()/2 #x y 坐标分开了
print(f"当前帧第 {i + 1} 组关键点,共有 {num_keypoints} 个关键点坐标:", keypoint.xyn)
# 绘制结果
annotated_frame = results[0].plot()
# 调整帧的大小以适应窗口 根据窗口的大小自动缩放
annotated_frame = cv2.resize(annotated_frame, (frame_width, frame_height))
# 显示图像
cv2.imshow("YOLOV8", annotated_frame)
# 按ESC退出
if cv2.waitKey(1) == 27:
break
else:
break
# 释放视频文件
cap.release()
cv2.destroyAllWindows()
主要收获:
存坐标的张量
是Results类的
具体参数如下
class Results(SimpleClass)
A class for storing and manipulating inference results.
Attributes:
orig_img – The original image as a numpy array.
orig_shape – The original image shape in (height, width) format.
boxes – A Boxes object containing the detection bounding boxes.
masks – A Masks object containing the detection masks.
probs – A Probs object containing probabilities of each class for classification task.
keypoints – A Keypoints object containing detected keypoints for each object.
speed – A dictionary of preprocess, inference, and postprocess speeds in milliseconds per image.
names – A dictionary of class names.
path – The path to the image file.
_keys – A tuple of attribute names for non-empty attributes.
Params:
orig_img – The original image as a numpy array.
path – The path to the image file.
names – A dictionary of class names.
boxes – A 2D tensor of bounding box coordinates for each detection.
masks – A 3D tensor of detection masks, where each mask is a binary image.
probs – A 1D tensor of probabilities of each class for classification task.
keypoints – A list of detected keypoints for each object.
# 正向推理
results = model(frame)
for result in results:
keypoints = result.keypoints
for i, keypoint in enumerate(keypoints):
num_keypoints = keypoint.xyn.numel()/2 #x y 坐标分开了
print(f"当前帧第 {i + 1} 组关键点,共有 {num_keypoints} 个关键点坐标:", keypoint.xyn)
说明 当前帧可能有多个人,所有需要for循环依次处理每一个人 用.numel获得张量的大小 keypoint.xyn.numel()
这里我想用来获取每一组坐标的坐标个数 所以是 keypoint.xyn.numel()/2