Skip to content

Commit 9d60a2f

Browse files
authored
Added support for superannotate tags (#320)
* added support for superannotate tags * documentation improvements
1 parent 18a3d07 commit 9d60a2f

File tree

4 files changed

+167
-96
lines changed

4 files changed

+167
-96
lines changed

darwin/importer/formats/superannotate.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
make_keypoint,
2121
make_line,
2222
make_polygon,
23+
make_tag,
2324
)
2425
from darwin.importer.formats.superannotate_schemas import (
2526
classes_export,
@@ -41,12 +42,15 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:
4142
{
4243
"instances": [
4344
{
45+
"classId": 1,
46+
"attributes": [],
4447
"type": "point",
4548
"x": 1,
4649
"y": 0
4750
},
4851
// { ... }
4952
],
53+
"tags": ["a_tag_here"],
5054
"metadata": {
5155
"name": "a_file_name.json"
5256
}
@@ -60,13 +64,14 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:
6064
- bbox ``Vector`` (not rotated): https://doc.superannotate.com/docs/vector-json#bounding-box-and-rotated-bounding-box
6165
- polygon and polyline ``Vector``s: https://doc.superannotate.com/docs/vector-json#polyline-and-polygon
6266
67+
We also support attributes and tags.
6368
6469
Each file must also have in the same folder a ``classes.json`` file with information about
6570
the classes. This file must have a structure simillar to:
6671
6772
.. code-block:: javascript
6873
[
69-
{"name": "a_name_here", "id": 1},
74+
{"name": "a_name_here", "id": 1, "attribute_groups": []},
7075
// { ... }
7176
]
7277
@@ -108,20 +113,24 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:
108113

109114
instances: List[Dict[str, Any]] = data.get("instances")
110115
metadata: Dict[str, Any] = data.get("metadata")
116+
tags: List[str] = data.get("tags")
111117

112-
return _convert(instances, path, classes, metadata)
118+
return _convert(instances, path, classes, metadata, tags)
113119

114120

115121
def _convert(
116122
instances: List[Dict[str, Any]],
117123
annotation_file_path: Path,
118124
superannotate_classes: List[Dict[str, Any]],
119125
metadata: Dict[str, Any],
126+
tags: List[str],
120127
) -> AnnotationFile:
121-
filename: str = str(metadata.get("name"))
128+
conver_to_darwin_object = partial(_convert_instance, superannotate_classes=superannotate_classes)
122129

123-
convert_with_classes = partial(_convert_objects, superannotate_classes=superannotate_classes)
124-
annotations: List[Annotation] = _map_to_list(convert_with_classes, instances)
130+
filename: str = str(metadata.get("name"))
131+
darwin_tags: List[Annotation] = _map_to_list(_convert_tag, tags)
132+
darwin_objects: List[Annotation] = _map_to_list(conver_to_darwin_object, instances)
133+
annotations: List[Annotation] = darwin_objects + darwin_tags
125134
classes: Set[AnnotationClass] = _map_to_set(_get_class, annotations)
126135

127136
return AnnotationFile(
@@ -133,7 +142,7 @@ def _convert(
133142
)
134143

135144

136-
def _convert_objects(obj: Dict[str, Any], superannotate_classes: List[Dict[str, Any]]) -> Annotation:
145+
def _convert_instance(obj: Dict[str, Any], superannotate_classes: List[Dict[str, Any]]) -> Annotation:
137146
type: str = str(obj.get("type"))
138147

139148
if type == "point":
@@ -157,6 +166,10 @@ def _convert_objects(obj: Dict[str, Any], superannotate_classes: List[Dict[str,
157166
raise ValueError(f"Unknown label object {obj}")
158167

159168

169+
def _convert_tag(tag: str) -> Annotation:
170+
return make_tag(tag)
171+
172+
160173
def _to_keypoint_annotation(point: Dict[str, Any], classes: List[Dict[str, Any]]) -> Annotation:
161174
x: float = cast(float, point.get("x"))
162175
y: float = cast(float, point.get("y"))

darwin/importer/formats/superannotate_schemas.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
##################################
2+
# import_file.json #
3+
##################################
4+
15
attributes = {
26
"type": "array",
37
"items": {
@@ -208,18 +212,44 @@
208212

209213
superannotate_export = {
210214
"type": "object",
215+
"required": ["instances", "metadata", "tags"],
211216
"properties": {
212217
"instances": {"type": "array", "items": {"oneOf": [point, ellipse, cuboid, polygon, bbox, polyline]},},
213218
"metadata": {"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}},
219+
"tags": {"type": "array", "items": {"type": "string"}},
220+
},
221+
}
222+
223+
##################################
224+
# classes.json #
225+
##################################
226+
227+
attribute_groups = {
228+
"type": "array",
229+
"items": {
230+
"type": "object",
231+
"required": ["id", "name", "attributes"],
232+
"properties": {
233+
"id": {"type": "integer"},
234+
"name": {"type": "string"},
235+
"attributes": {
236+
"type": "array",
237+
"itmes": {
238+
"type": "object",
239+
"required": ["id", "name"],
240+
"properties": {"id": {"type": "integer"}, "name": {"type": "string"}},
241+
},
242+
},
243+
},
214244
},
215-
"required": ["instances", "metadata"],
216245
}
217246

218247
classes_export = {
219248
"type": "array",
220249
"items": {
221250
"type": "object",
222-
"required": ["name", "id"],
223-
"properties": {"name": {"type": "string"}, "id": {"type": "integer"}},
251+
"required": ["name", "id", "attribute_groups"],
252+
"properties": {"name": {"type": "string"}, "id": {"type": "integer"}, "attribute_groups": attribute_groups},
224253
},
225254
}
255+

tests/darwin/importer/formats/import_labelbox_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import Any, Callable, List, Optional, cast
2+
from typing import List, Optional, cast
33

44
import pytest
55
from darwin.datatypes import (

0 commit comments

Comments
 (0)