Skip to content

Commit

Permalink
Removed labels from shape and id from annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertvanHouten committed May 12, 2022
1 parent fa03d14 commit 981dea2
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 425 deletions.
34 changes: 3 additions & 31 deletions ote_sdk/ote_sdk/entities/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
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 @@ -23,42 +21,17 @@ class Annotation(metaclass=abc.ABCMeta):
"""

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

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

@property
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):
"""
Expand Down Expand Up @@ -113,8 +86,7 @@ def set_labels(self, labels: List[ScoredLabel]):
def __eq__(self, other):
if isinstance(other, Annotation):
return (
self.id_ == other.id_
and self.get_labels(True) == other.get_labels(True)
self.get_labels(True) == other.get_labels(True)
and self.shape == other.shape
)
return False
Expand Down
4 changes: 0 additions & 4 deletions ote_sdk/ote_sdk/entities/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
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 @@ -277,7 +274,6 @@ 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
19 changes: 7 additions & 12 deletions ote_sdk/ote_sdk/entities/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from scipy import optimize, special
from shapely.geometry import Polygon as shapely_polygon

from ote_sdk.entities.scored_label import ScoredLabel
from ote_sdk.entities.shapes.rectangle import Rectangle
from ote_sdk.entities.shapes.shape import Shape, ShapeType
from ote_sdk.utils.time_utils import now
Expand All @@ -33,7 +32,6 @@ class Ellipse(Shape):
:param y1: top y coordinate of encapsulating rectangle
:param x2: right x coordinate of encapsulating rectangle
:param y2: bottom y coordinate of encapsulating rectangle
:param labels: list of the ScoredLabel's for the Ellipse
:param modification_date: last modified date
"""

Expand All @@ -44,14 +42,11 @@ def __init__(
y1: float,
x2: float,
y2: float,
labels: Optional[List[ScoredLabel]] = None,
modification_date: Optional[datetime.datetime] = None,
):
labels = [] if labels is None else labels
modification_date = now() if modification_date is None else modification_date
super().__init__(
type=ShapeType.ELLIPSE,
labels=labels,
shape_type=ShapeType.ELLIPSE,
modification_date=modification_date,
)

Expand Down Expand Up @@ -92,7 +87,7 @@ def width(self):
:example:
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.5, labels = [])
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> e1.width
0.5
Expand All @@ -107,7 +102,7 @@ def height(self):
:example:
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.5, labels = [])
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> e1.height
0.5
Expand Down Expand Up @@ -136,7 +131,7 @@ def minor_axis(self) -> float:
:example:
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.4, labels = [])
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.4)
>>> e1.minor_axis
0.2
Expand All @@ -153,7 +148,7 @@ def major_axis(self) -> float:
:example:
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.4, labels = [])
>>> e1 = Ellipse(x1=0.5, x2=1.0, y1=0.0, y2=0.4)
>>> e1.major_axis
0.25
Expand All @@ -179,7 +174,7 @@ def normalize_wrt_roi_shape(self, roi_shape: Rectangle) -> "Ellipse":
>>> roi = Rectangle(x1=0.0, x2=0.5, y1=0.0, y2=0.5)
>>> normalized = c1.normalize_wrt_roi_shape(roi_shape)
>>> normalized
Ellipse(, x1=0.25, y1=0.25, x2=0.3, y2=0.3, scored_labels=[])
Ellipse(, x1=0.25, y1=0.25, x2=0.3, y2=0.3)
:param roi_shape: Region of Interest
:return: New polygon in the image coordinate system
Expand Down Expand Up @@ -214,7 +209,7 @@ def denormalize_wrt_roi_shape(self, roi_shape: Rectangle) -> "Ellipse":
>>> roi = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0) # the half-right
>>> normalized = c1.denormalize_wrt_roi_shape(roi_shape) # should return top half
>>> normalized
Ellipse(, x1=0.0, y1=0.0, x2=1.0, y2=0.5, scored_labels=[])
Ellipse(, x1=0.0, y1=0.0, x2=1.0, y2=0.5)
:param roi_shape: Region of Interest
:return: New polygon in the ROI coordinate system
Expand Down
11 changes: 3 additions & 8 deletions ote_sdk/ote_sdk/entities/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from shapely.geometry import Polygon as shapely_polygon

from ote_sdk.entities.scored_label import ScoredLabel
from ote_sdk.entities.shapes.rectangle import Rectangle
from ote_sdk.entities.shapes.shape import Shape, ShapeType
from ote_sdk.utils.time_utils import now
Expand Down Expand Up @@ -80,22 +79,18 @@ class Polygon(Shape):
NB Freehand drawings are also stored as polygons.
:param points: list of Point's forming the polygon
:param labels: list of the ScoredLabel's for the Polygon
:param modification_date: last modified date
"""

