0

I am working on a project to count vehicles on the road without drawing a line using (yolo) when he detects a vehicle he counts it. enter image description here

I want to reach this value (3 cars) in yolo? To perform some operations on it and display it, if someone can help me?

BALO
  • 1
  • 1
  • Hello, welcome to Stack Overflow! Maybe you should [read this](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557), and then edit your question. – Diego Borba Aug 11 '23 at 20:18
  • 1
    It depends a little on your classes but you can see an example of that exact application here: https://github.com/heathhenley/RhodyCarCounter/blob/main/backend/worker/traffic.py#L68 - The docs give the details of the result object that is returned after running predict (https://docs.ultralytics.com/modes/predict/#working-with-results) – Heath Aug 11 '23 at 20:24
  • Have a look at roboflow's excellent repository supervision: https://github.com/roboflow/supervision. It kind of does it magically out of the box. – Sebastian Aug 14 '23 at 10:10

1 Answers1

0

To get the count of the same class objects from the results, you need to get the id of this class in model.names, and count the appearance of this value in the results[0].boxes.cls (takes all detected objects in the image and gets their class_ids).

# define the model
model = YOLO('yolov8s.pt')
# run inference on the source image
results = model('image.jpg')
# get the model names list
names = model.names
# get the 'car' class id
car_id = list(names)[list(names.values()).index('car')]
# count 'car' objects in the results
results[0].boxes.cls.tolist().count(car_id)
hanna_liavoshka
  • 115
  • 1
  • 6