-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport_labelme.py
executable file
·98 lines (94 loc) · 3.78 KB
/
import_labelme.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
97
98
#!/usr/bin/env python3
import json
def main(labels, labeled=False, dims=tuple()):
"""
import the labels and extract the coordinates to a list
if dimensions are specified, labels that don't exist inside the dims will be ignored
"""
with open(labels) as json_file:
# initialize input and output
labels = json.load(json_file)['shapes']
segments = {}
new_labels = []
# for each segment, grab its pt and store it in the output
for i in range(len(labels)):
pts = labels[i]['points']
if dims:
pts = list(
filter(
lambda pt: (0 <= pt[0] < dims[0]) and (0 <= pt[1] < dims[1]),
pts
)
)
if len(pts) < 3:
continue
# add to the segments dict
label = labels[i]['label']
if type(label) != int:
# convert the label to an integer, ignoring any chars in the string
label = int("".join([s for s in labels[i]['label'] if s.isdigit()]))
segments[label] = pts
# add to the new labels array
new_labels.append(pts)
return segments if labeled else new_labels
def write(file, segments, image_path=None):
"""
write the segments (belonging to image_path) to the file in JSON format
segments can be one of a number of things:
1) a simple list of pts
2) a list of 2-element tuples (label, pts)
3) a list of 3-element tuples (label, pts, class_label)
where class_label is one of two things:
1) a label
2) a 2-element tuple (label, probability)
if image_path is provided, the file will be in valid labelme format
"""
if image_path:
import os.path
image_path = os.path.relpath(image_path, os.path.dirname(file))
with open(file, 'w') as out:
json.dump(
{
'flags': {},
'shapes': [
{
'label': str(segment[0]),
'line_color': None,
'fill_color': None,
'points': segment[1],
'shape_type': "polygon",
'flags': dict([
segment[2] if type(segment[2]) is tuple else (segment[2], True)
]) if len(segment) == 3 else {}
}
if type(segment) is tuple else
{
'label': str(idx),
'line_color': None,
'fill_color': None,
'points': segment,
'shape_type': "polygon",
'flags': {}
}
for idx, segment in enumerate(segments)
],
"lineColor": [0,255,0,128],
"fillColor": [255,0,0,128],
"imagePath": image_path,
"imageData": None
},
out
)
if __name__ == '__main__':
# if this script is being called but not imported:
import argparse
parser = argparse.ArgumentParser(description='Import the labelme labels as a simple python list.')
parser.add_argument(
"labels", help="the path to the json file containing the labelme labels"
)
parser.add_argument(
"dims", nargs='?', default='', help="specify the width and height (separated by a single comma) in pixels of the image if labels that are outside that range should be ignored"
)
args = parser.parse_args()
args.dims = tuple(filter(lambda i: i, args.dims.split(",")))
print(main(args.labels, False, args.dims))