Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on OTX #393

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
47 changes: 0 additions & 47 deletions geti_sdk/data_models/annotation_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import attr
import cv2
import numpy as np
from otx.api.entities.annotation import AnnotationSceneEntity, AnnotationSceneKind

from geti_sdk.data_models.annotations import Annotation
from geti_sdk.data_models.enums import AnnotationKind
Expand Down Expand Up @@ -336,52 +335,6 @@ def apply_identifier(
annotation.modified = ""
return new_annotation

@classmethod
def from_ote(
cls,
ote_annotation_scene: AnnotationSceneEntity,
image_width: int,
image_height: int,
) -> "AnnotationScene":
"""
Create a :py:class:`~geti_sdk.data_models.annotation_scene.AnnotationScene`
instance from a given OTE SDK AnnotationSceneEntity object.

:param ote_annotation_scene: OTE AnnotationSceneEntity object to create the
instance from
:param image_width: Width of the image to which the annotation scene applies
:param image_height: Height of the image to which the annotation scene applies
:return: AnnotationScene instance
"""
annotations = [
Annotation.from_ote(
annotation, image_width=image_width, image_height=image_height
)
for annotation in ote_annotation_scene.annotations
]
return cls(
annotations=annotations,
id=ote_annotation_scene.id,
)

def to_ote(self, image_width: int, image_height: int) -> AnnotationSceneEntity:
"""
Create an AnnotationSceneEntity object from OTE SDK from the Geti SDK
AnnotationScene instance

:param image_width: Width of the image to which the annotation scene applies
:param image_height: Height of the image to which the annotation scene applies
:return: OTE SDK AnnotationSceneEntity instance, corresponding to the current
AnnotationScene
"""
annotations = [
annotation.to_ote(image_width=image_width, image_height=image_height)
for annotation in self.annotations
]
return AnnotationSceneEntity(
annotations=annotations, kind=AnnotationSceneKind[self.kind.name]
)

def map_labels(
self, labels: Sequence[Union[Label, ScoredLabel]]
) -> "AnnotationScene":
Expand Down
43 changes: 1 addition & 42 deletions geti_sdk/data_models/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@
from typing import ClassVar, List, Optional, Sequence, Union

import attr
from otx.api.entities.annotation import Annotation as OteAnnotation

from geti_sdk.data_models.label import Label, ScoredLabel
from geti_sdk.data_models.shapes import (
Ellipse,
Polygon,
Rectangle,
RotatedRectangle,
Shape,
)
from geti_sdk.data_models.shapes import Ellipse, Polygon, Rectangle, RotatedRectangle
from geti_sdk.data_models.utils import deidentify, str_to_datetime


Expand Down Expand Up @@ -97,40 +90,6 @@ def pop_label_by_name(self, label_name: str) -> None:
if index is not None:
self.labels.pop(index)

@classmethod
def from_ote(
cls, ote_annotation: OteAnnotation, image_width: int, image_height: int
) -> "Annotation":
"""
Create a :py:class:`~geti_sdk.data_models.annotations.Annotation` instance
from a given OTE SDK Annotation object.

:param ote_annotation: OTE Annotation object to create the instance from
:param image_width: Width of the image to which the annotation applies
:param image_height: Height of the image to which the annotation applies
:return: Annotation instance
"""
shape = Shape.from_ote(
ote_annotation.shape, image_width=image_width, image_height=image_height
)
labels = [
ScoredLabel.from_ote(ote_label)
for ote_label in ote_annotation.get_labels(include_empty=True)
]
return Annotation(shape=shape, labels=labels, id=ote_annotation.id)

def to_ote(self, image_width: int, image_height: int) -> OteAnnotation:
"""
Create an OTE SDK Annotation object corresponding to this
:py:class:`~geti_sdk.data_models.annotations.Annotation` instance

:param image_width: Width of the image to which the annotation applies
:param image_height: Height of the image to which the annotation applies
"""
shape = self.shape.to_ote(image_width=image_width, image_height=image_height)
labels = [label.to_ote() for label in self.labels]
return OteAnnotation(shape=shape, labels=labels)

def map_labels(self, labels: Sequence[Union[ScoredLabel, Label]]) -> "Annotation":
"""
Attempt to map the labels found in `labels` to those in the Annotation
Expand Down
17 changes: 0 additions & 17 deletions geti_sdk/data_models/enums/task_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from enum import Enum

from otx.api.entities.model_template import Domain as OteDomain


class TaskType(Enum):
"""
Expand Down Expand Up @@ -106,21 +104,6 @@ def from_domain(cls, domain):
"""
return cls[domain.name]

def to_ote_domain(self) -> OteDomain:
"""
Convert a TaskType instance to an OTE SDK Domain object.

NOTE: Not all TaskTypes have a counterpart in the OTE SDK Domain Enum, for
example TaskType.DATASET and TaskType.CROP cannot be converted to a Domain. For
those TaskTypes, a `Domain.NULL` instance will be returned.

:return: Domain instance corresponding to the TaskType instance
"""
if self in NON_TRAINABLE_TASK_TYPES:
return OteDomain.NULL
else:
return OteDomain[self.name]


NON_TRAINABLE_TASK_TYPES = [TaskType.DATASET, TaskType.CROP]

Expand Down
57 changes: 0 additions & 57 deletions geti_sdk/data_models/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
from typing import ClassVar, List, Optional, Tuple

import attr
from otx.api.entities.color import Color
from otx.api.entities.color import Color as OteColor
from otx.api.entities.label import Domain as OteLabelDomain
from otx.api.entities.label import LabelEntity
from otx.api.entities.scored_label import ScoredLabel as OteScoredLabel

from geti_sdk.data_models.enums import TaskType
from geti_sdk.data_models.enums.domain import Domain


Expand Down Expand Up @@ -88,22 +82,6 @@ def __hash__(self) -> int:
"""
return hash(self.__key())

def to_ote(self, task_type: TaskType) -> LabelEntity:
"""
Convert the `Label` instance to an OTE SDK LabelEntity object.

:return: OTE SDK LabelEntity instance corresponding to the label
"""
return LabelEntity(
name=self.name,
domain=task_type.to_ote_domain(),
id=self.id,
hotkey=self.hotkey,
is_empty=self.is_empty,
color=Color.from_hex_str(self.color),
is_anomalous=self.is_anomalous,
)

def prepare_for_post(self) -> None:
"""
Set all fields to None that are not valid for making a POST request to the
Expand Down Expand Up @@ -167,38 +145,3 @@ def from_label(cls, label: Label, probability: float) -> "ScoredLabel":
return ScoredLabel(
name=label.name, probability=probability, color=label.color, id=label.id
)

@classmethod
def from_ote(cls, ote_label: OteScoredLabel) -> "ScoredLabel":
"""
Create a :py:class`~geti_sdk.data_models.label.ScoredLabel` from
the OTE SDK ScoredLabel entity passed.

:param ote_label: OTE SDK ScoredLabel entity to convert from
:return: ScoredLabel instance created according to the ote_label
"""
return cls(
name=ote_label.name,
id=ote_label.id,
probability=ote_label.probability,
color=(
ote_label.color
if isinstance(ote_label.color, str)
else ote_label.color.hex_str
),
)

def to_ote(self) -> OteScoredLabel:
"""
Create a ScoredLabel object from OTE SDK corresponding to this
:py:class`~geti_sdk.data_models.label.ScoredLabel` instance.
"""
return OteScoredLabel(
label=LabelEntity(
name=self.name,
color=OteColor(*self.color_tuple),
id=self.id,
domain=OteLabelDomain.NULL,
),
probability=self.probability,
)
Loading
Loading