Skip to content

Commit 5bf1691

Browse files
author
Matt Sokoloff
committed
update drawing interface
1 parent be88bf1 commit 5bf1691

File tree

9 files changed

+28
-39
lines changed

9 files changed

+28
-39
lines changed

examples/label_export/images.ipynb

Lines changed: 5 additions & 14 deletions
Large diffs are not rendered by default.

labelbox/data/annotation_types/data/raster.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def bytes_to_np(self, image_bytes: bytes) -> np.ndarray:
3838
Returns:
3939
numpy array representing the image
4040
"""
41-
return np.array(Image.open(BytesIO(image_bytes)))[:, :, :3]
41+
arr = np.array(Image.open(BytesIO(image_bytes)))
42+
if len(arr.shape) == 2:
43+
arr = np.stack((arr,) * 3, axis=-1)
44+
return arr[:, :, :3]
4245

4346
def np_to_bytes(self, arr: np.ndarray) -> bytes:
4447
"""

labelbox/data/annotation_types/geometry/geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def raster(self,
2727
height: Optional[int] = None,
2828
width: Optional[int] = None,
2929
canvas: Optional[np.ndarray] = None,
30-
color: Optional[Union[int, Tuple[int,int,int]]] = None,
30+
color: Optional[Union[int, Tuple[int, int, int]]] = None,
3131
thickness: Optional[int] = 1) -> np.ndarray:
3232
raise NotImplementedError("Subclass must override this")
3333

labelbox/data/annotation_types/geometry/line.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def geometry(self) -> geojson.MultiLineString:
1717
[[[point.x, point.y] for point in self.points]])
1818

1919
def raster(self,
20-
height: Optional[int] = None,
20+
height: Optional[int] = None,
2121
width: Optional[int] = None,
2222
canvas: Optional[np.ndarray] = None,
23-
color: Union[int, Tuple[int,int,int]] = (255, 255, 255),
24-
thickness: int = 5) -> np.ndarray:
23+
color: Union[int, Tuple[int, int, int]] = (255, 255, 255),
24+
thickness: int = 1) -> np.ndarray:
2525
"""
2626
Draw the line onto a 3d mask
2727
Args:

labelbox/data/annotation_types/geometry/mask.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Mask(Geometry):
1818

1919
@property
2020
def geometry(self):
21-
mask = self.raster(color = 1)
21+
mask = self.raster(color=1)
2222
polygons = (
2323
shape(shp)
2424
for shp, val in shapes(mask, mask=None)
@@ -30,9 +30,8 @@ def raster(self,
3030
height: Optional[int] = None,
3131
width: Optional[int] = None,
3232
canvas: Optional[np.ndarray] = None,
33-
color: Optional[Union[int, Tuple[int,int,int]]] = None,
34-
thickness = None
35-
) -> np.ndarray:
33+
color: Optional[Union[int, Tuple[int, int, int]]] = None,
34+
thickness=None) -> np.ndarray:
3635
"""
3736
# TODO: Optionally use the color. a color of 1 will result in a binary canvas
3837
@@ -65,13 +64,11 @@ def raster(self,
6564
if isinstance(color, (tuple, list)):
6665
dims = dims + [len(color)]
6766

68-
canvas = canvas if canvas is not None else np.zeros(tuple(dims), dtype=np.uint8)
67+
canvas = canvas if canvas is not None else np.zeros(tuple(dims),
68+
dtype=np.uint8)
6969
canvas[mask.astype(np.bool)] = color
7070
return canvas
7171

72-
73-
74-
7572
def create_url(self, signer: Callable[[bytes], str]) -> str:
7673
"""
7774
Update the segmentation mask to have a url.

labelbox/data/annotation_types/geometry/point.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ def raster(self,
1919
height: Optional[int] = None,
2020
width: Optional[int] = None,
2121
canvas: Optional[np.ndarray] = None,
22-
color: Union[int, Tuple[int,int,int]] = (255, 255, 255),
23-
thickness: int = 10
24-
) -> np.ndarray:
22+
color: Union[int, Tuple[int, int, int]] = (255, 255, 255),
23+
thickness: int = 10) -> np.ndarray:
2524
"""
2625
Draw the point onto a 3d mask
2726
Args:
@@ -38,4 +37,4 @@ def raster(self,
3837
return cv2.circle(canvas, (int(self.x), int(self.y)),
3938
radius=thickness,
4039
color=color,
41-
thickness=-1)
40+
thickness=1)

labelbox/data/annotation_types/geometry/polygon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def raster(self,
2222
height: Optional[int] = None,
2323
width: Optional[int] = None,
2424
canvas: Optional[np.ndarray] = None,
25-
color: Union[int, Tuple[int,int,int]] = (255, 255, 255),
25+
color: Union[int, Tuple[int, int, int]] = (255, 255, 255),
2626
thickness: int = -1) -> np.ndarray:
2727
"""
2828
Draw the polygon onto a 3d mask

labelbox/data/annotation_types/geometry/rectangle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def raster(self,
3232
height: Optional[int] = None,
3333
width: Optional[int] = None,
3434
canvas: Optional[np.ndarray] = None,
35-
color: Union[int, Tuple[int,int,int]] = (255, 255, 255),
35+
color: Union[int, Tuple[int, int, int]] = (255, 255, 255),
3636
thickness: int = -1) -> np.ndarray:
3737
"""
3838
Draw the rectangle onto a 3d mask

labelbox/data/metrics/iou.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,11 @@ def mask_miou(ground_truths: List[ObjectAnnotation],
128128
Returns:
129129
float representing the iou score for the masks
130130
"""
131-
prediction_np = np.max(
132-
[pred.value.raster(color=1) for pred in predictions], axis=0)
133-
ground_truth_np = np.max([
134-
ground_truth.value.raster(color=1) for ground_truth in ground_truths
135-
],
136-
axis=0)
131+
prediction_np = np.max([pred.value.raster(color=1) for pred in predictions],
132+
axis=0)
133+
ground_truth_np = np.max(
134+
[ground_truth.value.raster(color=1) for ground_truth in ground_truths],
135+
axis=0)
137136
if prediction_np.shape != ground_truth_np.shape:
138137
raise ValueError(
139138
"Prediction and mask must have the same shape."

0 commit comments

Comments
 (0)