Skip to content

Commit f830808

Browse files
committed
updates
1 parent c502da0 commit f830808

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

labelbox/data/serialization/labelbox_v1/objects.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing_extensions import Literal
66

77
from pydantic import BaseModel, validator, Field
8+
import numpy as np
89

910
from .classification import LBV1Checklist, LBV1Classifications, LBV1Radio, LBV1Text, LBV1Dropdown
1011
from .feature import LBV1Feature
@@ -46,14 +47,25 @@ def validate_subclasses(cls, value, field):
4647
return value
4748

4849

49-
class _Coordinates(BaseModel):
50-
"""Union of the possible coordinate lists in tiled imagery exports"""
51-
coordinates: Union[List[float], List[List[float]], List[List[List[float]]]]
50+
class TIPointCoordinate(BaseModel):
51+
coordinates: List[float]
52+
53+
54+
class TILineCoordinate(BaseModel):
55+
coordinates: List[List[float]]
56+
57+
58+
class TIPolygonCoordinate(BaseModel):
59+
coordinates: List[List[List[float]]]
60+
61+
62+
class TIRectangleoordinate(BaseModel):
63+
coordinates: List[List[List[float]]]
5264

5365

5466
class LBV1TIPoint(LBV1ObjectBase):
5567
object_type: Literal['point'] = Field(..., alias='type')
56-
geometry: _Coordinates
68+
geometry: TIPointCoordinate
5769

5870
def to_common(self) -> Point:
5971
lng, lat = self.geometry.coordinates
@@ -62,7 +74,7 @@ def to_common(self) -> Point:
6274

6375
class LBV1TILine(LBV1ObjectBase):
6476
object_type: Literal['polyline'] = Field(..., alias='type')
65-
geometry: _Coordinates
77+
geometry: TILineCoordinate
6678

6779
def to_common(self) -> Line:
6880
return Line(points=[
@@ -72,7 +84,7 @@ def to_common(self) -> Line:
7284

7385
class LBV1TIPolygon(LBV1ObjectBase):
7486
object_type: Literal['polygon'] = Field(..., alias='type')
75-
geometry: _Coordinates
87+
geometry: TIPolygonCoordinate
7688

7789
def to_common(self) -> Polygon:
7890
for coord_list in self.geometry.coordinates:
@@ -82,12 +94,17 @@ def to_common(self) -> Polygon:
8294

8395
class LBV1TIRectangle(LBV1ObjectBase):
8496
object_type: Literal['rectangle'] = Field(..., alias='type')
85-
geometry: _Coordinates
97+
geometry: TIRectangleoordinate
8698

8799
def to_common(self) -> Rectangle:
88-
coord_list = self.geometry.coordinates[0]
89-
start = coord_list[0]
90-
end = coord_list[2]
100+
coord_list = np.array(self.geometry.coordinates[0])
101+
102+
min_x, max_x = np.min(coord_list[:, 0]), np.max(coord_list[:, 0])
103+
min_y, max_y = np.min(coord_list[:, 1]), np.max(coord_list[:, 1])
104+
105+
start = [min_x, min_y]
106+
end = [max_x, max_y]
107+
91108
return Rectangle(start=Point(x=start[0], y=start[1]),
92109
end=Point(x=end[0], y=end[1]))
93110

tests/data/serialization/labelbox_v1/test_tiled_image.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import json
22

33
import pytest
4+
from labelbox.data.annotation_types.geometry.polygon import Polygon
5+
from labelbox.data.annotation_types.geometry.line import Line
6+
from labelbox.data.annotation_types.geometry.point import Point
7+
from labelbox.data.annotation_types.geometry.rectangle import Rectangle
48

59
from labelbox.data.serialization.labelbox_v1.converter import LBV1Converter
10+
from labelbox.schema.bulk_import_request import Bbox
611

712

813
@pytest.mark.parametrize(
@@ -18,5 +23,23 @@ def test_image(file_path):
1823
collection_as_list = collection.as_list()
1924

2025
assert len(collection_as_list) == 2
21-
assert len(collection_as_list[0].annotations) == 6
22-
assert len(collection_as_list[1].annotations) == 8
26+
27+
non_simple_annotations = collection_as_list[0].annotations
28+
assert len(non_simple_annotations) == 6
29+
expected_shapes = [Polygon, Point, Point, Point, Line, Rectangle]
30+
for idx in range(len(non_simple_annotations)):
31+
assert isinstance(non_simple_annotations[idx].value,
32+
expected_shapes[idx])
33+
assert non_simple_annotations[-1].value.start.x == -99.36567524971268
34+
assert non_simple_annotations[-1].value.start.y == 19.34717117508651
35+
assert non_simple_annotations[-1].value.end.x == -99.3649886680726
36+
assert non_simple_annotations[-1].value.end.y == 19.41999425190506
37+
38+
simple_annotations = collection_as_list[1].annotations
39+
assert len(simple_annotations) == 8
40+
expected_shapes = [
41+
Polygon, Point, Point, Point, Point, Point, Line, Rectangle
42+
]
43+
for idx in range(len(simple_annotations)):
44+
assert isinstance(simple_annotations[idx].value,
45+
expected_shapes[idx])

0 commit comments

Comments
 (0)