Skip to content

Commit bc83de9

Browse files
author
Matt Sokoloff
committed
recommended changes
1 parent 5129601 commit bc83de9

File tree

6 files changed

+47
-48
lines changed

6 files changed

+47
-48
lines changed

labelbox/data/annotation_types/geometry/geometry.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Any, Union
1+
from typing import Dict, Any, Optional, Union
22

33
import geojson
44
import numpy as np
@@ -25,3 +25,12 @@ def shapely(
2525

2626
def raster(self, *args, **kwargs) -> np.ndarray:
2727
raise NotImplementedError("Subclass must override this")
28+
29+
def get_or_create_canvas(self, height: Optional[int], width: Optional[int],
30+
canvas: Optional[np.ndarray]):
31+
if canvas is None:
32+
if height is None or width is None:
33+
raise ValueError(
34+
"Must either provide canvas or height and width")
35+
canvas = np.zeros((height, width, 3), dtype=np.uint8)
36+
return canvas

labelbox/data/annotation_types/geometry/line.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import List, Optional, Union, Tuple
22

33
import geojson
44
import numpy as np
@@ -19,25 +19,22 @@ def geometry(self) -> geojson.MultiLineString:
1919
def raster(self,
2020
height: Optional[int] = None,
2121
width: Optional[int] = None,
22-
thickness=1,
23-
color=(255, 255, 255),
24-
canvas=None) -> np.ndarray:
22+
thickness: int = 1,
23+
color: Union[Tuple, int] = (255, 255, 255),
24+
canvas: Optional[np.ndarray] = None) -> np.ndarray:
2525
"""
2626
Draw the line onto a 3d mask
2727
Args:
2828
height (int): height of the mask
2929
width (int): width of the mask
3030
thickness (int): How thick to draw the line
31-
color (int): color for the line. Only a single int since this is a grayscale mask.
31+
color (int): color for the line.
32+
RGB values by default but if a 2D canvas is provided this can set this to an int.
33+
canvas (np.ndarry): Canvas for drawing line on.
3234
Returns:
3335
numpy array representing the mask with the line drawn on it.
3436
"""
35-
if canvas is None:
36-
if height is None or width is None:
37-
raise ValueError(
38-
"Must either provide canvas or height and width")
39-
canvas = np.zeros((height, width, 3), dtype=np.uint8)
40-
37+
canvas = self.get_or_create_canvas(height, width, canvas)
4138
pts = np.array(self.geometry['coordinates']).astype(np.int32)
4239
return cv2.polylines(canvas,
4340
pts,

labelbox/data/annotation_types/geometry/point.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Tuple, Union
22

33
import geojson
44
import numpy as np
@@ -19,23 +19,21 @@ def raster(self,
1919
height: Optional[int] = None,
2020
width: Optional[int] = None,
2121
thickness: int = 1,
22-
color=(255, 255, 255),
23-
canvas=None) -> np.ndarray:
22+
color: Union[Tuple, int] = (255, 255, 255),
23+
canvas: Optional[np.ndarray] = None) -> np.ndarray:
2424
"""
2525
Draw the point onto a 3d mask
2626
Args:
2727
height (int): height of the mask
2828
width (int): width of the mask
2929
thickness (int): pixel radius of the point
30-
color (int): color for the point. Only a single int since this is a grayscale mask.
30+
color (int): color for the point.
31+
RGB values by default but if a 2D canvas is provided this can set this to an int.
32+
canvas (np.ndarray): Canvas to draw the point on
3133
Returns:
3234
numpy array representing the mask with the point drawn on it.
3335
"""
34-
if canvas is None:
35-
if height is None or width is None:
36-
raise ValueError(
37-
"Must either provide canvas or height and width")
38-
canvas = np.zeros((height, width, 3), dtype=np.uint8)
36+
canvas = self.get_or_create_canvas(height, width, canvas)
3937
return cv2.circle(canvas, (int(self.x), int(self.y)),
4038
radius=thickness,
4139
color=color,

labelbox/data/annotation_types/geometry/polygon.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import List, Optional, Union, Tuple
22

33
import numpy as np
44
import geojson
@@ -21,23 +21,22 @@ def geometry(self) -> geojson.MultiPolygon:
2121
def raster(self,
2222
height: Optional[int] = None,
2323
width: Optional[int] = None,
24-
color=(255, 255, 255),
25-
thickness=-1,
26-
canvas=None) -> np.ndarray:
24+
color: Union[int, Tuple] = (255, 255, 255),
25+
thickness: int = -1,
26+
canvas: Optional[np.ndarray] = None) -> np.ndarray:
2727
"""
2828
Draw the polygon onto a 3d mask
2929
Args:
3030
height (int): height of the mask
3131
width (int): width of the mask
32-
color (int): color for the polygon. Only a single int since this is a grayscale mask.
32+
color (int): color for the polygon.
33+
RGB values by default but if a 2D canvas is provided this can set this to an int.
34+
thickness (int): How thick to make the polygon border. -1 fills in the polygon
35+
canvas (np.ndarray): Canvas to draw the polygon on
3336
Returns:
3437
numpy array representing the mask with the polygon drawn on it.
3538
"""
36-
if canvas is None:
37-
if height is None or width is None:
38-
raise ValueError(
39-
"Must either provide canvas or height and width")
40-
canvas = np.zeros((height, width, 3), dtype=np.uint8)
39+
canvas = self.get_or_create_canvas(height, width, canvas)
4140
pts = np.array(self.geometry['coordinates']).astype(np.int32)
4241
if thickness == -1:
4342
return cv2.fillPoly(canvas, pts, color)

