1

I have done some data annotation work by using labelme software. So, I have now the complete dataset in COCO format.

However, I am struggling with a few things.

In my dataset, I have in total of 10 images, and 3 categories,i.e., apple, mango, and banana.

Of those 10 images, 6 images contain apple, 5 images contain mango, and 4 contain banana.

With my code, I can count this thing.

Among those 6 images which contain apples, 4 images have 5 apples each. So now I want to know the total number of instances of each category in my dataset. Is it possible?

for example, I am attaching one image.

enter image description here

On this image, there is X number of instances from Class Elephant. I want to count the X from my annotated dataset. I need help because I am very new to this.

Thanks in Advance.

1 Answer 1

2

I'm probably far too late to help you, but for anyone else.

COCO annotation files have 5 keys (for object detection) “info”, “licenses”, “images”, “annotations”, “categories”.

Annotations has a dict for each element of a list. Categories has a mapping between category IDs and their names.

First you have to get that ID -> Class Mapping.

Then iterate through those annotations adding a count to the relevant class (increment counter in dictionary by 1).

def get_count_per_class(data: dict) -> dict:
    """
    Count per class given COCO annotations in dict format.
    """
    id_to_class_name = {x['id']: x['name'] for x in data['categories']} 
    annotations = data['annotations']
    
    counts = {}
    for annotation in annotations:
        class_name = id_to_class_name[annotation['category_id']]
        counts[class_name] = counts.get(class_name, 0) + 1
        
    return counts
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.