提取并打印 Yolov8 结果

问题描述 投票:0回答:1

我想使用Python获取检测到的对象名称(人)和数量以输入到excel

我的代码:

from ultralytics import YOLO
import cv2

model = YOLO('../Yolo-Weights/yolov8l.pt')
results = model("Images/1.png", show= True)

它可以检测到物体,输出是:

Ultralytics YOLOv8.0.26  Python-3.10.0 torch-2.0.1+cpu CPU
YOLOv8l summary (fused): 268 layers, 43668288 parameters, 0 gradients, 165.2 GFLOPs

image 1/1 C:\Users\user\Desktop\Object-Detection-101\Chapter 5 - Running Yolo\Images\1.png: 384x640 8 persons, 1 bus, 4 backpacks, 3 handbags, 1 skateboard, 552.9ms
Speed: 1.0ms pre-process, 552.9ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 640)
extract yolov8
1个回答
0
投票

Yolov8 在结果对象中返回输出,因此您需要使用以下代码片段

我们可以像下面这样做

object_classes = results[0].boxes.cls.to('cpu').tolist()

这将给出检测到的对象的类的索引(来自名称)。 您可以使用以下方式获取名称

class_names = results[0].names

您可以使用下面的代码片段来获取边界框

bboxes_xyxy = results[0].boxes.xyxy.to('cpu').tolist()

参考yolov8_predict了解更多详情

这里我使用了 xyxy 格式,您可以从 yolov8 中的可用格式中选择任何内容

你可以过滤你想要的对象,你可以使用 pandas 加载到 Excel 工作表

请参阅 excel_with pandas 了解如何将 excel 与 pandas 结合使用的详细说明

© www.soinside.com 2019 - 2024. All rights reserved.