合并一个目录内的所有COCO格式的JSON文件

这个函数可以将我在D:/bop/ycbv/train_real/coco_annotations/目录下保存的所有的coco 格式的json文件合并

#muzik999/MergeCVATJson
#https://github.com/muzik999/MergeCVATJson
import json
import os

path = 'D:/bop/ycbv/train_real/coco_annotations/'
entries = os.listdir(path)
entries.sort()

main = open(path + entries[0])
main = json.load(main)

for entry in entries[1:]:
    file = open(path + entry)
    file = json.load(file)

    for i in file['images']:
        main['images'].append(i)

    for i in file['annotations']:
        main['annotations'].append(i)
for i in range(len(main['images'])):
    main['images'][i]['id'] = i+1
for i in range(len(main['annotations'])):
    main['annotations'][i]['id'] = i+1

sameID = []
for i in range(0, len(main['annotations'])):
    if(main['annotations'][i]['image_id'] != main['annotations'][i-1]['image_id']):
        sameID.append(main['annotations'][i]['id'])
    #if(main['annotations'][i]['image_id'] == main['annotations'][i-1]['image_id']):

newList = []
c = 1
for i in range(len(sameID)-1):
    newList.extend([c] * (sameID[i+1] - sameID[i]))
    c = c + 1
newList.extend([75,75,75,75])
for i in range(len(newList)):
    main['annotations'][i]['image_id'] = newList[i]
with open('data.json', 'w') as outfile:
    json.dump(main, outfile)
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用以下代码将CVAT生成的多个COCO标签合并一个: ```python import json # 读取CVAT生成的多个COCO标签文件 label_files = ['label1.json', 'label2.json', 'label3.json'] # 替换为您的标签文件名 combined_annotations = { "info": {}, "licenses": [], "images": [], "annotations": [], "categories": [] } # 合并images和annotations image_id = 1 annotation_id = 1 for label_file in label_files: with open(label_file, 'r') as f: data = json.load(f) # 合并images for image in data['images']: image['id'] = image_id combined_annotations['images'].append(image) image_id += 1 # 合并annotations for annotation in data['annotations']: annotation['id'] = annotation_id combined_annotations['annotations'].append(annotation) annotation_id += 1 # 合并categories categories = {} category_id = 1 for label_file in label_files: with open(label_file, 'r') as f: data = json.load(f) for category in data['categories']: category_name = category['name'] if category_name not in categories: category['id'] = category_id categories[category_name] = category_id combined_annotations['categories'].append(category) category_id += 1 # 更新annotations中的category_id for annotation in combined_annotations['annotations']: old_category_id = annotation['category_id'] category_name = next( (category['name'] for category in combined_annotations['categories'] if category['id'] == old_category_id), None) if category_name: new_category_id = categories[category_name] annotation['category_id'] = new_category_id # 保存合并后的标签文件 with open('combined_labels.json', 'w') as f: json.dump(combined_annotations, f) ``` 请将`label_files`列表替换为您的标签文件名列表。将该代码保存为一个Python脚本,并将标签文件和脚本放在同一目录下。运行代码后,它将生成一个名为`combined_labels.json`的文件,其中包含合并后的标签数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值