Spaces:
Running
Running
import numpy as np | |
def rle_encode(x, fg_val=1): | |
""" | |
Args: | |
x: numpy array of shape (height, width), 1 - mask, 0 - background | |
Returns: run length encoding as list | |
""" | |
dots = np.where( | |
x.T.flatten() == fg_val)[0] # .T sets Fortran order down-then-right | |
run_lengths = [] | |
prev = -2 | |
for b in dots: | |
if b > prev + 1: | |
run_lengths.extend((b + 1, 0)) | |
run_lengths[-1] += 1 | |
prev = b | |
return run_lengths | |
def list_to_string(x): | |
""" | |
Converts list to a string representation | |
Empty list returns '-' | |
""" | |
if x: # non-empty list | |
s = str(x).replace("[", "").replace("]", "").replace(",", "") | |
else: | |
s = '-' | |
return s | |
def rle_decode(mask_rle, shape=(256, 256)): | |
''' | |
mask_rle: run-length as string formatted (start length) | |
empty predictions need to be encoded with '-' | |
shape: (height, width) of array to return | |
Returns numpy array, 1 - mask, 0 - background | |
''' | |
img = np.zeros(shape[0]*shape[1], dtype=np.uint8) | |
if mask_rle != '-': | |
s = mask_rle.split() | |
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])] | |
starts -= 1 | |
ends = starts + lengths | |
for lo, hi in zip(starts, ends): | |
img[lo:hi] = 1 | |
return img.reshape(shape, order='F') |