Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions darwin/importer/formats/superannotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
make_keypoint,
make_line,
make_polygon,
make_tag,
)
from darwin.importer.formats.superannotate_schemas import (
classes_export,
Expand All @@ -41,12 +42,15 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:
{
"instances": [
{
"classId": 1,
"attributes": [],
"type": "point",
"x": 1,
"y": 0
},
// { ... }
],
"tags": ["a_tag_here"],
"metadata": {
"name": "a_file_name.json"
}
Expand All @@ -60,13 +64,14 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:
- bbox ``Vector`` (not rotated): https://doc.superannotate.com/docs/vector-json#bounding-box-and-rotated-bounding-box
- polygon and polyline ``Vector``s: https://doc.superannotate.com/docs/vector-json#polyline-and-polygon

We also support attributes and tags.

Each file must also have in the same folder a ``classes.json`` file with information about
the classes. This file must have a structure simillar to:

.. code-block:: javascript
[
{"name": "a_name_here", "id": 1},
{"name": "a_name_here", "id": 1, "attribute_groups": []},
// { ... }
]

Expand Down Expand Up @@ -108,20 +113,24 @@ def parse_path(path: Path) -> Optional[AnnotationFile]:

instances: List[Dict[str, Any]] = data.get("instances")
metadata: Dict[str, Any] = data.get("metadata")
tags: List[str] = data.get("tags")

return _convert(instances, path, classes, metadata)
return _convert(instances, path, classes, metadata, tags)


def _convert(
instances: List[Dict[str, Any]],
annotation_file_path: Path,
superannotate_classes: List[Dict[str, Any]],
metadata: Dict[str, Any],
tags: List[str],
) -> AnnotationFile:
filename: str = str(metadata.get("name"))
conver_to_darwin_object = partial(_convert_instance, superannotate_classes=superannotate_classes)

convert_with_classes = partial(_convert_objects, superannotate_classes=superannotate_classes)
annotations: List[Annotation] = _map_to_list(convert_with_classes, instances)
filename: str = str(metadata.get("name"))
darwin_tags: List[Annotation] = _map_to_list(_convert_tag, tags)
darwin_objects: List[Annotation] = _map_to_list(conver_to_darwin_object, instances)
annotations: List[Annotation] = darwin_objects + darwin_tags
classes: Set[AnnotationClass] = _map_to_set(_get_class, annotations)

return AnnotationFile(
Expand All @@ -133,7 +142,7 @@ def _convert(
)


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

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


def _convert_tag(tag: str) -> Annotation:
return make_tag(tag)
Comment on lines +169 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems to basically act as proxy of make_tag, I'd say, let's use make_tag right away 🙂



def _to_keypoint_annotation(point: Dict[str, Any], classes: List[Dict[str, Any]]) -> Annotation:
x: float = cast(float, point.get("x"))
y: float = cast(float, point.get("y"))
Expand Down
36 changes: 33 additions & 3 deletions darwin/importer/formats/superannotate_schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##################################
# import_file.json #
##################################
Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this comment convention elsewhere?


attributes = {
"type": "array",
"items": {
Expand Down Expand Up @@ -208,18 +212,44 @@

superannotate_export = {
"type": "object",
"required": ["instances", "metadata", "tags"],
"properties": {
"instances": {"type": "array", "items": {"oneOf": [point, ellipse, cuboid, polygon, bbox, polyline]},},
"metadata": {"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}},
"tags": {"type": "array", "items": {"type": "string"}},
},
}

##################################
# classes.json #
##################################

attribute_groups = {
"type": "array",
"items": {
"type": "object",
"required": ["id", "name", "attributes"],
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"attributes": {
"type": "array",
"itmes": {
"type": "object",
"required": ["id", "name"],
"properties": {"id": {"type": "integer"}, "name": {"type": "string"}},
},
},
},
},
"required": ["instances", "metadata"],
}

classes_export = {
"type": "array",
"items": {
"type": "object",
"required": ["name", "id"],
"properties": {"name": {"type": "string"}, "id": {"type": "integer"}},
"required": ["name", "id", "attribute_groups"],
"properties": {"name": {"type": "string"}, "id": {"type": "integer"}, "attribute_groups": attribute_groups},
},
}

2 changes: 1 addition & 1 deletion tests/darwin/importer/formats/import_labelbox_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Any, Callable, List, Optional, cast
from typing import List, Optional, cast

import pytest
from darwin.datatypes import (
Expand Down
Loading