# pylint: disable=too-many-arguments; Requires refactor
def __init__(
self,
points: List[Point],
labels: Optional[List[ScoredLabel]] = None,
modification_date: Optional[datetime.datetime] = None,
):
labels = [] if labels is None else labels
modification_date = now() if modification_date is None else modification_date
super().__init__(
type=ShapeType.POLYGON,
labels=labels,
shape_type=ShapeType.POLYGON,
modification_date=modification_date,
)

Expand Down Expand Up @@ -151,7 +146,7 @@ def normalize_wrt_roi_shape(self, roi_shape: Rectangle) -> "Polygon":
>>> roi = Rectangle(x1=0.0, x2=0.5, y1=0.0, y2=0.5)
>>> normalized = p1.normalize_wrt_roi_shape(roi_shape)
>>> normalized
Polygon(, len(points)=3, scored_labels=[])
Polygon(, len(points)=3)
:param roi_shape: Region of Interest
:return: New polygon in the image coordinate system
Expand Down Expand Up @@ -181,7 +176,7 @@ def denormalize_wrt_roi_shape(self, roi_shape: Rectangle) -> "Polygon":
>>> roi = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0) # the half-right
>>> normalized = p1.denormalize_wrt_roi_shape(roi_shape)
>>> normalized
Polygon(, len(points)=3, scored_labels=[])
Polygon(, len(points)=3)
:param roi_shape: Region of Interest
:return: New polygon in the ROI coordinate system
Expand Down
45 changes: 16 additions & 29 deletions ote_sdk/ote_sdk/entities/shapes/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import datetime
import math
import warnings
from typing import List, Optional
from typing import Optional

import numpy as np
from shapely.geometry import Polygon as shapely_polygon

from ote_sdk.entities.scored_label import ScoredLabel
from ote_sdk.entities.shapes.shape import Shape, ShapeEntity, ShapeType
from ote_sdk.utils.time_utils import now