labelbox/data/annotation_types/geometry/rectangle.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union, Tuple
22

33
import cv2
44
import geojson
@@ -31,23 +31,22 @@ def geometry(self) -> geojson.geometry.Geometry:
3131
def raster(self,
3232
height: Optional[int] = None,
3333
width: Optional[int] = None,
34-
color=(255, 255, 255),
35-
thickness=-1,
36-
canvas=None) -> np.ndarray:
34+
color: Union[int, Tuple] = (255, 255, 255),
35+
thickness: int = -1,
36+
canvas: Optional[np.ndarray] = None) -> np.ndarray:
3737
"""
3838
Draw the rectangle onto a 3d mask
3939
Args:
4040
height (int): height of the mask
4141
width (int): width of the mask
42-
color (int): color for the rectangle. Only a single int since this is a grayscale mask.
42+
color (int): color for the polygon.
43+
RGB values by default but if a 2D canvas is provided this can set this to an int.
44+
thickness (int): How thick to make the rectangle border. -1 fills in the rectangle
45+
canvas (np.ndarray): Canvas to draw rectangle on
4346
Returns:
4447
numpy array representing the mask with the rectangle drawn on it.
4548
"""
46-
if canvas is None:
47-
if height is None or width is None:
48-
raise ValueError(
49-
"Must either provide canvas or height and width")
50-
canvas = np.zeros((height, width, 3), dtype=np.uint8)
49+
canvas = self.get_or_create_canvas(height, width, canvas)
5150
pts = np.array(self.geometry['coordinates']).astype(np.int32)
5251
if thickness == -1:
5352
return cv2.fillPoly(canvas, pts, color)

labelbox/data/serialization/labelbox_v1/converter.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ def deserialize_video(json_data: Iterable[Dict[str, Any]],
2929
Returns:
3030
LabelGenerator containing the video data.
3131
"""
32-
33-
def label_generator():
34-
for example in LBV1VideoIterator(json_data, client):
35-
if example['Label']:
36-
yield LBV1Label(**example).to_common()
37-
38-
return LabelGenerator(data=label_generator())
32+
label_generator = (LBV1Label(**example).to_common()
33+
for example in LBV1VideoIterator(json_data, client)
34+
if example['Label'])
35+
return LabelGenerator(data=label_generator)
3936

4037
@staticmethod
4138
def deserialize(json_data: Iterable[Dict[str, Any]]) -> LabelGenerator:

0 commit comments

Comments
 (0)