-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_mask_from_labelbox.py
64 lines (51 loc) · 2.09 KB
/
generate_mask_from_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
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
"""
Script Name: find_scale.py
Author: Stefan Herdy
Date: 01.02.2023
Description:
Download your Labelbox annotations and save them as cathegorical imge file
Usage:
- Download your export json-file from Labelbox
- Set the path to your json-file
- Set the path to your raw images
- Update your label object names
"""
import json
import requests
import io
from PIL import Image
import urllib
import cv2
import shutil
import numpy as np
with open('path-to-your-json-file') as json_file:
data = json.load(json_file)
for i, d in enumerate(data):
filename = data[i]['External ID']
raw_img = cv2.imread('path-to-your-raw-images' + filename, -1)
shape = np.shape(raw_img)
mask_full = np.zeros(shape)
classes = ['class1', 'class2', 'class3', 'class4']
try:
for idx, obj in enumerate(data[i]['Label']['objects']):
URL = data[i]['Label']['objects'][idx]['instanceURI']
for classname, j in enumerate(classes):
if data[i]['Label']['objects'][idx]['title'] == classname:
cl = j
with requests.get(URL, stream=True) as r:
with open("temp" + data[i]['External ID'] + ".jpg", "wb") as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
img = cv2.imread("temp" + data[i]['External ID'] + ".jpg", 0)
mask = np.where(img == 255)
mask_full[mask] = cl
minv = np.min(mask_full)
maxv = np.max(mask_full)
unique = np.unique(mask_full)
res_mask = cv2.resize(mask_full, (512, 512), interpolation = cv2.INTER_NEAREST)
res_raw = cv2.resize(raw_img, (512, 512), interpolation = cv2.INTER_NEAREST)
cv2.imwrite('masks/' + data[i]['External ID'] + '-mask.png', res_mask)
cv2.imwrite('raw_images/' + data[i]['External ID'] + '-mask.png', res_raw)
except:
pass