根据 Python 中训练的 YOLOv8 模型的检测对象结果预测类别

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

我有一个经过训练的模型,并且我已经使用以下代码检测到了我所需的对象

import cv2
from PIL import Image
from ultralytics import YOLO

image = cv2.imread("screenshot.png")
model = YOLO('runs/detect/train4/weights/best.pt')
results = model.predict(image, show=True, stream=True, classes=0, imgsz=512)
for result in results:
    for box in result.boxes:
        class_id = result.names[box.cls[0].item()]
        if (class_id == "myclassname"): 
            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("---")

从检测到的图像部分中,我需要检测其他类,我该如何做到这一点?

我已经搜索了足够多的内容,但我看不到任何关于此的帖子。

python deep-learning object-detection yolov8
1个回答
0
投票

获取 xyxy 框坐标 ([x1 y1 x2 y2]),从原始图像中切片(裁剪)框区域:

box_image = image[y1:y2, x1:x2]
,并使用更改为相关类的“classes”参数对此裁剪后的图像进行另一个预测id:
model.predict(box_image, classes=0)
.

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