如何在Python中计数可可格式数据集中某些类的实例

发布于 2025-02-03 04:00:45 字数 527 浏览 5 评论 0原文

我通过使用LabelME软件完成了一些数据注释工作。因此,我现在拥有可可格式的完整数据集。

但是,我正在挣扎着几件事。

在我的数据集中,我总共有10张图像和3个类别,即,苹果,芒果和香蕉。

在这10张图像中,有6张图像包含苹果,5张图像包含芒果,其中4张包含香蕉。

有了我的代码,我可以计算这件事。

在包含苹果的6张图像中,有4张图像有5个苹果 每个。所以现在我想知道每个实例的总数 我的数据集中的类别。是否可能?

例如,我正在附上一张图像。

在此图像上,有X级大象的实例数。我想从注释的数据集中计算X。我需要帮助,因为我非常陌生。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2025-02-10 04:00:45

我可能为时已晚,无法帮助您,但对其他任何人来说。

可可注释文件有5个密钥(用于对象检测)“信息”,“许可”,“图像”,“注释”,“类别”。

注释对列表的每个元素都有一个命令。
类别在类别ID及其名称之间具有映射。

首先,您必须获得该ID->类映射。

然后通过那些注释迭代,将计数添加到相关类中(字典中的增量计数器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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文