File tree Expand file tree Collapse file tree 5 files changed +55
-5
lines changed
labelbox/data/annotation_types Expand file tree Collapse file tree 5 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 1515from pydantic import BaseModel , validator
1616from 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
2319from .base_data import BaseData
2420from .raster import RasterData
2521
Original file line number Diff line number Diff line change 44import numpy as np
55import cv2
66from pydantic import validator
7+ from shapely .geometry import LineString as SLineString
78
89from .point import Point
910from .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 ,
Original file line number Diff line number Diff line change 33import geojson
44import numpy as np
55import cv2
6+ from shapely .geometry import Point as SPoint
67
78from .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 ,
Original file line number Diff line number Diff line change 44import geojson
55import numpy as np
66from pydantic import validator
7+ from shapely .geometry import Polygon as SPolygon
78
89from .geometry import Geometry
910from .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 ,
Original file line number Diff line number Diff line change 33import cv2
44import geojson
55import numpy as np
6+ from shapely .geometry import Polygon as SPolygon
67
78from .geometry import Geometry
89from .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 ,
You can’t perform that action at this time.
0 commit comments