Ultralytics YOLO 是计算机视觉和 ML 领域专业人士的高效工具。
安装 ultralytics 库:
pip install ultralytics
实现代码如下:
import cv2
from ultralytics import YOLO
# 加载预训练的 YOLOv8n 模型
ckpt_dir = "./ckpt/" # 模型缓存地址
model = YOLO(ckpt_dir + 'yolov8n.pt')
# 定义图像文件的路径
source = 'img.jpg'
# 运行推理,并附加参数
results = model.predict(source,
project='./', # 保存预测结果的根目录
name='exp', # 保存预测结果目录名称
exist_ok=True,
save=True,
imgsz=640, # 推理模型输入图像尺寸
conf=0.2 # 置信度阈值
)
print("results.names :\n",results) # 输出推理结果
print("model names:",model.names) # 输出模型类别
# 遍历各检测目标结果
for result in results:
boxes = result.boxes # 获取检测目标边界框
confidences = result.boxes.conf # 获取检测目标置信度
cls = result.boxes.cls # 获取检测目标标签
print("boxes:{},conf:{},cls:{}".format(boxes.xyxy,confidences,cls))
# 读取模型推理可视化图片显示
img_result = cv2.imread("exp/" + source)
cv2.namedWindow("result",0)
cv2.imshow("result",img_result)
cv2.waitKey(0)
模型类别输出信息:
model names: {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie',
28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard',
38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange',
50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet',
62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book',
74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
模型推理输出信息:
boxes:tensor([[219.5013, 171.1023, 340.7977, 316.2900],
[313.7640, 95.0483, 522.8762, 362.2893],
[230.2642, 84.0345, 329.5569, 264.4615]], device='cuda:0'),
conf:tensor([0.9257, 0.9062, 0.8834], device='cuda:0'),
cls:tensor([1., 0., 0.], device='cuda:0')
模型推理可视化显示:
助力快速掌握数据集的信息和使用方式。