Ultralytics YOLO 是计算机视觉和 ML 领域专业人士的高效工具。
安装 ultralytics 库:
pip install ultralytics
官方YoLo Pose 模型列表信息:
实现代码如下:
from ultralytics import YOLO
import cv2
# Load a model
ckpt_dir = "./ckpt/" # 模型缓存地址
model = YOLO(ckpt_dir + "yolo11l-pose.pt") # load an official model
img_path = "1.jpg" # 图片路径
# Predict with the model
results = model(
source = img_path,
project='./', # 保存预测结果的根目录
name='exp', # 保存预测结果目录名称
exist_ok=True,
save=True,
imgsz=640, # 推理模型输入图像尺寸
conf=0.3 # 置信度阈值
) # predict on an image
img = cv2.imread(img_path) # 加载读取图片,用于模型推理结果可视化
# 遍历结果进行可视化
for result in results:
boxes = result.boxes # 获取检测目标边界框
confidences = result.boxes.conf # 获取检测目标置信度
cls = result.boxes.cls # 获取检测目标标签
print("boxes:{},conf:{},cls:{}".format(boxes.xyxy,confidences,cls))
conf = result.keypoints.conf.detach().cpu().numpy()
print("result.keypoints.conf:",conf.shape)
key_points = result.keypoints.data.detach().cpu().numpy()
print("result.keypoints.data:",key_points.shape)
for i in range(key_points.shape[0]):
for j in range(key_points.shape[1]):
conf_ = key_points[i][j][2]
if conf_>0.3: # 高于置信度阈值显示关键点
x,y = int(key_points[i][j][0]),int(key_points[i][j][1])
cv2.circle(img, (x,y), 4, (100,255,0), 3) #绘制圆
cv2.circle(img, (x,y), 4, (255,60,0), 1) #绘制圆
cv2.namedWindow("img",0)
cv2.imshow("img",img)
cv2.waitKey(0)
log 如下,可以看出keypoints是一个三维数组,第一维度代表人的个数,17代表一个人所具有关键点个数,3代表单个关键点的(x,y,confidence)。
关键点索引信息:
In the default YOLO11 pose model, there are 17 keypoints, each representing a different part of the human body. Here is the mapping of each index to its respective body joint:
0: 鼻子 1: 左眼 2: 右眼 3: 左耳 4: 右耳 5: 左肩 6: 右肩 7: 左肘 8: 右肘 9: 左腕 10: 右腕 11: 左髋 12: 右髋 13: 左膝 14: 右膝 15: 左脚踝 16: 右脚踝
boxes:tensor([[208., 49., 476., 419.]]),conf:tensor([0.9404]),cls:tensor([0.])
result.keypoints.conf: (1, 17)
result.keypoints.data: (1, 17, 3)
模型推理可视化示例图如下:
助力快速掌握数据集的信息和使用方式。