-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission_to_mask.py
49 lines (41 loc) · 1.18 KB
/
submission_to_mask.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
#!/usr/bin/python
import os
import sys
import PIL
from PIL import Image
import math
import matplotlib.image as mpimg
import numpy as np
# Convert an array of binary labels to a uint8
def binary_to_uint8(img):
rimg = (img * 255).round().astype(np.uint8)
return rimg
def reconstruct_from_labels(label_file, image_id):
h = 16
w = h
imgwidth = int(math.ceil((600.0/w))*w)
imgheight = int(math.ceil((600.0/h))*h)
nc = 3
im = np.zeros((imgwidth, imgheight), dtype=np.uint8)
f = open(label_file)
lines = f.readlines()
image_id_str = '%.3d_' % image_id
for i in range(1, len(lines)):
line = lines[i]
if not image_id_str in line:
continue
tokens = line.split(',')
id = tokens[0]
prediction = int(tokens[1])
tokens = id.split('_')
i = int(tokens[1])
j = int(tokens[2])
je = min(j+w, imgwidth)
ie = min(i+h, imgheight)
if prediction == 0:
adata = np.zeros((w,h))
else:
adata = np.ones((w,h))
im[j:je, i:ie] = binary_to_uint8(adata)
# Image.fromarray(im).save('prediction_' + '%.3d' % image_id + '.png')
return im