-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelboxReader.py
96 lines (83 loc) · 3.57 KB
/
labelboxReader.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Zhiang Chen, Oct 21, 2019
"""
import numpy as np
import json
from PIL import Image, ImageDraw
class LabelboxReader(object):
def __init__(self, image_size):
self.H, self.W = image_size
self.labels_dict = {}
def readJson(self, path):
with open(path, 'r') as f:
self.labels_dict = json.load(f)
def draw_poly(self, points_list):
polygon = []
for point in points_list:
x = point['x']
y = point['y']
polygon.append((x, y))
if len(polygon) > 2:
img = Image.new('L', (self.H, self.W), 0)
ImageDraw.Draw(img).polygon(polygon, outline=255, fill=255)
return True, np.array(img)
else:
return False, None
def convert2ndarray(self, masks_dict):
cls = []
nd = []
if masks_dict.get('nd_roof', False):
for nd_roof in masks_dict['nd_roof']:
if self.draw_poly(nd_roof['geometry'])[0]:
cls.append(0)
nd.append(self.draw_poly(nd_roof['geometry'])[1])
if masks_dict.get('d0_roof', False):
for d0_roof in masks_dict['d0_roof']:
if self.draw_poly(d0_roof['geometry'])[0]:
cls.append(1)
nd.append(self.draw_poly(d0_roof['geometry'])[1])
if masks_dict.get('d1_roof', False):
for d1_roof in masks_dict['d1_roof']:
if self.draw_poly(d1_roof['geometry'])[0]:
cls.append(2)
nd.append(self.draw_poly(d1_roof['geometry'])[1])
if masks_dict.get('d2_roof', False):
for d2_roof in masks_dict['d2_roof']:
if self.draw_poly(d2_roof['geometry'])[0]:
cls.append(3)
nd.append(self.draw_poly(d2_roof['geometry'])[1])
if masks_dict.get('d3_roof', False):
for d3_roof in masks_dict['d3_roof']:
if self.draw_poly(d3_roof['geometry'])[0]:
cls.append(4)
nd.append(self.draw_poly(d3_roof['geometry'])[1])
return np.array(cls), np.array(nd)
def convert(self):
for label in self.labels_dict:
dataset_name = label['Dataset Name']
image_name = label['External ID']
if not label['Label']:
nd_label = np.zeros((1, self.H, self.W))
cls = np.zeros(1)
elif label['Label'] == 'Skip':
nd_label = np.zeros((1, self.H, self.W))
cls = np.zeros(1)
elif label['Label'].get('tile_damage', False):
if label['Label']['tile_damage'] == 'non_damage':
nd_label = np.zeros((1, self.H, self.W))
cls = np.zeros(1)
else:
cls, nd_label = self.convert2ndarray(label['Label'])
else:
cls, nd_label = self.convert2ndarray(label['Label'])
# save ndarray and classes in two files
nd_label = np.swapaxes(nd_label, 0, 1)
nd_label = np.swapaxes(nd_label, 1, 2)
cls_file_name = dataset_name + "_" + image_name.split('.')[0] + "_cls.npy"
nd_file_name = dataset_name + "_" + image_name.split('.')[0] + "_nd.npy"
np.save("./datasets/Eureka/labels/" + cls_file_name, cls, allow_pickle=True)
np.save("./datasets/Eureka/labels/" + nd_file_name, nd_label, allow_pickle=True)
if __name__ == "__main__":
lb = LabelboxReader(image_size=(1000, 1000))
lb.readJson("./datasets/Eureka/labels.json")
lb.convert()