BALO
BALO

Reputation: 1

YOLOv8 counting value

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?

Upvotes: 0

Views: 2082

Answers (2)

Utkarsh Kant
Utkarsh Kant

Reputation: 21

Here is a solution you can try.

# load your model
model = YOLO(model_path)

# save results
res = model.predict(image_file_path)
# save class label names
names = res[0].names    # same as model.names

# store number of objects detected per class label
class_detections_values = []
for k, v in names.items():
    class_detections_values.append(res[0].boxes.cls.tolist().count(k))
# create dictionary of objects detected per class
classes_detected = dict(zip(names.values(), class_detections_values))

Upvotes: 0

hanna_liavoshka
hanna_liavoshka

Reputation: 1295

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)

Upvotes: 0

Related Questions