Skip to content

Commit

Permalink
[PY-618] Add properties to existing Annotation Objects (#744)
Browse files Browse the repository at this point in the history
* base annotation darwin

* fixes for annotations

* base str vs url

* tests for base annotation loading

* cleanup

* cleanup 2: electric boogaloo

* test bounding box auto-calculate

* typing

* cleanup

* property addition

* property metadata parsing

* cleanup validator

* test for properties base example

* linting

* Changes for list/get endpoint

* linting

* add properties to existing Annotation Objects

* update _parse_properties, returns None

* update utils - update SelectedProperty import

---------

Co-authored-by: Nathan Perkins <[email protected]>
Co-authored-by: Nathan Perkins <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent a8d24aa commit 72080cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions darwin/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
except ImportError:
NDArray = Any # type:ignore

from darwin.future.data_objects.properties import SelectedProperty
from darwin.path_utils import construct_full_path, is_properties_enabled, parse_metadata

# Utility types
Expand Down Expand Up @@ -215,6 +216,9 @@ class Annotation:
# The darwin ID of this annotation.
id: Optional[str] = None

# Properties of this annotation.
properties: Optional[list[SelectedProperty]] = None

def get_sub(self, annotation_type: str) -> Optional[SubAnnotation]:
"""
Returns the first SubAnnotation that matches the given type.
Expand Down Expand Up @@ -269,6 +273,9 @@ class VideoAnnotation:
# The darwin ID of this annotation.
id: Optional[str] = None

# Properties of this annotation.
properties: Optional[list[SelectedProperty]] = None

def get_data(
self,
only_keyframes: bool = True,
Expand Down Expand Up @@ -1272,6 +1279,7 @@ def make_video_annotation(
segments: List[Segment],
interpolated: bool,
slot_names: List[str],
properties: Optional[list[SelectedProperty]] = None,
) -> VideoAnnotation:
"""
Creates and returns a ``VideoAnnotation``.
Expand Down Expand Up @@ -1308,6 +1316,7 @@ def make_video_annotation(
segments,
interpolated,
slot_names=slot_names or [],
properties=properties,
)


Expand Down
20 changes: 20 additions & 0 deletions darwin/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
UnrecognizableFileEncoding,
UnsupportedFileType,
)
from darwin.future.data_objects.properties import SelectedProperty
from darwin.version import __version__

if TYPE_CHECKING:
Expand Down Expand Up @@ -839,6 +840,9 @@ def _parse_darwin_annotation(annotation: Dict[str, Any]) -> Optional[dt.Annotati
if annotation.get("reviewers") is not None:
main_annotation.reviewers = _parse_annotators(annotation["reviewers"])

if "properties" in annotation:
main_annotation.properties = _parse_properties(annotation["properties"])

return main_annotation


Expand All @@ -861,6 +865,7 @@ def _parse_darwin_video_annotation(annotation: dict) -> Optional[dt.VideoAnnotat
annotation.get("ranges", annotation.get("segments", [])),
annotation.get("interpolated", False),
slot_names=parse_slot_names(annotation),
properties=_parse_properties(annotation.get("properties", [])),
)

if "id" in annotation:
Expand Down Expand Up @@ -941,6 +946,21 @@ def _parse_annotators(annotators: List[Dict[str, Any]]) -> List[dt.AnnotationAut
return [dt.AnnotationAuthor(annotator["full_name"], annotator["email"]) for annotator in annotators]


def _parse_properties(properties: List[Dict[str, Any]]) -> Optional[List[SelectedProperty]]:
selected_properties = []
for property in properties:
selected_properties.append(
SelectedProperty(
frame_index=property.get("frame_index", None),
name=property.get("name", None),
type=property.get("type", None),
value=property.get("value", None),
)
)

return selected_properties or None


def split_video_annotation(annotation: dt.AnnotationFile) -> List[dt.AnnotationFile]:
"""
Splits the given video ``AnnotationFile`` into several video ``AnnotationFile``s, one for each
Expand Down

0 comments on commit 72080cb

Please sign in to comment.