Skip to content

Commit 8a670fd

Browse files
authored
Merge pull request #369 from Labelbox/geo_convert
initial work into converters
2 parents 87a11eb + f830808 commit 8a670fd

File tree

6 files changed

+352
-6
lines changed

6 files changed

+352
-6
lines changed

labelbox/data/serialization/labelbox_v1/label.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from labelbox.data.annotation_types.data.tiled_image import TiledImageData
12
from labelbox.utils import camel_case
23
from typing import List, Optional, Union
34

@@ -171,8 +172,9 @@ def from_common(cls, label: Label):
171172
external_id=label.data.external_id,
172173
**label.extra)
173174

174-
def _data_row_to_common(self) -> Union[ImageData, TextData, VideoData]:
175-
# Use data row information to construct the appropriate annotatin type
175+
def _data_row_to_common(
176+
self) -> Union[ImageData, TextData, VideoData, TiledImageData]:
177+
# Use data row information to construct the appropriate annotation type
176178
data_row_info = {
177179
'url' if self._is_url() else 'text': self.row_data,
178180
'external_id': self.external_id,
@@ -228,7 +230,8 @@ def _row_contains(self, substrs):
228230
return any([substr in self.row_data for substr in substrs])
229231

230232
def _is_url(self):
231-
return self.row_data.startswith(("http://", "https://"))
233+
return self.row_data.startswith(
234+
("http://", "https://")) or "tileLayerUrl" in self.row_data
232235

233236
class Config:
234237
allow_population_by_field_name = True

labelbox/data/serialization/labelbox_v1/objects.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from typing import Any, Dict, List, Optional, Union, Type
2+
try:
3+
from typing import Literal
4+
except:
5+
from typing_extensions import Literal
26

3-
from pydantic import BaseModel, validator
7+
from pydantic import BaseModel, validator, Field
8+
import numpy as np
49

510
from .classification import LBV1Checklist, LBV1Classifications, LBV1Radio, LBV1Text, LBV1Dropdown
611
from .feature import LBV1Feature
@@ -42,6 +47,68 @@ def validate_subclasses(cls, value, field):
4247
return value
4348

4449

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]]]
64+
65+
66+
class LBV1TIPoint(LBV1ObjectBase):
67+
object_type: Literal['point'] = Field(..., alias='type')
68+
geometry: TIPointCoordinate
69+
70+
def to_common(self) -> Point:
71+
lng, lat = self.geometry.coordinates
72+
return Point(x=lng, y=lat)
73+
74+
75+
class LBV1TILine(LBV1ObjectBase):
76+
object_type: Literal['polyline'] = Field(..., alias='type')
77+
geometry: TILineCoordinate
78+
79+
def to_common(self) -> Line:
80+
return Line(points=[
81+
Point(x=coord[0], y=coord[1]) for coord in self.geometry.coordinates
82+
])
83+
84+
85+
class LBV1TIPolygon(LBV1ObjectBase):
86+
object_type: Literal['polygon'] = Field(..., alias='type')
87+
geometry: TIPolygonCoordinate
88+
89+
def to_common(self) -> Polygon:
90+
for coord_list in self.geometry.coordinates:
91+
return Polygon(
92+
points=[Point(x=coord[0], y=coord[1]) for coord in coord_list])
93+
94+
95+
class LBV1TIRectangle(LBV1ObjectBase):
96+
object_type: Literal['rectangle'] = Field(..., alias='type')
97+
geometry: TIRectangleoordinate
98+
99+
def to_common(self) -> Rectangle:
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+
108+
return Rectangle(start=Point(x=start[0], y=start[1]),
109+
end=Point(x=end[0], y=end[1]))
110+
111+
45112
class _Point(BaseModel):
46113
x: float
47114
y: float
@@ -194,7 +261,8 @@ def from_common(cls, text_entity: TextEntity,
194261

195262
class LBV1Objects(BaseModel):
196263
objects: List[Union[LBV1Line, LBV1Point, LBV1Polygon, LBV1Rectangle,
197-
LBV1TextEntity, LBV1Mask]]
264+
LBV1TextEntity, LBV1Mask, LBV1TIPoint, LBV1TILine,
265+
LBV1TIPolygon, LBV1TIRectangle]]
198266

