YOLOv8 笔记

How to Detect Objects in Images Using the YOLOv8 Neural Network

cat_dog.png

from ultralytics import YOLO
from PIL import Image

# 加载模型文件
model = YOLO("yolov8m.pt")

# 针对目标图像进行推理
results = model.predict("cat_dog.jpg")

# 因为输入只有一个图像所以只有一个元素
result = results[0]

# 每个box表示检测到的一个对象
for box in result.boxes:
  class_id = result.names[box.cls[0].item()]
  cords = box.xyxy[0].tolist()
  cords = [round(x) for x in cords]
  conf = round(box.conf[0].item(), 2)
  print("Object type:", class_id)
  print("Coordinates:", cords)
  print("Probability:", conf)
  print("---")

image = Image.fromarray(result.plot()[:,:,::-1])
image.show()