Expand All @@ -36,7 +35,6 @@ class Rectangle(Shape):
:param y1: see above
:param x2: see above
:param y2: see above
:param labels: list of the ScoredLabel's for the rectangle
:param modification_date: last modified date
"""

Expand All @@ -47,14 +45,11 @@ def __init__(
y1: float,
x2: float,
y2: float,
labels: Optional[List[ScoredLabel]] = None,
modification_date: Optional[datetime.datetime] = None,
):
labels = [] if labels is None else labels
modification_date = now() if modification_date is None else modification_date
super().__init__(
type=ShapeType.RECTANGLE,
labels=labels,
shape_type=ShapeType.RECTANGLE,
modification_date=modification_date,
)

Expand Down Expand Up @@ -106,7 +101,7 @@ def clip_to_visible_region(self) -> "Rectangle":
x2 = min(max(0.0, self.x2), 1.0)
y2 = min(max(0.0, self.y2), 1.0)

return Rectangle(x1, y1, x2, y2, [], self.modification_date)
return Rectangle(x1, y1, x2, y2, self.modification_date)

def normalize_wrt_roi_shape(self, roi_shape: "Rectangle") -> "Rectangle":
"""
Expand All @@ -122,7 +117,7 @@ def normalize_wrt_roi_shape(self, roi_shape: "Rectangle") -> "Rectangle":
>>> roi = Rectangle(x1=0.0, x2=0.5, y1=0.0, y2=0.5)
>>> normalized = b1.normalize_wrt_roi_shape(roi_shape)
>>> normalized
Box(, x=0.25, y=0.0, width=0.25, height=0.25, scored_labels=[])
Box(, x=0.25, y=0.0, width=0.25, height=0.25)
:param roi_shape: Region of Interest
:return: New polygon in the image coordinate system
Expand Down Expand Up @@ -152,14 +147,14 @@ def denormalize_wrt_roi_shape(self, roi_shape: "Rectangle") -> "Rectangle":
Box denormalized to a rectangle as ROI
>>> from ote_sdk.entities.annotation import Annotation
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5, labels = [])
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
# the top-right
>>> roi = Annotation(Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0), labels = [])
>>> roi = Annotation(Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0))
# the half-right
>>> normalized = b1.denormalize_wrt_roi_shape(roi_shape)
# should return top half
>>> normalized
Box(, x=0.0, y=0.0, width=1.0, height=0.5, scored_labels=[])
Box(, x=0.0, y=0.0, width=1.0, height=0.5)
:param roi_shape: Region of Interest
:return: New polygon in the ROI coordinate system
Expand Down Expand Up @@ -193,26 +188,18 @@ def _as_shapely_polygon(self) -> shapely_polygon:
return shapely_polygon(points)

@classmethod
def generate_full_box(
cls, labels: Optional[List[ScoredLabel]] = None
) -> "Rectangle":
def generate_full_box(cls) -> "Rectangle":
"""
Returns a rectangle that fully encapsulates the normalized coordinate space,
with `labels`
Returns a rectangle that fully encapsulates the normalized coordinate space
:example:
>>> Rectangle.generate_full_box()
Box(, x=0.0, y=0.0, width=1.0, height=1.0, scored_labels=[])
:param labels: labels to assigned to the output rectangle
Box(, x=0.0, y=0.0, width=1.0, height=1.0)
:return: a rectangle that fully encapsulates the normalized coordinate space,
with `labels`
"""
if labels is None:
labels = []
return cls(x1=0.0, y1=0.0, x2=1.0, y2=1.0, labels=labels)
return cls(x1=0.0, y1=0.0, x2=1.0, y2=1.0)

@staticmethod
def is_full_box(rectangle: ShapeEntity) -> bool:
Expand All @@ -222,11 +209,11 @@ def is_full_box(rectangle: ShapeEntity) -> bool:
:example:
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0, labels = [])
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=1.0)
>>> Rectangle.is_full_box(b1)
False
>>> b2 = Rectangle(x1=0.0, x2=1.0, y1=0.0, y2=1.0, labels = [])
>>> b2 = Rectangle(x1=0.0, x2=1.0, y1=0.0, y2=1.0)
>>> Rectangle.is_full_box(b2)
True
Expand Down Expand Up @@ -271,7 +258,7 @@ def width(self):
:example:
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5, labels = [])
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> b1.width
0.5
Expand All @@ -286,7 +273,7 @@ def height(self):
:example:
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5, labels = [])
>>> b1 = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5)
>>> b1.height
0.5
Expand All @@ -301,7 +288,7 @@ def diagonal(self):
:example:
>>> b1 = Rectangle(x1=0.0, x2=0.3, y1=0.0, y2=0.4, labels = [])
>>> b1 = Rectangle(x1=0.0, x2=0.3, y1=0.0, y2=0.4)
>>> b1.diagonal
0.5
Expand Down
Loading

0 comments on commit 981dea2

Please sign in to comment.