-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fb-LEAP-1340/naive-for-timelinelabels
- Loading branch information
Showing
3 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import numpy as np | ||
# This code was copy-pasted from `label_studio_converter.brush.decode_rle` and should be kept in sync with that function. | ||
# It can be removed if `evalme` is merged into SDK. | ||
|
||
class InputStream: | ||
def __init__(self, data): | ||
self.data = data | ||
self.i = 0 | ||
|
||
def read(self, size): | ||
out = self.data[self.i : self.i + size] | ||
self.i += size | ||
return int(out, 2) | ||
|
||
|
||
def access_bit(data, num): | ||
"""from bytes array to bits by num position""" | ||
base = int(num // 8) | ||
shift = 7 - int(num % 8) | ||
return (data[base] & (1 << shift)) >> shift | ||
|
||
|
||
def bytes2bit(data): | ||
"""get bit string from bytes data""" | ||
return "".join([str(access_bit(data, i)) for i in range(len(data) * 8)]) | ||
|
||
|
||
def decode_rle(rle, print_params: bool = False): | ||
"""from LS RLE to numpy uint8 3d image [width, height, channel] | ||
Args: | ||
print_params (bool, optional): If true, a RLE parameters print statement is suppressed | ||
""" | ||
input = InputStream(bytes2bit(rle)) | ||
num = input.read(32) | ||
word_size = input.read(5) + 1 | ||
rle_sizes = [input.read(4) + 1 for _ in range(4)] | ||
|
||
if print_params: | ||
print( | ||
"RLE params:", num, "values", word_size, "word_size", rle_sizes, "rle_sizes" | ||
) | ||
|
||
i = 0 | ||
out = np.zeros(num, dtype=np.uint8) | ||
while i < num: | ||
x = input.read(1) | ||
j = i + 1 + input.read(rle_sizes[input.read(2)]) | ||
if x: | ||
val = input.read(word_size) | ||
out[i:j] = val | ||
i = j | ||
else: | ||
while i < j: | ||
val = input.read(word_size) | ||
out[i] = val | ||
i += 1 | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters