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

OTX decoupling: deployment #345

Merged
23 commits merged into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
27 changes: 26 additions & 1 deletion geti_sdk/data_models/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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


@attr.define
Expand Down Expand Up @@ -64,10 +65,29 @@ class Label:
group: str
is_empty: bool
hotkey: str = ""
domain: Optional[Domain] = None
id: Optional[str] = None
parent_id: Optional[str] = None
is_anomalous: Optional[bool] = None

def __key(self) -> Tuple[str, str]:
"""
Return a tuple representing the key of the label.

The key is a tuple containing the name and color of the label.

:return: A tuple representing the key of the label.
"""
return (self.name, self.color)

def __hash__(self) -> int:
"""
Calculate the hash value of the object.

:return: The hash value of the object.
"""
return hash(self.__key())

def to_ote(self, task_type: TaskType) -> LabelEntity:
"""
Convert the `Label` instance to an OTE SDK LabelEntity object.
Expand All @@ -81,6 +101,7 @@ def to_ote(self, task_type: TaskType) -> LabelEntity:
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:
Expand Down Expand Up @@ -160,7 +181,11 @@ def from_ote(cls, ote_label: OteScoredLabel) -> "ScoredLabel":
name=ote_label.name,
id=ote_label.id,
probability=ote_label.probability,
color=ote_label.color.hex_str,
color=(
ote_label.color
if isinstance(ote_label.color, str)
else ote_label.color.hex_str
),
)

def to_ote(self) -> OteScoredLabel:
Expand Down
51 changes: 51 additions & 0 deletions geti_sdk/data_models/label_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from enum import Enum
from typing import List, Optional

from geti_sdk.data_models.label import Label


class LabelGroupType(Enum):
"""Enum to indicate the LabelGroupType."""

EXCLUSIVE = 1
EMPTY_LABEL = 2


class LabelGroup:
"""
Representation of a group of labels.
"""

def __init__(
self,
name: str,
labels: List[Label],
group_type: LabelGroupType = LabelGroupType.EXCLUSIVE,
id: Optional[str] = None,
) -> None:
"""
Initialize a LabelGroup object.

:param name: The name of the label group.
:param labels: A list of Label objects associated with the group.
:param group_type: The type of the label group. Defaults to LabelGroupType.EXCLUSIVE.
:param id: The ID of the label group. Defaults to None.
"""
self.id = id
self.name = name
self.group_type = group_type
self.labels = sorted(labels, key=lambda label: label.id)
65 changes: 65 additions & 0 deletions geti_sdk/data_models/label_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from typing import List, Optional

from geti_sdk.data_models.label import Label
from geti_sdk.data_models.label_group import LabelGroup, LabelGroupType


class LabelSchema:
"""
The `LabelSchema` class defines the structure and properties of labels and label groups.

:param label_groups: Optional list of `LabelGroup` objects representing the label groups in the schema
"""

def __init__(self, label_groups: Optional[List[LabelGroup]] = None) -> None:
"""
Initialize a new instance of the `LabelSchema` class.

:param label_groups: Optional list of `LabelGroup` objects representing the label groups in the schema
"""
self._groups = label_groups

def get_labels(self, include_empty: bool = False) -> List[Label]:
"""
Get the labels in the label schema.

:param include_empty: Flag determining whether to include empty labels
:return: List of all labels in the label schema
"""
labels = {
label
for group in self._groups
for label in group.labels
if include_empty or not label.is_empty
}
return sorted(list(labels), key=lambda label: label.id)

def get_groups(self, include_empty: bool = False) -> List[LabelGroup]:
"""
Get the label groups in the label schema.

:param include_empty: Flag determining whether to include empty label groups
:return: List of all label groups in the label schema
"""
if include_empty:
return self._groups

return [
group
for group in self._groups
if group.group_type != LabelGroupType.EMPTY_LABEL
]
11 changes: 11 additions & 0 deletions geti_sdk/data_models/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ def y_max(self) -> int:
"""
return self.y + self.height

@classmethod
def generate_full_box(cls, image_width: int, image_height: int) -> "Rectangle":
"""
Return a rectangle that fully encapsulates the image.

:param image_width: Width of the image to which the rectangle applies (in pixels)
:param image_height: Height of the image to which the rectangle applies (in pixels)
:return: Rectangle: A rectangle that fully encapsulates the image.
"""
return cls(x=0, y=0, width=image_width, height=image_height)


@attr.define(slots=False)
class Ellipse(Shape):
Expand Down
Loading
Loading