Skip to content

Commit

Permalink
Merge branch 'master' into fb-LEAP-1340/naive-for-timelinelabels
Browse files Browse the repository at this point in the history
  • Loading branch information
jombooth committed Aug 21, 2024
2 parents 04e3f6a + 3792573 commit df91b20
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
58 changes: 58 additions & 0 deletions evalme/image/brush.py
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
2 changes: 1 addition & 1 deletion evalme/image/object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from collections import defaultdict, Counter

from label_studio_converter.brush import decode_rle
from evalme.image.brush import decode_rle
from shapely.geometry import Polygon, MultiPolygon, LineString
from shapely.ops import unary_union, polygonize

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ scipy>=1.4.1
attr==0.3.1
xmljson==0.2.1
lxml>=4.2.5
label-studio-converter>=0.0.36
python-dateutil
label-studio-tools

0 comments on commit df91b20

Please sign in to comment.