-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge_json.py
54 lines (41 loc) · 1.73 KB
/
merge_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import os
import argparse
def merge_json_files(src, dest):
if not os.path.exists(src):
raise Exception('Given .json annotation input file doesn\'t exist.')
if not os.path.exists(dest):
os.makedirs(dest)
annot_merged = dict()
annot_merged['type'] = 'instances'
annot_merged['info'] = list()
annot_merged['licenses'] = list()
annot_merged['images'] = list()
annot_merged['annotations'] = list()
annot_merged['categories'] = list()
for root, dirs, files in os.walk(src):
for file in files:
if file.endswith(".json"):
json_path = os.path.join(root, file)
print(json_path)
with open(json_path) as f:
annot = json.load(f)
for key in annot.keys():
if not key == 'type':
print(key)
annot_merged[key] += (annot[key])
print(len(annot_merged['images']))
annot_merged['categories'] = list({v['id']: v for v in annot_merged['categories']}.values())
json_save_name = os.path.basename(os.path.normpath(src)) + '.json'
coco_file = os.path.join(dest, json_save_name)
json.dump(annot_merged, open(coco_file, 'w'))
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-src", "--SourceDirectoryPath", required=True,
help="Source directory path for .json files")
ap.add_argument("-dest", "--DestinationDirectoryPath", required=True,
help="Destination directory path for merged .json file")
args = vars(ap.parse_args())
src = args["SourceDirectoryPath"]
dest = args["DestinationDirectoryPath"]
merge_json_files(src, dest)