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
33 changes: 19 additions & 14 deletions src/superannotate/lib/core/data_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from typing import Callable
from typing import Dict
from typing import List
from operator import itemgetter

import lib.core as constances
from lib.core.enums import AnnotationTypes
from lib.core.reporter import Reporter
from superannotate_schemas.schemas.classes import AnnotationClass
from superannotate_schemas.schemas.classes import Attribute
from superannotate_schemas.schemas.classes import AttributeGroup

import lib.core as constances
from lib.core.enums import AnnotationTypes
from lib.core.reporter import Reporter


class BaseDataHandler(metaclass=ABCMeta):
@abstractmethod
Expand Down Expand Up @@ -47,7 +49,7 @@ def get_annotation_class(self, name: str) -> AnnotationClass:

@lru_cache()
def get_attribute_group(
self, annotation_class: AnnotationClass, attr_group_name: str
self, annotation_class: AnnotationClass, attr_group_name: str
) -> AttributeGroup:
for attr_group in annotation_class.attribute_groups:
if attr_group.name == attr_group_name:
Expand Down Expand Up @@ -114,10 +116,10 @@ def handle(self, annotation: dict):

class MissingIDsHandler(BaseAnnotationDateHandler):
def __init__(
self,
annotation_classes: List[AnnotationClass],
templates: List[dict],
reporter: Reporter,
self,
annotation_classes: List[AnnotationClass],
templates: List[dict],
reporter: Reporter,
):
super().__init__(annotation_classes)
self.validate_existing_classes(annotation_classes)
Expand Down Expand Up @@ -187,7 +189,7 @@ def handle(self, annotation: dict):
template["name"]: template["id"] for template in self._templates
}
for annotation_instance in (
i for i in annotation["instances"] if i.get("type", None) == "template"
i for i in annotation["instances"] if i.get("type", None) == "template"
):
annotation_instance["templateId"] = template_name_id_map.get(
annotation_instance.get("templateName", ""), -1
Expand Down Expand Up @@ -237,6 +239,8 @@ def handle(self, annotation: dict):


class VideoFormatHandler(BaseAnnotationDateHandler):
INSTANCE_FIELDS = {"className", "pointLabels", "createdBy", "createdAt", "updatedBy", "updatedAt"}

@staticmethod
def _point_handler(time_stamp):
pass
Expand Down Expand Up @@ -266,7 +270,7 @@ def safe_time(timestamp):
return "0" if str(timestamp) == "0.0" else timestamp

def convert_timestamp(timestamp):
return timestamp / 10**6 if timestamp else "0"
return timestamp / 10 ** 6 if timestamp else "0"

editor_data = {
"instances": [],
Expand Down Expand Up @@ -300,8 +304,9 @@ def convert_timestamp(timestamp):
else:
editor_instance["classId"] = id_generator.send("unknown_class")

if meta.get("pointLabels", None):
editor_instance["pointLabels"] = meta["pointLabels"]
matched_fields = self.INSTANCE_FIELDS & meta.keys()
for matched_field in matched_fields:
editor_instance[matched_field] = meta[matched_field]
active_attributes = set()
for parameter in instance["parameters"]:
start_time = safe_time(convert_timestamp(parameter["start"]))
Expand Down Expand Up @@ -363,10 +368,10 @@ def convert_timestamp(timestamp):
(group_name, attr_name)
)
attributes_to_add = (
existing_attributes_in_current_instance - active_attributes
existing_attributes_in_current_instance - active_attributes
)
attributes_to_delete = (
active_attributes - existing_attributes_in_current_instance
active_attributes - existing_attributes_in_current_instance
)
if attributes_to_add or attributes_to_delete:
editor_instance["timeline"][timestamp][
Expand Down
20 changes: 14 additions & 6 deletions tests/integration/annotations/test_video_annotation_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,27 @@ def test_video_annotation_converter(self):
data = {'instances': [
{
'attributes': [], 'timeline': {
'0': {'active': True, 'points': {'x1': 223.32, 'y1': 78.45, 'x2': 312.31, 'y2': 176.66}},
17.271058: {'points': {'x1': 182.08, 'y1': 33.18, 'x2': 283.45, 'y2': 131.39}},
30.526667: {'active': False, 'points': {'x1': 182.42, 'y1': 97.19, 'x2': 284.11, 'y2': 195.4}}},
'type': 'bbox', 'locked': False, 'classId': -1, 'pointLabels': {'3': 'point label bro'}
'0': {
'active': True, 'points': {'x1': 223.32, 'y1': 78.45, 'x2': 312.31, 'y2': 176.66}},
17.271058: {
'points': {
'x1': 182.08, 'y1': 33.18, 'x2': 283.45, 'y2': 131.39}
},
30.526667: {
'active': False, 'points': {'x1': 182.42, 'y1': 97.19, 'x2': 284.11, 'y2': 195.4}}},
'type': 'bbox', 'locked': False, 'classId': -1, "className": "vid",
"pointLabels": {
"3": "point label bro"
},
},
{
'attributes': [],
'timeline': {29.713736: {'active': True, 'x': 1, 'y': 2}, 30.526667: {'active': False, 'x': 2, 'y': 3}},
'type': 'point', 'locked': False, 'classId': -1
'type': 'point', 'locked': False, 'classId': -1, "className": "vid",
},
{
'attributes': [], 'timeline': {5.528212: {'active': True}, 6.702957: {}, 7.083022: {'active': False}},
'type': 'event', 'locked': False, 'classId': -1
'type': 'event', 'locked': False, 'classId': -1, "className": "vid",
}
],
'tags': ['some tag'], 'name': 'video.mp4',
Expand Down