-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpostprocessing_merge_annotations.py
More file actions
61 lines (44 loc) · 1.44 KB
/
postprocessing_merge_annotations.py
File metadata and controls
61 lines (44 loc) · 1.44 KB
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
54
55
56
57
58
59
60
61
"""
Use this script to merge the individual annotation files into one file per image.
If before running the script you have:
annotations/
- 1.png/
- eye_left.json
- eye_right.json
- 2.png/
- eye_left.json
- eye_right.json
After running the script you will have a new folder in annotations:
annotations/
- 1.png /
- eye_left.json
- eye_right.json
- 2.png /
- eye_left.json
- eye_right.json
- /__annotations_merged
- 1.png.json
- 2.png.json
"""
from pathlib import Path
import json
import config
IMAGE_DIR = Path(config.IMAGE_DIR)
ANNOTATION_DIR = Path(config.ANNOTATION_DIR)
MERGED_ANNOTATIONS_DIR = ANNOTATION_DIR / "__annotations_merged"
def index_images():
images = list(IMAGE_DIR.glob("*.png")) + list(IMAGE_DIR.glob("*.jpg"))
return sorted(images)
def load_annotations(image_name):
annotations = (ANNOTATION_DIR / image_name).glob("*.json")
annotations = {a.name.replace(".json", ""): json.loads(a.read_text()) for a in annotations}
return annotations
def main():
MERGED_ANNOTATIONS_DIR.mkdir(exist_ok=True, parents=False)
print("Writing images with landmarks to {}".format(MERGED_ANNOTATIONS_DIR))
for image in index_images():
print(image)
merged_annotations = load_annotations(image.name)
merged_file = MERGED_ANNOTATIONS_DIR / (image.name + ".json")
merged_file.write_text(json.dumps(merged_annotations, indent=4))
main()