1
- from typing import Any , Dict , List , Optional , Union
1
+ from typing import Any , Dict , List , Optional , Union , Type
2
2
3
3
from pydantic import BaseModel , validator
4
4
5
+ from .classification import LBV1Checklist , LBV1Classifications , LBV1Radio , LBV1Text , LBV1Dropdown
6
+ from .feature import LBV1Feature
5
7
from ...annotation_types .annotation import (ClassificationAnnotation ,
6
8
ObjectAnnotation )
7
9
from ...annotation_types .data import MaskData
8
10
from ...annotation_types .geometry import Line , Mask , Point , Polygon , Rectangle
9
11
from ...annotation_types .ner import TextEntity
10
12
from ...annotation_types .types import Cuid
11
- from .classification import LBV1Checklist , LBV1Classifications , LBV1Radio , LBV1Text , LBV1Dropdown
12
- from .feature import LBV1Feature
13
13
14
14
15
15
class LBV1ObjectBase (LBV1Feature ):
@@ -65,10 +65,10 @@ def from_common(cls, rectangle: Rectangle,
65
65
height = rectangle .end .y - rectangle .start .y ,
66
66
width = rectangle .end .x - rectangle .start .x ,
67
67
),
68
- schema_id = feature_schema_id ,
69
- title = title ,
70
- classifications = classifications ,
71
- ** extra )
68
+ schema_id = feature_schema_id ,
69
+ title = title ,
70
+ classifications = classifications ,
71
+ ** extra )
72
72
73
73
74
74
class LBV1Polygon (LBV1ObjectBase ):
@@ -83,11 +83,12 @@ def from_common(cls, polygon: Polygon,
83
83
feature_schema_id : Cuid , title : str ,
84
84
extra : Dict [str , Any ]) -> "LBV1Polygon" :
85
85
return cls (
86
- polygon = [_Point (x = point .x , y = point .y ) for point in polygon .points ],
86
+ polygon = [_Point (x = point .x , y = point .y ) for point in polygon .points [: - 1 ]], # drop closing point
87
87
classifications = classifications ,
88
88
schema_id = feature_schema_id ,
89
89
title = title ,
90
- ** extra )
90
+ ** extra
91
+ )
91
92
92
93
93
94
class LBV1Point (LBV1ObjectBase ):
@@ -138,7 +139,6 @@ def from_common(cls, mask: Mask,
138
139
classifications : List [ClassificationAnnotation ],
139
140
feature_schema_id : Cuid , title : str ,
140
141
extra : Dict [str , Any ]) -> "LBV1Mask" :
141
-
142
142
if mask .mask .url is None :
143
143
raise ValueError (
144
144
"Mask does not have a url. Use `LabelGenerator.add_url_to_masks`, `LabelList.add_url_to_masks`, or `Label.add_url_to_masks`."
@@ -175,17 +175,12 @@ def from_common(cls, text_entity: TextEntity,
175
175
classifications : List [ClassificationAnnotation ],
176
176
feature_schema_id : Cuid , title : str ,
177
177
extra : Dict [str , Any ]) -> "LBV1TextEntity" :
178
-
179
- return cls (data = {
180
- 'location' : {
181
- 'start' : text_entity .start ,
182
- 'end' : text_entity .end
183
- }
184
- },
185
- classifications = classifications ,
186
- schema_id = feature_schema_id ,
187
- title = title ,
188
- ** extra )
178
+ return cls (
179
+ data = _Location (location = _TextPoint (start = text_entity .start , end = text_entity .end )),
180
+ classifications = classifications ,
181
+ schema_id = feature_schema_id ,
182
+ title = title ,
183
+ ** extra )
189
184
190
185
191
186
class LBV1Objects (BaseModel ):
@@ -222,7 +217,6 @@ def from_common(cls, annotations: List[ObjectAnnotation]) -> "LBV1Objects":
222
217
objects = []
223
218
for annotation in annotations :
224
219
obj = cls .lookup_object (annotation )
225
- subclasses = []
226
220
subclasses = LBV1Classifications .from_common (
227
221
annotation .classifications ).classifications
228
222
@@ -237,9 +231,8 @@ def from_common(cls, annotations: List[ObjectAnnotation]) -> "LBV1Objects":
237
231
238
232
@staticmethod
239
233
def lookup_object (
240
- annotation : ObjectAnnotation
241
- ) -> Union [LBV1Line , LBV1Point , LBV1Polygon , LBV1Rectangle , LBV1Mask ,
242
- LBV1TextEntity ]:
234
+ annotation : ObjectAnnotation
235
+ ) -> Type [Union [LBV1Line , LBV1Point , LBV1Polygon , LBV1Rectangle , LBV1Mask , LBV1TextEntity ]]:
243
236
result = {
244
237
Line : LBV1Line ,
245
238
Point : LBV1Point ,
0 commit comments