Skip to content

Commit

Permalink
reverted removing id from annotation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertvanHouten committed May 12, 2022
1 parent 2286e21 commit 9e916f1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
37 changes: 32 additions & 5 deletions ote_sdk/ote_sdk/entities/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from enum import Enum
from typing import Dict, List, Optional, Set

from bson import ObjectId

from ote_sdk.entities.id import ID
from ote_sdk.entities.label import LabelEntity
from ote_sdk.entities.scored_label import ScoredLabel
Expand All @@ -21,19 +23,44 @@ class Annotation(metaclass=abc.ABCMeta):
"""

# pylint: disable=redefined-builtin;
def __init__(self, shape: ShapeEntity, labels: List[ScoredLabel]):
def __init__(
self, shape: ShapeEntity, labels: List[ScoredLabel], id: Optional[ID] = None
):
self.__id_ = ID(ObjectId()) if id is None else id
self.__shape = shape
self.__labels = labels

def __repr__(self):
return (
f"{self.__class__.__name__}("
f"shape={self.shape}, "
f"labels={self.get_labels(include_empty=True)}"
f"labels={self.get_labels(include_empty=True)}, "
f"id={self.id_})"
)

@property
def shape(self) -> ShapeEntity:
def id_(self):
"""
Returns the id for the annotation
"""
return self.__id_

@id_.setter
def id_(self, value):
self.__id_ = value

@property
def id(self):
"""DEPRECATED"""
return self.__id_

@id.setter
def id(self, value):
"""DEPRECATED"""
self.__id_ = value

@property
def shape(self):
"""
Returns the shape that is in the annotation
"""
Expand Down Expand Up @@ -86,8 +113,8 @@ def set_labels(self, labels: List[ScoredLabel]) -> None:
def __eq__(self, other):
if isinstance(other, Annotation):
return (
self.get_labels(include_empty=True)
== other.get_labels(include_empty=True)
self.id_ == other.id_
and self.get_labels(True) == other.get_labels(True)
and self.shape == other.shape
)
return False
Expand Down
4 changes: 4 additions & 0 deletions ote_sdk/ote_sdk/entities/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
from enum import Enum
from typing import Iterator, List, Optional, Sequence, Union, overload

from bson.objectid import ObjectId

from ote_sdk.entities.annotation import AnnotationSceneEntity, AnnotationSceneKind
from ote_sdk.entities.dataset_item import DatasetItemEntity
from ote_sdk.entities.id import ID
from ote_sdk.entities.label import LabelEntity
from ote_sdk.entities.subset import Subset

Expand Down Expand Up @@ -274,6 +277,7 @@ def with_empty_annotations(

# reset ROI
roi = copy.copy(dataset_item.roi)
roi.id_ = ID(ObjectId())
roi.set_labels([])

new_dataset_item = DatasetItemEntity(
Expand Down
7 changes: 6 additions & 1 deletion ote_sdk/ote_sdk/tests/entities/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def test_annotation_default_property(self):

annotation = self.annotation

assert type(annotation.id_) == ID
assert annotation.id_ is not None
assert str(annotation.shape) == "Rectangle(x=0.5, y=0.0, width=0.5, height=0.5)"
assert annotation.get_labels() == []

Expand All @@ -107,6 +109,9 @@ def test_annotation_setters(self):
annotation = self.annotation
ellipse = Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3)
annotation.shape = ellipse
annotation.id_ = ID(123456789)

assert annotation.id_ == ID(123456789)
assert annotation.shape == ellipse

@pytest.mark.priority_medium
Expand Down Expand Up @@ -139,7 +144,7 @@ def test_annotation_magic_methods(self):

assert (
repr(annotation)
== "Annotation(shape=Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3), labels=[]"
== "Annotation(shape=Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3), labels=[], id=123456789)"
)
assert annotation == other_annotation
assert annotation != third_annotation
Expand Down
12 changes: 12 additions & 0 deletions ote_sdk/ote_sdk/tests/entities/test_dataset_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ def annotations(self) -> List[Annotation]:
detection_annotation = Annotation(
shape=rectangle,
labels=[ScoredLabel(label=labels[0])],
id=ID("detection_annotation_1"),
)
segmentation_annotation = Annotation(
shape=other_rectangle,
labels=[ScoredLabel(label=labels[1])],
id=ID("segmentation_annotation_1"),
)
return [detection_annotation, segmentation_annotation]

Expand Down Expand Up @@ -107,6 +109,7 @@ def roi(self):
modification_date=datetime.datetime(year=2021, month=12, day=9),
),
labels=self.roi_scored_labels(),
id=ID("roi_annotation"),
)
return roi

Expand Down Expand Up @@ -158,6 +161,7 @@ def compare_denormalized_annotations(
expected_annotation = expected_annotations[index]
# Redefining id and modification_date required because of new Annotation objects created after shape
# denormalize
actual_annotation.id_ = expected_annotation.id_
actual_annotation.shape.modification_date = (
expected_annotation.shape.modification_date
)
Expand Down Expand Up @@ -187,10 +191,12 @@ def annotations_to_add(self) -> List[Annotation]:
annotation_to_add = Annotation(
shape=Rectangle(x1=0.1, y1=0.1, x2=0.7, y2=0.8),
labels=[ScoredLabel(label=labels_to_add[0])],
id=ID("added_annotation_1"),
)
other_annotation_to_add = Annotation(
shape=Rectangle(x1=0.2, y1=0.3, x2=0.8, y2=0.9),
labels=[ScoredLabel(label=labels_to_add[1])],
id=ID("added_annotation_2"),
)
return [annotation_to_add, other_annotation_to_add]

Expand Down Expand Up @@ -393,6 +399,7 @@ def test_dataset_item_roi_numpy(self):
rectangle_roi = Annotation(
Rectangle(x1=0.2, y1=0.1, x2=0.8, y2=0.9),
[ScoredLabel(roi_label)],
ID("rectangle_roi"),
)
assert np.array_equal(
dataset_item.roi_numpy(rectangle_roi), media.numpy[1:9, 3:13]
Expand All @@ -401,6 +408,7 @@ def test_dataset_item_roi_numpy(self):
ellipse_roi = Annotation(
Ellipse(x1=0.1, y1=0.0, x2=0.9, y2=0.8),
[ScoredLabel(roi_label)],
ID("ellipse_roi"),
)
assert np.array_equal(
dataset_item.roi_numpy(ellipse_roi), media.numpy[0:8, 2:14]
Expand All @@ -417,6 +425,7 @@ def test_dataset_item_roi_numpy(self):
]
),
labels=[],
id=ID("polygon_roi"),
)
assert np.array_equal(
dataset_item.roi_numpy(polygon_roi), media.numpy[4:8, 5:13]
Expand Down Expand Up @@ -616,6 +625,8 @@ def test_dataset_item_append_annotations(self):
)
dataset_item.append_annotations(annotations_to_add)
# Random id is generated for normalized annotations
normalized_annotations[0].id_ = dataset_item.annotation_scene.annotations[2].id_
normalized_annotations[1].id_ = dataset_item.annotation_scene.annotations[3].id_
assert (
dataset_item.annotation_scene.annotations
== full_box_annotations + normalized_annotations
Expand All @@ -633,6 +644,7 @@ def test_dataset_item_append_annotations(self):
incorrect_shape_annotation = Annotation(
shape=incorrect_polygon,
labels=[ScoredLabel(incorrect_shape_label)],
id=ID("incorrect_shape_annotation"),
)
dataset_item.append_annotations([incorrect_shape_annotation])
assert (
Expand Down
1 change: 1 addition & 0 deletions ote_sdk/ote_sdk/tests/entities/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ def check_empty_annotations_dataset(
assert actual_item.media is expected_item.media
assert actual_item.annotation_scene.annotations == []
assert actual_item.annotation_scene.kind == expected_kind
assert actual_item.roi.id_ != expected_item.roi.id_
assert actual_item.roi.shape is expected_item.roi.shape
assert actual_item.roi.get_labels() == []
assert actual_item.subset is expected_item.subset
Expand Down
2 changes: 2 additions & 0 deletions ote_sdk/ote_sdk/tests/entities/test_result_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def default_result_media_parameters() -> dict:
rectangle_annotation = Annotation(
shape=Rectangle(x1=0.1, y1=0.4, x2=0.4, y2=0.9),
labels=[ScoredLabel(rectangle_label)],
id=ID("rectangle_annotation"),
)
annotation_scene = AnnotationSceneEntity(
annotations=[rectangle_annotation],
Expand All @@ -64,6 +65,7 @@ def optional_result_media_parameters(self) -> dict:
roi = Annotation(
shape=Rectangle(x1=0.3, y1=0.2, x2=0.7, y2=0.6),
labels=[ScoredLabel(roi_label)],
id=ID("roi_annotation"),
)
result_media_label = LabelEntity(
"ResultMedia label",
Expand Down
4 changes: 4 additions & 0 deletions ote_sdk/ote_sdk/tests/utils/test_shape_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ def full_rectangle_annotation(self) -> Annotation:
return Annotation(
shape=Rectangle(x1=0, y1=0, x2=1, y2=1),
labels=self.full_rectangle_scored_labels(),
id=ID("full_rectangle_annotation"),
)

@staticmethod
Expand Down Expand Up @@ -628,6 +629,7 @@ def rectangle_annotation(self) -> Annotation:
return Annotation(
shape=Rectangle(x1=0.1, y1=0.4, x2=0.4, y2=0.9),
labels=self.rectangle_scored_labels(),
id=ID("rectangle_annotation"),
)

@staticmethod
Expand Down Expand Up @@ -664,6 +666,7 @@ def polygon_annotation(self) -> Annotation:
]
),
labels=self.polygon_scored_labels(),
id=ID("polygon_annotation"),
)

@staticmethod
Expand Down Expand Up @@ -692,6 +695,7 @@ def ellipse_annotation(self) -> Annotation:
return Annotation(
shape=Ellipse(x1=0.5, y1=0.0, x2=1.0, y2=0.5),
labels=self.ellipse_scored_labels(),
id=ID("ellipse_annotation"),
)

def annotation_scene(self) -> AnnotationSceneEntity:
Expand Down
3 changes: 3 additions & 0 deletions ote_sdk/ote_sdk/utils/segmentation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

import cv2
import numpy as np
from bson import ObjectId

from ote_sdk.entities.annotation import Annotation
from ote_sdk.entities.dataset_item import DatasetItemEntity
from ote_sdk.entities.id import ID
from ote_sdk.entities.label import LabelEntity
from ote_sdk.entities.scored_label import ScoredLabel
from ote_sdk.entities.shapes.polygon import Point, Polygon
Expand Down Expand Up @@ -252,6 +254,7 @@ def create_annotation_from_segmentation_map(
Annotation(
shape=polygon,
labels=[ScoredLabel(label, probability)],
id=ID(ObjectId()),
)
)
else:
Expand Down

0 comments on commit 9e916f1

Please sign in to comment.