feat(yolo): 增强图像分类与姿态估计功能

- 图像分类中增加显示top1和top5的标签名及置信度
- 实例分割中引入OpenCV进行掩码处理并可视化抠图效果
- 姿态估计中添加关键点标注说明注释
This commit is contained in:
mshe 2025-09-26 22:45:20 +08:00
parent b388083559
commit e2cbb52dd1
3 changed files with 32 additions and 6 deletions

View File

@ -2,7 +2,16 @@ from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolov8n-cls.pt')
results = model('./resources/图像分类.png',save=True)
for result in results:
probs = result.probs
print(probs)
model_names = model.names
results = model('./resources/图像分类.png', save=True)
for index in range(len(results)):
item = results[index]
probs = item.probs
top_1 = probs.top1
top_1_conf = probs.top1conf
top_5 = probs.top5
top_5_conf = probs.top5conf.numpy()
top_5_names = [model_names[i] for i in top_5]
print('第[{}]张图片分类结果:'.format(index))
print('top1:[{}],可置信度:[{}],名称:[{}]'.format(top_1, top_1_conf, model.names[probs.top1]))
print('top5:[{}],可置信度:[{}],名称:[{}]'.format(top_5, top_5_conf, top_5_names))

View File

@ -1,8 +1,23 @@
import cv2
from ultralytics import YOLO
import numpy as np
# 加载预训练模型
model = YOLO('models/yolov8n-seg.pt')
results = model('./resources/图像分类.png',save= True)
print(model.names)
img_path = "./resources/图像分类.png"
results = model(img_path, save=True)
image = cv2.imread(img_path)
# 创建掩码,如需单独分割,则在循环中创建多个mask
mask = np.zeros(image.shape[:2], dtype=np.uint8)
for result in results:
masks = result.masks
print(masks)
points_array = masks.xy
for points in points_array:
points = points.astype(int)
cv2.fillPoly(mask, [points], 255)
green_bg = np.full_like(image, (0, 255, 0))
result = np.where(mask[:, :, np.newaxis] == 255, image, green_bg)
cv2.imshow('抠图', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

View File

@ -3,6 +3,8 @@ from ultralytics import YOLO
# 加载预训练模型
model = YOLO('models/yolo11n-pose.pt')
results = model('./resources/姿态估计',save= True)
# 1.鼻子 2.左眼 3.右眼 4.左耳 5.右耳 6.左肩 7.右肩 8.左手肘 9.右肘 10.左手腕
# 11.右腕 12.左髋 13.右髋 14.左膝 15.右膝 16. 左脚踝 17.右脚踝
for result in results:
keypoints = result.keypoints
print(keypoints)