YOLOv8中检测多个类时如何获取分段实例的类名?检测没有像这里的
.cls
属性YOLOv8 获取预测的类名称。此外,文档似乎没有提及任何内容,例如这里
当我在预测函数中使用
show=true
参数时,类在结果图像中被区分,但我无法以编程方式获取它们。我的代码可以获取我想要的所有检测结果,但不让我知道哪个是哪个:
from ultralytics import YOLO
model = YOLO("path/to/best.pt")
result = model.predict(os.path.join(cut_dir, im_name), save_conf=True, show=True)
if result[0].masks is not None:
for counter, detection in enumerate(result[0].masks.data):
detected = np.asarray(detection.cpu())
对于分割任务,可以参考结果对象的
boxes.cls
属性来获取检测到的类索引,与检测任务相同。 model.names[class_index]
将返回类名:
from ultralytics import YOLO
model = YOLO("path/to/best.pt")
result = model.predict(os.path.join(cut_dir, im_name), save_conf=True, show=True)
if result[0].masks is not None:
for counter, detection in enumerate(result[0].masks.data):
cls_id = int(result[0].boxes[counter].cls.item())
cls_name = model.names[cls_id]