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 15
15
from pydantic import BaseModel , validator
16
16
from pydantic .class_validators import root_validator
17
17
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
23
19
from .base_data import BaseData
24
20
from .raster import RasterData
25
21
Original file line number Diff line number Diff line change 4
4
import numpy as np
5
5
import cv2
6
6
from pydantic import validator
7
+ from shapely .geometry import LineString as SLineString
7
8
8
9
from .point import Point
9
10
from .geometry import Geometry
@@ -25,6 +26,17 @@ def geometry(self) -> geojson.MultiLineString:
25
26
return geojson .MultiLineString (
26
27
[[[point .x , point .y ] for point in self .points ]])
27
28
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
+
28
40
def draw (self ,
29
41
height : Optional [int ] = None ,
30
42
width : Optional [int ] = None ,
Original file line number Diff line number Diff line change 3
3
import geojson
4
4
import numpy as np
5
5
import cv2
6
+ from shapely .geometry import Point as SPoint
6
7
7
8
from .geometry import Geometry
8
9
@@ -24,6 +25,16 @@ class Point(Geometry):
24
25
def geometry (self ) -> geojson .Point :
25
26
return geojson .Point ((self .x , self .y ))
26
27
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
+
27
38
def draw (self ,
28
39
height : Optional [int ] = None ,
29
40
width : Optional [int ] = None ,
Original file line number Diff line number Diff line change 4
4
import geojson
5
5
import numpy as np
6
6
from pydantic import validator
7
+ from shapely .geometry import Polygon as SPolygon
7
8
8
9
from .geometry import Geometry
9
10
from .point import Point
@@ -30,6 +31,17 @@ def geometry(self) -> geojson.Polygon:
30
31
self .points .append (self .points [0 ])
31
32
return geojson .Polygon ([[(point .x , point .y ) for point in self .points ]])
32
33
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
+
33
45
def draw (self ,
34
46
height : Optional [int ] = None ,
35
47
width : Optional [int ] = None ,
Original file line number Diff line number Diff line change 3
3
import cv2
4
4
import geojson
5
5
import numpy as np
6
+ from shapely .geometry import Polygon as SPolygon
6
7
7
8
from .geometry import Geometry
8
9
from .point import Point
@@ -30,6 +31,24 @@ def geometry(self) -> geojson.geometry.Geometry:
30
31
[self .start .x , self .start .y ],
31
32
]])
32
33
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
+
33
52
def draw (self ,
34
53
height : Optional [int ] = None ,
35
54
width : Optional [int ] = None ,
You can’t perform that action at this time.
0 commit comments