forked from potpov/IAN_annotation_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_maps.py
More file actions
60 lines (48 loc) · 1.75 KB
/
generate_maps.py
File metadata and controls
60 lines (48 loc) · 1.75 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
import cv2
import numpy as np
import pathlib
import json
import math
from tqdm import tqdm
from conf import conf
import os
def generate_mask(data):
"""
generate a binary mask from a labelme json file
:param data: data loaded from the json
:return: binary image
"""
# image info
original_name = data['imagePath'].split('\\')[-1][:-4]
width = data['imageWidth']
height = data['imageHeight']
# mask
filename = '{}_map.jpg'.format(original_name).lstrip("0")
mask = np.zeros((height, width), np.uint8)
# draw all shapes from 'data' json
for shape in data['shapes']:
if shape['shape_type'] == 'circle':
cx, cy = np.int32(shape['points'][0])
px, py = np.int32(shape['points'][1])
r = np.int32(math.sqrt((px - cx) ** 2 + (py - cy) ** 2))
mask = cv2.circle(mask, (cx, cy), r, [255, 255, 255], cv2.FILLED)
elif shape['shape_type'] == 'rectangle':
tl_x, tl_y = np.int32(shape['points'][0])
br_x, br_y = np.int32(shape['points'][1])
mask = cv2.rectangle(mask, (tl_x, tl_y), (br_x, br_y), [255, 255, 255], cv2.FILLED)
else:
# generic polygon
contour = np.array(shape['points'], np.int32)
mask = cv2.fillPoly(mask, [contour], [255, 255, 255])
cv2.imwrite(os.path.join(conf.SAVE_DIR, filename), mask)
def main():
filelist = []
for f in pathlib.Path(conf.JSON_DIR).iterdir():
if str(f).lower().endswith('.json'):
filelist.append(f)
for file in tqdm(filelist):
with open(str(file)) as data_file:
generate_mask(json.load(data_file))
if __name__ == '__main__':
pathlib.Path(conf.SAVE_DIR).mkdir(parents=True, exist_ok=True)
main()