Skip to content

Commit 9e916f1

Browse files
reverted removing id from annotation.py
1 parent 2286e21 commit 9e916f1

8 files changed

+64
-6
lines changed

ote_sdk/ote_sdk/entities/annotation.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from enum import Enum
99
from typing import Dict, List, Optional, Set
1010

11+
from bson import ObjectId
12+
1113
from ote_sdk.entities.id import ID
1214
from ote_sdk.entities.label import LabelEntity
1315
from ote_sdk.entities.scored_label import ScoredLabel
@@ -21,19 +23,44 @@ class Annotation(metaclass=abc.ABCMeta):
2123
"""
2224

2325
# pylint: disable=redefined-builtin;
24-
def __init__(self, shape: ShapeEntity, labels: List[ScoredLabel]):
26+
def __init__(
27+
self, shape: ShapeEntity, labels: List[ScoredLabel], id: Optional[ID] = None
28+
):
29+
self.__id_ = ID(ObjectId()) if id is None else id
2530
self.__shape = shape
2631
self.__labels = labels
2732

2833
def __repr__(self):
2934
return (
3035
f"{self.__class__.__name__}("
3136
f"shape={self.shape}, "
32-
f"labels={self.get_labels(include_empty=True)}"
37+
f"labels={self.get_labels(include_empty=True)}, "
38+
f"id={self.id_})"
3339
)
3440

3541
@property
36-
def shape(self) -> ShapeEntity:
42+
def id_(self):
43+
"""
44+
Returns the id for the annotation
45+
"""
46+
return self.__id_
47+
48+
@id_.setter
49+
def id_(self, value):
50+
self.__id_ = value
51+
52+
@property
53+
def id(self):
54+
"""DEPRECATED"""
55+
return self.__id_
56+
57+
@id.setter
58+
def id(self, value):
59+
"""DEPRECATED"""
60+
self.__id_ = value
61+
62+
@property
63+
def shape(self):
3764
"""
3865
Returns the shape that is in the annotation
3966
"""
@@ -86,8 +113,8 @@ def set_labels(self, labels: List[ScoredLabel]) -> None:
86113
def __eq__(self, other):
87114
if isinstance(other, Annotation):
88115
return (
89-
self.get_labels(include_empty=True)
90-
== other.get_labels(include_empty=True)
116+
self.id_ == other.id_
117+
and self.get_labels(True) == other.get_labels(True)
91118
and self.shape == other.shape
92119
)
93120
return False

ote_sdk/ote_sdk/entities/datasets.py

+4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
from enum import Enum
1313
from typing import Iterator, List, Optional, Sequence, Union, overload
1414

15+
from bson.objectid import ObjectId
16+
1517
from ote_sdk.entities.annotation import AnnotationSceneEntity, AnnotationSceneKind
1618
from ote_sdk.entities.dataset_item import DatasetItemEntity
19+
from ote_sdk.entities.id import ID
1720
from ote_sdk.entities.label import LabelEntity
1821
from ote_sdk.entities.subset import Subset
1922

@@ -274,6 +277,7 @@ def with_empty_annotations(
274277

275278
# reset ROI
276279
roi = copy.copy(dataset_item.roi)
280+
roi.id_ = ID(ObjectId())
277281
roi.set_labels([])
278282

279283
new_dataset_item = DatasetItemEntity(

ote_sdk/ote_sdk/tests/entities/test_annotation.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def test_annotation_default_property(self):
8181

8282
annotation = self.annotation
8383

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

@@ -107,6 +109,9 @@ def test_annotation_setters(self):
107109
annotation = self.annotation
108110
ellipse = Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3)
109111
annotation.shape = ellipse
112+
annotation.id_ = ID(123456789)
113+
114+
assert annotation.id_ == ID(123456789)
110115
assert annotation.shape == ellipse
111116

112117
@pytest.mark.priority_medium
@@ -139,7 +144,7 @@ def test_annotation_magic_methods(self):
139144

140145
assert (
141146
repr(annotation)
142-
== "Annotation(shape=Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3), labels=[]"
147+
== "Annotation(shape=Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3), labels=[], id=123456789)"
143148
)
144149
assert annotation == other_annotation
145150
assert annotation != third_annotation

ote_sdk/ote_sdk/tests/entities/test_dataset_item.py

+12
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ def annotations(self) -> List[Annotation]:
6666
detection_annotation = Annotation(
6767
shape=rectangle,
6868
labels=[ScoredLabel(label=labels[0])],
69+
id=ID("detection_annotation_1"),
6970
)
7071
segmentation_annotation = Annotation(
7172
shape=other_rectangle,
7273
labels=[ScoredLabel(label=labels[1])],
74+
id=ID("segmentation_annotation_1"),
7375
)
7476
return [detection_annotation, segmentation_annotation]
7577

@@ -107,6 +109,7 @@ def roi(self):
107109
modification_date=datetime.datetime(year=2021, month=12, day=9),
108110
),
109111
labels=self.roi_scored_labels(),
112+
id=ID("roi_annotation"),
110113
)
111114
return roi
112115

@@ -158,6 +161,7 @@ def compare_denormalized_annotations(
158161
expected_annotation = expected_annotations[index]
159162
# Redefining id and modification_date required because of new Annotation objects created after shape
160163
# denormalize
164+
actual_annotation.id_ = expected_annotation.id_
161165
actual_annotation.shape.modification_date = (
162166
expected_annotation.shape.modification_date
163167
)
@@ -187,10 +191,12 @@ def annotations_to_add(self) -> List[Annotation]:
187191
annotation_to_add = Annotation(
188192
shape=Rectangle(x1=0.1, y1=0.1, x2=0.7, y2=0.8),
189193
labels=[ScoredLabel(label=labels_to_add[0])],
194+
id=ID("added_annotation_1"),
190195
)
191196
other_annotation_to_add = Annotation(
192197
shape=Rectangle(x1=0.2, y1=0.3, x2=0.8, y2=0.9),
193198
labels=[ScoredLabel(label=labels_to_add[1])],
199+
id=ID("added_annotation_2"),
194200
)
195201
return [annotation_to_add, other_annotation_to_add]
196202

@@ -393,6 +399,7 @@ def test_dataset_item_roi_numpy(self):
393399
rectangle_roi = Annotation(
394400
Rectangle(x1=0.2, y1=0.1, x2=0.8, y2=0.9),
395401
[ScoredLabel(roi_label)],
402+
ID("rectangle_roi"),
396403
)
397404
assert np.array_equal(
398405
dataset_item.roi_numpy(rectangle_roi), media.numpy[1:9, 3:13]
@@ -401,6 +408,7 @@ def test_dataset_item_roi_numpy(self):
401408
ellipse_roi = Annotation(
402409
Ellipse(x1=0.1, y1=0.0, x2=0.9, y2=0.8),
403410
[ScoredLabel(roi_label)],
411+
ID("ellipse_roi"),
404412
)
405413
assert np.array_equal(
406414
dataset_item.roi_numpy(ellipse_roi), media.numpy[0:8, 2:14]
@@ -417,6 +425,7 @@ def test_dataset_item_roi_numpy(self):
417425
]
418426
),
419427
labels=[],
428+
id=ID("polygon_roi"),
420429
)
421430
assert np.array_equal(
422431
dataset_item.roi_numpy(polygon_roi), media.numpy[4:8, 5:13]
@@ -616,6 +625,8 @@ def test_dataset_item_append_annotations(self):
616625
)
617626
dataset_item.append_annotations(annotations_to_add)
618627
# Random id is generated for normalized annotations
628+
normalized_annotations[0].id_ = dataset_item.annotation_scene.annotations[2].id_
629+
normalized_annotations[1].id_ = dataset_item.annotation_scene.annotations[3].id_
619630
assert (
620631
dataset_item.annotation_scene.annotations
621632
== full_box_annotations + normalized_annotations
@@ -633,6 +644,7 @@ def test_dataset_item_append_annotations(self):
633644
incorrect_shape_annotation = Annotation(
634645
shape=incorrect_polygon,
635646
labels=[ScoredLabel(incorrect_shape_label)],
647+
id=ID("incorrect_shape_annotation"),
636648
)
637649
dataset_item.append_annotations([incorrect_shape_annotation])
638650
assert (

ote_sdk/ote_sdk/tests/entities/test_datasets.py

+1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def check_empty_annotations_dataset(
454454
assert actual_item.media is expected_item.media
455455
assert actual_item.annotation_scene.annotations == []
456456
assert actual_item.annotation_scene.kind == expected_kind
457+
assert actual_item.roi.id_ != expected_item.roi.id_
457458
assert actual_item.roi.shape is expected_item.roi.shape
458459
assert actual_item.roi.get_labels() == []
459460
assert actual_item.subset is expected_item.subset

ote_sdk/ote_sdk/tests/entities/test_result_media.py

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def default_result_media_parameters() -> dict:
3838
rectangle_annotation = Annotation(
3939
shape=Rectangle(x1=0.1, y1=0.4, x2=0.4, y2=0.9),
4040
labels=[ScoredLabel(rectangle_label)],
41+
id=ID("rectangle_annotation"),
4142
)
4243
annotation_scene = AnnotationSceneEntity(
4344
annotations=[rectangle_annotation],
@@ -64,6 +65,7 @@ def optional_result_media_parameters(self) -> dict:
6465
roi = Annotation(
6566
shape=Rectangle(x1=0.3, y1=0.2, x2=0.7, y2=0.6),
6667
labels=[ScoredLabel(roi_label)],
68+
id=ID("roi_annotation"),
6769
)
6870
result_media_label = LabelEntity(
6971
"ResultMedia label",

ote_sdk/ote_sdk/tests/utils/test_shape_drawer.py

+4
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ def full_rectangle_annotation(self) -> Annotation:
600600
return Annotation(
601601
shape=Rectangle(x1=0, y1=0, x2=1, y2=1),
602602
labels=self.full_rectangle_scored_labels(),
603+
id=ID("full_rectangle_annotation"),
603604
)
604605

605606
@staticmethod
@@ -628,6 +629,7 @@ def rectangle_annotation(self) -> Annotation:
628629
return Annotation(
629630
shape=Rectangle(x1=0.1, y1=0.4, x2=0.4, y2=0.9),
630631
labels=self.rectangle_scored_labels(),
632+
id=ID("rectangle_annotation"),
631633
)
632634

633635
@staticmethod
@@ -664,6 +666,7 @@ def polygon_annotation(self) -> Annotation:
664666
]
665667
),
666668
labels=self.polygon_scored_labels(),
669+
id=ID("polygon_annotation"),
667670
)
668671

669672
@staticmethod
@@ -692,6 +695,7 @@ def ellipse_annotation(self) -> Annotation:
692695
return Annotation(
693696
shape=Ellipse(x1=0.5, y1=0.0, x2=1.0, y2=0.5),
694697
labels=self.ellipse_scored_labels(),
698+
id=ID("ellipse_annotation"),
695699
)
696700

697701
def annotation_scene(self) -> AnnotationSceneEntity:

ote_sdk/ote_sdk/utils/segmentation_utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
import cv2
1414
import numpy as np
15+
from bson import ObjectId
1516

1617
from ote_sdk.entities.annotation import Annotation
1718
from ote_sdk.entities.dataset_item import DatasetItemEntity
19+
from ote_sdk.entities.id import ID
1820
from ote_sdk.entities.label import LabelEntity
1921
from ote_sdk.entities.scored_label import ScoredLabel
2022
from ote_sdk.entities.shapes.polygon import Point, Polygon
@@ -252,6 +254,7 @@ def create_annotation_from_segmentation_map(
252254
Annotation(
253255
shape=polygon,
254256
labels=[ScoredLabel(label, probability)],
257+
id=ID(ObjectId()),
255258
)
256259
)
257260
else:

0 commit comments

Comments
 (0)