合并 json 个包含 COCO 人物关键点注释的文件

Combine json files containing COCO person keypoint annotations

我已经使用 coco-annotator 对一些图像进行了注释。 我想将这些注释与现有注释("person_keypoints_train2017.json" 和 "person_keypoints_val2017.json")结合起来。

有人成功过吗? 如果是,如何? (最好使用 python)

json 可以使用 json 模块访问 vlaues 尝试读取这两个文件,从而决定要组合的值:

import json
from collections import OrderedDict
filename1 = "person_keypoints_train2017.json"
with open(filename1) as f:
    data_filename1 = json.load(f, object_pairs_hook=OrderedDict)
filename2 = "person_keypoints_val2017.json"
with open(filename1) as f:
    data_filename2 = json.load(f, object_pairs_hook=OrderedDict)

现在你有两个字典,你可以操作数据了。 为了将它们保存到文件中:

    with open(file_name, 'w') as outfile:
    json.dump(data, outfile, separators=(',', ':'))

简单的解决方案:

使用以下工具Merge COCO Files

您不需要安装任何库

只需 git 克隆并 运行 以下内容:

python merge.py Input1.json Input2.json OUTPUT_JSON.json

从零开始的解决方案:

要合并它们,您需要按照以下步骤操作:

  1. 从所有注释文件中获取整个图像并将它们与它们的 ID 一起存储在字典中(例如,每个图像应表示为一个元组 (filename, ImageID),其中 ImageID 用于映射到注释)
  2. 检查是否有重复的图像名称(这样我们就没有重复的注释)
  3. 将来自不同注释文件的图像列表合并为一个列表
  4. 重置 ImageID 并保留一个临时字典以将旧的映射到新的。
  5. 从文件中收集注释,然后使用 old_to_new 映射将它们映射到新图像的 IDS。
  6. 重置注释 ID