如何操作空结果? (即没有检测到)
例如:
608x800 (no detections), 320.5ms
代码:
for box in boxes:
if box.cls == 0:
print("large bottled water")
elif box.cls == 1:
print("medium bottled water")
elif box.cls == 2:
print("medium soda")
elif box.cls == 3:
print("medium thick bottle")
elif box.cls == 4:
print("small bottled water")
elif box.cls == 5:
print("small soda")
elif box.cls == 6:
print("small thick bottle")
elif box.cls == 7:
print("small thick bottle")
elif box.cls == 8:
print("xs soda")
elif box.cls == 9:
print("xs thick bottle")
我只尝试过这个:
if box.cls == "":
print("no detection")
我阅读了文档但找不到解决方案
在这种情况下,我会在迭代之前检查
result
是否为空(未检测)。从 torch.Tensor 获取普通 cls
值:cls = int(box.cls.item())
from ultralytics import YOLO
model = YOLO('yolov8s.pt')
results = model(source='image.jpg') # or different type of source
for result in results:
if result:
for box in result.boxes:
cls = int(box.cls.item())
if cls == 15:
print("large bottled water")
elif cls == 1:
print("medium bottled water")
elif cls == 2:
print("medium soda")
else:
print("no detection")