Skip to content

Commit 8567e0c

Browse files
authored
Merge pull request #395 from Labelbox/al-1299
addition of from shapely function for annotation types
2 parents 9ce5724 + 10d49fc commit 8567e0c

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

labelbox/data/annotation_types/data/tiled_image.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
from pydantic import BaseModel, validator
1616
from pydantic.class_validators import root_validator
1717

18-
from labelbox.data.annotation_types.geometry.polygon import Polygon
19-
from labelbox.data.annotation_types.geometry.point import Point
20-
from labelbox.data.annotation_types.geometry.line import Line
21-
from labelbox.data.annotation_types.geometry.rectangle import Rectangle
22-
from ..geometry import Point
18+
from labelbox.data.annotation_types import Rectangle, Point, Line, Polygon
2319
from .base_data import BaseData
2420
from .raster import RasterData
2521

labelbox/data/annotation_types/geometry/line.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import cv2
66
from pydantic import validator
7+
from shapely.geometry import LineString as SLineString
78

89
from .point import Point
910
from .geometry import Geometry
@@ -25,6 +26,17 @@ def geometry(self) -> geojson.MultiLineString:
2526
return geojson.MultiLineString(
2627
[[[point.x, point.y] for point in self.points]])
2728

29+
@classmethod
30+
def from_shapely(cls, shapely_obj: SLineString) -> "Line":
31+
"""Transforms a shapely object."""
32+
if not isinstance(shapely_obj, SLineString):
33+
raise TypeError(
34+
f"Expected Shapely Line. Got {shapely_obj.geom_type}")
35+
36+
obj_coords = shapely_obj.__geo_interface__['coordinates']
37+
return Line(
38+
points=[Point(x=coords[0], y=coords[1]) for coords in obj_coords])
39+
2840
def draw(self,
2941
height: Optional[int] = None,
3042
width: Optional[int] = None,

labelbox/data/annotation_types/geometry/point.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import geojson
44
import numpy as np
55
import cv2
6+
from shapely.geometry import Point as SPoint
67

78
from .geometry import Geometry
89

@@ -24,6 +25,16 @@ class Point(Geometry):
2425
def geometry(self) -> geojson.Point:
2526
return geojson.Point((self.x, self.y))
2627

28+
@classmethod
29+
def from_shapely(cls, shapely_obj: SPoint) -> "Point":
30+
"""Transforms a shapely object."""
31+
if not isinstance(shapely_obj, SPoint):
32+
raise TypeError(
33+
f"Expected Shapely Point. Got {shapely_obj.geom_type}")
34+
35+
obj_coords = shapely_obj.__geo_interface__['coordinates']
36+
return Point(x=obj_coords[0], y=obj_coords[1])
37+
2738
def draw(self,
2839
height: Optional[int] = None,
2940
width: Optional[int] = None,

labelbox/data/annotation_types/geometry/polygon.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import geojson
55
import numpy as np
66
from pydantic import validator
7+
from shapely.geometry import Polygon as SPolygon
78

89
from .geometry import Geometry
910
from .point import Point
@@ -30,6 +31,17 @@ def geometry(self) -> geojson.Polygon:
3031
self.points.append(self.points[0])
3132
return geojson.Polygon([[(point.x, point.y) for point in self.points]])
3233

34+
@classmethod
35+
def from_shapely(cls, shapely_obj: SPolygon) -> "Polygon":
36+
"""Transforms a shapely object."""
37+
#we only consider 0th index because we only allow for filled polygons
38+
if not isinstance(shapely_obj, SPolygon):
39+
raise TypeError(
40+
f"Expected Shapely Polygon. Got {shapely_obj.geom_type}")
41+
obj_coords = shapely_obj.__geo_interface__['coordinates'][0]
42+
return Polygon(
43+
points=[Point(x=coords[0], y=coords[1]) for coords in obj_coords])
44+
3345
def draw(self,
3446
height: Optional[int] = None,
3547
width: Optional[int] = None,

labelbox/data/annotation_types/geometry/rectangle.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cv2
44
import geojson
55
import numpy as np
6+
from shapely.geometry import Polygon as SPolygon
67

78
from .geometry import Geometry
89
from .point import Point
@@ -30,6 +31,24 @@ def geometry(self) -> geojson.geometry.Geometry:
3031
[self.start.x, self.start.y],
3132
]])
3233

34+
@classmethod
35+
def from_shapely(cls, shapely_obj: SPolygon) -> "Rectangle":
36+
"""Transforms a shapely object.
37+
38+
If the provided shape is a non-rectangular polygon, a rectangle will be
39+
returned based on the min and max x,y values."""
40+
if not isinstance(shapely_obj, SPolygon):
41+
raise TypeError(
42+
f"Expected Shapely Polygon. Got {shapely_obj.geom_type}")
43+
44+
min_x, min_y, max_x, max_y = shapely_obj.bounds
45+
46+
start = [min_x, min_y]
47+
end = [max_x, max_y]
48+
49+
return Rectangle(start=Point(x=start[0], y=start[1]),
50+
end=Point(x=end[0], y=end[1]))
51+
3352
def draw(self,
3453
height: Optional[int] = None,
3554
width: Optional[int] = None,

0 commit comments

Comments
 (0)