统计coco格式json文件中空目标图像和包含目标图像的数目,并导出所有图像文件名为txt文件

import json
# 统计coco格式json文件中,空目标图像和包含目标图像的数目,
# 并将coco格式json文件中包含的所有图像文件名进行导出

def count_empty_targets(coco_json_file, output_txt_file):
    with open(coco_json_file, 'r') as file:
        data = json.load(file)

    image_count = len(data['images'])
    empty_target_count = 0
    non_empty_target_count = 0

    image_ids_with_annotations = set(anno['image_id'] for anno in data['annotations'])

    with open(output_txt_file, 'w') as txt_file:
        for image in data['images']:
            if image['id'] not in image_ids_with_annotations:
                empty_target_count += 1
            else:
                non_empty_target_count += 1

            txt_file.write(f"{image['file_name']}\n")

    return empty_target_count, non_empty_target_count


if __name__ == '__main__':
    coco_json_file = 'annotations/balanced_val.json'
    output_txt_file = './val_img_name.txt'
    empty_target_count, non_empty_target_count = count_empty_targets(coco_json_file, output_txt_file)

    print(f"Number of images containing empty targets: {empty_target_count}")
    print(f"Number of images containing targets: {non_empty_target_count}")


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鹏要高飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值