Skip to content

Commit

Permalink
converters without otx
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Davidyuk <igor.davidyuk@intel.com>
  • Loading branch information
igor-davidyuk committed Mar 6, 2024
1 parent c8a55e4 commit db859a0
Show file tree
Hide file tree
Showing 13 changed files with 1,112 additions and 75 deletions.
20 changes: 20 additions & 0 deletions 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 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

0 comments on commit db859a0

Please sign in to comment.