自建数据集系列:cocoJson数据集统计分析

前言

在这里插入图片描述
之前文章中放了这么一张统计的图,虽然给出了每个类别对应的实例数,一看就是及其不均衡,不利于分类。但这里实例的图片分布并没有,不利于后续的不均衡扩增。

实现

cocoJsonStat.py

import json
from unicodedata import category
import tqdm
import os

json_file = "COD10K_CAM_coco/annotations/instances_train2017.json"

cnt_dict = {}
with open(json_file) as f:
        data = json.load(f)

        # Create image dict
        images = {'%g' % x['id']: x for x in data['images']}

        categories = {'%g' % x['id']: x for x in data['categories']}

        for x in data['categories']:
            if not x['supercategory'] in cnt_dict.keys():
                cnt_dict[x['supercategory']] = {x["name"]:{"class_id":x["id"],"cnt":0,"imgs":[]}}
            else:
                cnt_dict[x['supercategory']][x["name"]] = {"class_id":x["id"],"cnt":0,"imgs":[]}

        

        # Write labels file
        for x in data['annotations']:
            if x['iscrowd']:
                print("啥")
                continue
            
            cate = categories['%g' % x['category_id']]
            img = images['%g' % x['image_id']]

            cur_obj = cnt_dict[cate['supercategory']][cate["name"]]

            cur_obj["cnt"] += 1
            cur_obj["imgs"].append(img["file_name"])

        print(cnt_dict)

save_json_path = os.path.join("./", "%s.json" % "stat_CAM_coco_train.json")
json.dump(cnt_dict, open(save_json_path, 'w'), indent=4)

在这里插入图片描述
点到为止,之后的数据处理操作,大伙就各抒己见吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星空•物语

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

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

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

打赏作者

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

抵扣说明:

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

余额充值