I just want to get class data in my python script like: person, car, truck, dog but my output more than this. Also I can not use results as a string.
Python script:
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model.predict(source="0")
Output:
0: 480x640 1 person, 1 car, 7.1ms
0: 480x640 1 person, 1 car, 7.2ms
0: 480x640 1 person, 1 car, 7.1ms
0: 480x640 1 person, 1 car, 7.1ms
0: 480x640 1 person, 1 car, 7.1ms
0: 480x640 1 person, 7.9ms
0: 480x640 1 person, 7.1ms
0: 480x640 1 person, 1 car, 7.1ms
0: 480x640 1 person, 1 car, 7.1ms
You can pass each class to the model's name dict like this:
from ultralytics.yolo.engine.model import YOLO
model = YOLO("yolov8n.pt")
results = model.predict(stream=True, imgsz=512) # source already setup
names = model.names
for r in results:
for c in r.boxes.cls:
print(names[int(c)])
output:
YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs
bus
person
person
person
person
image 1/2 /home/xyz/ultralytics/ultralytics/assets/bus.jpg: 512x384 4 persons, 1 bus, 35.7ms
person
person
person
tie
tie
image 2/2 /home/xyz/ultralytics/ultralytics/assets/zidane.jpg: 288x512 3 persons, 2 ties, 199.0ms
Speed: 3.9ms pre-process, 117.4ms inference, 27.9ms postprocess per image at shape (1, 3, 512, 512)