-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_labelbox.py
51 lines (45 loc) · 1.36 KB
/
dump_labelbox.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
import sys
import os
import json
import requests
import cv2
import numpy as np
def load_json(fjs):
with open(fjs) as f:
js = f.read()
return json.loads(js)
def dump(js, path):
for jsone in js:
name = jsone['External ID']
print(f'[INFO] Processing {name}...')
try:
objs = jsone['Label']['objects']
except:
continue
mask = None
for i, obj in enumerate(objs):
link = obj['instanceURI']
while True:
try:
print(f' [INFO] Downloading instance {i}...', end='')
r = requests.get(link)
except KeyboardInterrupt:
exit()
except:
print(' retrying...', end='')
else:
break
if r.status_code == 200:
print(' OK!')
img = np.frombuffer(r.content, dtype=np.uint8)
img = cv2.imdecode(img, 0)
mask = img if mask is None else cv2.bitwise_or(mask, img)
else:
print(f' [ERROR] Status {r.status_code}')
mName = name.rsplit('.', maxsplit=1)[0] + '.png'
cv2.imwrite(os.path.join(path, mName), mask)
if __name__ == '__main__':
fjs = sys.argv[1]
path = sys.argv[2]
js = load_json(fjs)
dump(js, path)