Skip to content

Instantly share code, notes, and snippets.

@miladfa7
Created March 29, 2024 21:47
Show Gist options
  • Save miladfa7/e55ca8df93c895c2b83380279a5e60b2 to your computer and use it in GitHub Desktop.
Save miladfa7/e55ca8df93c895c2b83380279a5e60b2 to your computer and use it in GitHub Desktop.
how to merge multiple coco json files in python
# pip install pycocotools
from pycocotools.coco import COCO
import json
def merge_coco_json(json_files, output_file):
merged_annotations = {
"info": {},
"licenses": [],
"images": [],
"annotations": [],
"categories": []
}
image_id_offset = 0
annotation_id_offset = 0
category_id_offset = 0
existing_category_ids = set()
for idx, file in enumerate(json_files):
coco = COCO(file)
# Update image IDs to avoid conflicts
for image in coco.dataset['images']:
image['id'] += image_id_offset
merged_annotations['images'].append(image)
# Update annotation IDs to avoid conflicts
for annotation in coco.dataset['annotations']:
annotation['id'] += annotation_id_offset
annotation['image_id'] += image_id_offset
merged_annotations['annotations'].append(annotation)
# Update categories and their IDs to avoid conflicts
for category in coco.dataset['categories']:
if category['id'] not in existing_category_ids:
category['id'] += category_id_offset
merged_annotations['categories'].append(category)
existing_category_ids.add(category['id'])
image_id_offset = len(merged_annotations['images'])
annotation_id_offset = len(merged_annotations['annotations'])
category_id_offset = len(merged_annotations['categories'])
# Save merged annotations to output file
with open(output_file, 'w') as f:
json.dump(merged_annotations, f)
# List of paths to COCO JSON files to merge
json_files = ["part1.json", "part2.json"]
# Output file path for merged annotations
output_file = "merged_coco.json"
# Merge COCO JSON files
merge_coco_json(json_files, output_file)
print("Merged COCO JSON files saved to", output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment