合并多个coco格式数据的json标注文件

合并多个coco格式数据集的json标注文件

用这个 https://blog.csdn.net/qq_52101879/article/details/132118517

  1. 拼接多个coco格式的json(单个coco json中可包含多个图片与标注)
  2. 仅支持单个category
  3. 会自动将 image idbbox id 顺序排列
  4. 仅支持 bbox,不支持 segmentaion
if __name__ == '__main__':
    ''' 
        拼接多个coco格式的json(单个coco json中可包含多个图片与标注)
        仅支持单个category
        会自动将image id 与 bbox id 顺序排列
    '''
import json
from pathlib import Path

if __name__ == '__main__':
    json_root = Path('../coco_json')
    output_file = 'train.json'

    categories = [
        {
            "supercategory": "wildfire",
            "id": 1,
            "name": "smoke"
        }
    ]
    total_data = {"images": [], "categories": categories, "annotations": []}
    img_id_count, bbox_id_count = 0, 0
    file_list = [str(i) for i in json_root.iterdir() if i.suffix == '.json']
    print(file_list)
    for js_file in file_list:
        print(js_file)
        with open(js_file, 'r') as f:
            js_data = json.load(f)
            images = js_data['images']
            annotations = js_data['annotations']
        # Dont change origin data
        for img_idx, image in enumerate(images):
            print(img_id_count, bbox_id_count)
            image_new = image.copy()
            origin_img_id = image['id']
            image_new['id'] = img_id_count
            total_data['images'].append(image_new)
            for idx, anno in enumerate(annotations):
                if anno['image_id'] == origin_img_id:
                    anno_new = anno.copy()
                    anno_new['id'] = bbox_id_count
                    anno_new['image_id'] = img_id_count
                    total_data['annotations'].append(anno_new)
                    bbox_id_count += 1
            img_id_count += 1

    with open(output_file, 'w') as f:
        json.dump(total_data, f)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值