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)