YOLOv8 get predicted class name
Asked Answered
F

2

8

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
Fixative answered 29/1, 2023 at 18:46 Comment(2)
A simple internet search will get you a long way: inside-machinelearning.com/en/yolov8-how-to-usePronunciamento
Iterate over results[0].boxes.boxes, they are in the format [x1, y1, x2, y2, score, label]. Use the int(label) as the index for your class list.Pronunciamento
U
20

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)
Upspring answered 30/1, 2023 at 12:40 Comment(6)
Same output from ultralytics.yolo.engine.model import YOLO def main(): model = YOLO("yolov8n.pt") results = model.predict(imgsz=512, stream=True, source="0") # source already setup for r in results: for c in r.boxes.cls: print(model.names[int(c)]) main()Fixative
You added a print so something must changeUpspring
print does not effect anything. I tried many times. Also my file path: /home/gursel/ultralytics/test.py is it okay?Fixative
try: pip install --upgrade ultralyticsUpspring
I did it but I get an error when I added stream: stream is not a valid key. Similar keys: []Fixative
It is solved I recloned ultralytics repo. Also code works correctly thanks! @Mike BFixative
E
0

code with a single loop

clist= res[0].boxes.cls
cls = set()
for cno in clist:
    cls.add(model.names[int(cno)])

print(cls)
Equitation answered 23/4, 2023 at 16:57 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Smatter

© 2022 - 2025 — McMap. All rights reserved.