199267
def to_common(self) -> List[ObjectAnnotation]:
200268
objects = [

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ typeguard
1414
imagesize
1515
pyproj
1616
pygeotile
17+
typing-extensions

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
extras_require={
3030
'data': [
3131
"shapely", "geojson", "numpy", "rasterio", "PILLOW",
32-
"opencv-python", "typeguard", "imagesize", "pyproj", "pygeotile"
32+
"opencv-python", "typeguard", "imagesize", "pyproj", "pygeotile",
33+
"typing-extensions"
3334
],
3435
},
3536
classifiers=[
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
[{
2+
"ID": "ckwdp4o2t8fjq108j6c8t9507",
3+
"DataRow ID": "ckwdowhd91rp80zqh2ev9cx9w",
4+
"Labeled Data": "{\n \"tileLayerUrl\": \"https://s3-us-east-2.amazonaws.com/lb-ron/CACI/ron_mctiles/{z}/{x}/{y}.png\",\n \"bounds\": [\n [19.3469381, -99.3922238],\n [19.42443609, -99.22896466]\n ],\n \"minZoom\": 12,\n \"maxZoom\": 21,\n \"tileSize\": 10000,\n \"epsg\": \"EPSG4326\",\n \"alternativeLayers\": [{\n \"tileLayerUrl\": \"https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v11/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw\",\n \"name\": \"Satellite\"\n },\n {\n \"tileLayerUrl\": \"https://api.mapbox.com/styles/v1/mapbox/navigation-guidance-night-v4/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw\",\n \"name\": \"Guidance\"\n }\n ]\n}",
5+
"Label": {
6+
"objects": [{
7+
"featureId": "ckwdp4zzf00003g6bmj3fnk07",
8+
"schemaId": "ckw3oiu476k780zbpdxqbawbg",
9+
"color": "#ff0000",
10+
"title": "polygon",
11+
"value": "polygon",
12+
"type": "polygon",
13+
"geometry": {
14+
"coordinates": [
15+
[
16+
[-99.2032814025879, 19.410744883371013],
17+
[-99.18886184692384, 19.41123059525866],
18+
[-99.19281005859376, 19.40264947166554],
19+
[-99.2087745666504, 19.397953948267737],
20+
[-99.21255111694336, 19.398439698351616],
21+
[-99.2032814025879, 19.410744883371013]
22+
]
23+
]
24+
}
25+
}, {
26+
"featureId": "ckwdp52w900023g6bkq3p5c9a",
27+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
28+
"color": "#FF34FF",
29+
"title": "point",
30+
"value": "point",
31+
"type": "point",
32+
"geometry": {
33+
"coordinates": [-99.16156768798828, 19.405725775580528]
34+
}
35+
}, {
36+
"featureId": "ckwdp53mx00043g6bxvgz1odn",
37+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
38+
"color": "#FF34FF",
39+
"title": "point",
40+
"value": "point",
41+
"type": "point",
42+
"geometry": {
43+
"coordinates": [-99.19692993164064, 19.378036946667645]
44+
}
45+
}, {
46+
"featureId": "ckwdp547j00063g6bn19303pv",
47+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
48+
"color": "#FF34FF",
49+
"title": "point",
50+
"value": "point",
51+
"type": "point",
52+
"geometry": {
53+
"coordinates": [-99.16019439697268, 19.37447429931405]
54+
}
55+
}, {
56+
"featureId": "ckwdp54yt00073g6bgawsm0dk",
57+
"schemaId": "ckwdnchwp843o108j6eof4qdi",
58+
"color": "#FF4A46",
59+
"title": "poly line",
60+
"value": "poly_line",
61+
"type": "polyline",
62+
"geometry": {
63+
"coordinates": [
64+
[-99.2161560058594, 19.358765334468117],
65+
[-99.19795989990236, 19.36216637277295],
66+
[-99.18560028076172, 19.356497936190603],
67+
[-99.17324066162112, 19.360222930999285]
68+
]
69+
}
70+
}, {
71+
"featureId": "ckx841y2700013g69pboavi3s",
72+
"schemaId": "ckwdnchwp843s108j5w9i491y",
73+
"color": "#006FA6",
74+
"title": "bbox",
75+
"value": "bbox",
76+
"type": "rectangle",
77+
"geometry": {
78+
"coordinates": [
79+
[
80+
[-99.36567524971268, 19.34717117508651],
81+
[-99.36567524971268, 19.41999425190506],
82+
[-99.3649886680726, 19.41999425190506],
83+
[-99.3649886680726, 19.34717117508651],
84+
[-99.36567524971268, 19.34717117508651]
85+
]
86+
]
87+
}
88+
}],
89+
"classifications": [],
90+
"relationships": []
91+
},
92+
"Created By": "jtso@labelbox.com",
93+
"Project Name": "tms_mal_project",
94+
"Created At": "2021-11-24T15:43:41.000Z",
95+
"Updated At": "2021-12-15T22:34:02.363Z",
96+
"Seconds to Label": 910.657,
97+
"External ID": null,
98+
"Agreement": -1,
99+
"Benchmark Agreement": -1,
100+
"Benchmark ID": null,
101+
"Dataset Name": "mx_large_tiled.json",
102+
"Reviews": [],
103+
"View Label": "https://editor.labelbox.com?project=ckw3oipbx9iee0zc321ut9aua&label=ckwdp4o2t8fjq108j6c8t9507",
104+
"Has Open Issues": 0,
105+
"Skipped": false
106+
},
107+
{
108+
"ID": "ckwmlfgyobl6r0z9p0jhb3235",
109+
"DataRow ID": "ckwb4r0fk005c0zqo5le521ih",
110+
"Labeled Data": "\n {\n \"tileLayerUrl\": \"https://labelbox.s3-us-west-2.amazonaws.com/pathology/{z}/{x}/{y}.png\",\n \"minZoom\": 1,\n \"maxZoom\": 8.5,\n \"bounds\": [\n [0, 0],\n [135, 128]\n ],\n \"epsg\": \"Simple\",\n \"version\": 2,\n \"tileSize\": 128\n }",
111+
"Label": {
112+
"objects": [{
113+
"featureId": "ckx83js8l00033g691hz1zs4g",
114+
"schemaId": "ckw3oiu476k780zbpdxqbawbg",
115+
"color": "#ff0000",
116+
"title": "polygon",
117+
"value": "polygon",
118+
"polygon": [{
119+
"x": 52.49074938173845,
120+
"y": 24.209072254552296
121+
}, {
122+
"x": 45.99135397338951,
123+
"y": 33.20339437483625
124+
}, {
125+
"x": 52.99070287468838,
126+
"y": 38.94976684057322
127+
}, {
128+
"x": 58.24021455066253,
129+
"y": 30.455129282527267
130+
}]
131+
}, {
132+
"featureId": "ckwmlfm5r00023g6bvigbvtyh",
133+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
134+
"color": "#FF34FF",
135+
"title": "point",
136+
"value": "point",
137+
"point": {
138+
"x": 0,
139+
"y": 128
140+
}
141+
}, {
142+
"featureId": "ckwmlg1oi00053g6b9peptx9l",
143+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
144+
"color": "#FF34FF",
145+
"title": "point",
146+
"value": "point",
147+
"point": {
148+
"x": 22.352061642639708,
149+
"y": 128
150+
}
151+
}, {
152+
"featureId": "ckwnmjc6600013g6brrg60h3k",
153+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
154+
"color": "#FF34FF",
155+
"title": "point",
156+
"value": "point",
157+
"point": {
158+
"x": 5.921614630706372,
159+
"y": 6.468767056927199
160+
}
161+
}, {
162+
"featureId": "ckwnmjeul00033g6b2b7rezcz",
163+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
164+
"color": "#FF34FF",
165+
"title": "point",
166+
"value": "point",
167+
"point": {
168+
"x": 132.24738299361238,
169+
"y": 64.15800846258857
170+
}
171+
}, {
172+
"featureId": "ckwnmjfu800053g6b5tea82ip",
173+
"schemaId": "ckwdnchwp843m108j4o6f7zvg",
174+
"color": "#FF34FF",
175+
"title": "point",
176+
"value": "point",
177+
"point": {
178+
"x": 135,
179+
"y": 89.80450287438632
180+
}
181+
}, {
182+
"featureId": "ckx83jvyd00063g69wndhctep",
183+
"schemaId": "ckwdnchwp843o108j6eof4qdi",
184+
"color": "#FF4A46",
185+
"title": "poly line",
186+
"value": "poly_line",
187+
"line": [{
188+
"x": 23.743423537118105,
189+
"y": 49.692984928690166
190+
}, {
191+
"x": 36.742214353816,
192+
"y": 49.19330036645216
193+
}, {
194+
"x": 42.49167952274007,
195+
"y": 58.18762248673612
196+
}]
197+
}, {
198+
"featureId": "ckx83jplk00013g69d2mjchbg",
199+
"schemaId": "ckwdnchwp843s108j5w9i491y",
200+
"color": "#006FA6",
201+
"title": "bbox",
202+
"value": "bbox",
203+
"bbox": {
204+
"top": 31.454498407003257,
205+
"left": 31.24272593136689,
206+
"height": 6.995583871331959,
207+
"width": 8.499209380148628
208+
}
209+
}],
210+
"classifications": [],
211+
"relationships": []
212+
},
213+
"Created By": "jtso@labelbox.com",
214+
"Project Name": "tms_mal_project",
215+
"Created At": "2021-11-30T21:17:42.000Z",
216+
"Updated At": "2021-12-15T22:19:57.398Z",
217+
"Seconds to Label": 421.39300000000003,
218+
"External ID": null,
219+
"Agreement": -1,
220+
"Benchmark Agreement": -1,
221+
"Benchmark ID": null,
222+
"Dataset Name": "simple tms",
223+
"Reviews": [],
224+
"View Label": "https://editor.labelbox.com?project=ckw3oipbx9iee0zc321ut9aua&label=ckwmlfgyobl6r0z9p0jhb3235",
225+
"Has Open Issues": 0,
226+
"Skipped": false
227+
}
228+
]

0 commit comments

Comments
 (0)