目标检测 利用COCO格式数据集的json文件,统计数据集中bounding box的尺寸分布、统计小尺寸目标的尺寸分布

        读取一个MSCOCO格式的json文件,统计数据集中的boundingbox尺寸分布,把height、width信息保存到两个list中去,然后绘制统计图,可视化尺寸分布。

        还可以自己加一些别的功能

import json
import matplotlib.pyplot as plt


def bbox_distribution(json_file, small_h, small_w):
    with open(json_file, 'r') as f:
        data = json.load(f)

    bbox_widths = []
    bbox_heights = []

    for annotation in data['annotations']:
        bbox = annotation['bbox']
        bbox_widths.append(bbox[2])
        bbox_heights.append(bbox[3])
        # 如果height或者width小于阈值,把annotation信息保存到一个txt文件
        if bbox[2] < small_w or bbox[3] < small_h:
            with open('small.txt', 'a') as f:
                f.write(str(annotation) + '\n')

    return bbox_widths, bbox_heights


def plot_distribution(widths, heights):
    plt.figure(figsize=(12, 6))

    plt.subplot(1, 2, 1)
    plt.hist(widths, bins=40, color='blue', edgecolor='black')
    plt.title('Distribution of BBox Widths')
    plt.xlabel('Width')
    plt.ylabel('Count')

    plt.subplot(1, 2, 2)
    plt.hist(heights, bins=40, color='blue', edgecolor='black')
    plt.title('Distribution of BBox Heights')
    plt.xlabel('Height')
    plt.ylabel('Count')

    plt.tight_layout()
    plt.show()


# 绘制长在0-100之间的bbox的分布
def plot_distribution_small(widths, heights):
    plt.figure(figsize=(12, 6))

    plt.subplot(1, 2, 1)
    plt.hist(widths, bins=20, color='blue', edgecolor='black', range=(0, 100))
    plt.title('Distribution of BBox Widths')
    plt.xlabel('Width')
    # x轴的刻度
    plt.xticks(range(0, 100, 5))
    plt.ylabel('Count')

    plt.subplot(1, 2, 2)
    plt.hist(heights, bins=20, color='blue', edgecolor='black', range=(0, 100))
    plt.title('Distribution of BBox Heights')
    plt.xlabel('Height')
    plt.xticks(range(0, 100, 5))
    plt.ylabel('Count')

    plt.tight_layout()
    plt.show()


def main():
    json_file = 'RSOD/train.json'
    small_h = 5
    small_w = 5
    bbox_widths, bbox_heights = bbox_distribution(json_file, small_h, small_w)
    plot_distribution(bbox_widths, bbox_heights)
    plot_distribution_small(bbox_widths, bbox_heights)


if __name__ == "__main__":
    main()

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中描述了COCO数据集的基本信息格式,其中包括以下几个部分: 1. "info": 描述数据集的相关信息,包括数据集的版本,创建者,年份等。 2. "licenses": 描述数据集中使用的许可证信息,通常包括许可证的ID,许可证的URL和许可证的名称。 3. "images": 描述图片的信息,每张图片都有一个字典进行描述,包括图片的ID,文件名,宽度,高度等。 4. "annotations": 描述bounding box的信息,每个bounding box都有一个字典进行描述,包括bounding box的ID,对应的图片ID,bounding box的坐标,类别ID等。 5. "categories": 描述图片类别的信息,每个类别都有一个字典进行描述,包括类别的ID,类别的名称,类别的超类等。 以上是COCO数据集的基本信息格式,可以通过一个json文件进行存储和读取。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详细!正确!COCO数据集(.json)训练格式转换成YOLO格式(.txt)](https://blog.csdn.net/qq_42012888/article/details/120270283)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [COCO 格式数据集(json文件转txt)](https://blog.csdn.net/qq_48297115/article/details/129476009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [标准COCO格式json文件内容](https://blog.csdn.net/qq_34972053/article/details/130622204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值