-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcollect_annotations.py
39 lines (31 loc) · 1.28 KB
/
collect_annotations.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
"""collect annotations in subfolders"""
import argparse
from pathlib import Path
import json
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--root', type=str, required=True, help='path to root folder')
args = parser.parse_args()
root = Path(args.root)
output_json = root / 'annotations.json'
assert not output_json.exists()
images = []
annotations = []
categories = []
annotation_files = root.glob("**/annotations.json")
for anno_file in annotation_files:
print('read', anno_file)
parent = anno_file.relative_to(root).parent
data = json.load(anno_file.open())
for image in data['images']:
image['file_name'] = str(parent / image['file_name'])
images += data['images']
annotations += data['annotations']
categories = data['categories'] # assume all categories are the same
with output_json.open('w') as f:
print('save', output_json)
for i, anno in enumerate(annotations):
anno['id'] = i
anno['iscrowd'] = 0
anno['segmentation'] = []
json.dump(dict(images=images, annotations=annotations, categories=categories), f)