def optional_result_media_parameters(self) -> dict: optional_result_media_parameters = self.default_result_media_parameters( ) roi_label = LabelEntity( "ROI label", Domain.DETECTION, Color(10, 200, 40), creation_date=datetime.datetime(year=2021, month=12, day=18), id=ID("roi_label_1"), ) roi = Annotation( shape=Rectangle(x1=0.3, y1=0.2, x2=0.7, y2=0.6), labels=[ScoredLabel(roi_label)], id=ID("roi_annotation"), ) result_media_label = LabelEntity( "ResultMedia label", Domain.CLASSIFICATION, Color(200, 60, 100), creation_date=datetime.datetime(year=2021, month=12, day=20), id=ID("result_media_1"), ) optional_result_media_parameters["roi"] = roi optional_result_media_parameters["label"] = result_media_label return optional_result_media_parameters
def generate_labels_list(include_empty: bool = True) -> list: classification_label = ScoredLabel( LabelEntity( name="classification", domain=Domain.CLASSIFICATION, color=Color(red=187, green=28, blue=28), creation_date=datetime(year=2021, month=10, day=25), )) detection_label = ScoredLabel( LabelEntity( name="detection", domain=Domain.DETECTION, color=Color(red=180, green=30, blue=24), creation_date=datetime(year=2021, month=9, day=24), )) empty_label = ScoredLabel( LabelEntity( name="empty_rectangle_label", domain=Domain.CLASSIFICATION, color=Color(red=178, green=25, blue=30), creation_date=datetime(year=2021, month=7, day=26), is_empty=True, )) labels_list = [classification_label, detection_label] if include_empty: labels_list.append(empty_label) return labels_list
def test_color(self): """ <b>Description:</b> Check that Color can correctly return the value <b>Expected results:</b> Test passes if the results match """ assert color == Color(red=red, green=green, blue=blue, alpha=alpha) assert color.hex_str == f"#{color_hex}{alpha_hex}" assert type(color.random()) == Color assert color.rgb_tuple == (red, green, blue) assert color.bgr_tuple == (blue, green, red) assert color != ColorEntity assert (repr(color) == f"Color(red={red}, green={green}, blue={blue}, alpha={alpha})") assert color.red == red assert color.green == green assert color.blue == blue color.red = 68 color.green = 54 color.blue = 32 color.alpha = 0 assert color.hex_str == "#44362000"
def default_result_media_parameters() -> dict: rectangle_label = LabelEntity( name="Rectangle Annotation Label", domain=Domain.DETECTION, color=Color(100, 200, 60), creation_date=datetime.datetime(year=2021, month=12, day=16), id=ID("rectangle_label_1"), ) rectangle_annotation = Annotation( shape=Rectangle(x1=0.1, y1=0.4, x2=0.4, y2=0.9), labels=[ScoredLabel(rectangle_label)], id=ID("rectangle_annotation"), ) annotation_scene = AnnotationSceneEntity( annotations=[rectangle_annotation], kind=AnnotationSceneKind.ANNOTATION, creation_date=datetime.datetime(year=2021, month=12, day=16), id=ID("annotation_scene"), ) return { "name": "ResultMedia name", "type": "Test ResultMedia", "annotation_scene": annotation_scene, "numpy": RANDOM_IMAGE, }
def rectangle_labels() -> list: rectangle_label = LabelEntity( name="Rectangle label", domain=Domain.DETECTION, color=Color(red=100, green=50, blue=200), id=ID("rectangle_label_1"), ) other_rectangle_label = LabelEntity( name="Other rectangle label", domain=Domain.SEGMENTATION, color=Color(red=200, green=80, blue=100), id=ID("rectangle_label_2"), ) return [ ScoredLabel(label=rectangle_label), ScoredLabel(label=other_rectangle_label), ]
def labels_to_add() -> List[LabelEntity]: label_to_add = LabelEntity( name="Label which will be added", domain=Domain.DETECTION, color=Color(red=60, green=120, blue=70), creation_date=datetime.datetime(year=2021, month=12, day=12), id=ID("label_to_add_1"), ) other_label_to_add = LabelEntity( name="Other label to add", domain=Domain.SEGMENTATION, color=Color(red=80, green=70, blue=100), creation_date=datetime.datetime(year=2021, month=12, day=11), is_empty=True, id=ID("label_to_add_2"), ) return [label_to_add, other_label_to_add]
def test_dataset_item_append_annotations(self): """ <b>Description:</b> Check DatasetItemEntity class "append_annotations" method <b>Input data:</b> DatasetItemEntity class object with specified "media", "annotation_scene", "roi", "metadata" and "subset" parameters <b>Expected results:</b> Test passes if annotations list returned after "append_annotations" method is equal to expected <b>Steps</b> 1. Check annotations list returned after "append_annotations" method with specified non-included annotations 2. Check annotations list returned after "append_annotations" method with incorrect shape annotation """ # Checking annotations list returned after "append_annotations" method with specified non-included annotations dataset_item = DatasetItemParameters().default_values_dataset_item() full_box_annotations = list(dataset_item.annotation_scene.annotations) annotations_to_add = self.annotations_to_add() normalized_annotations = [] for annotation in annotations_to_add: normalized_annotations.append( Annotation( shape=annotation.shape.normalize_wrt_roi_shape( dataset_item.roi.shape), labels=annotation.get_labels(), )) dataset_item.append_annotations(annotations_to_add) # Random id is generated for normalized annotations normalized_annotations[ 0].id = dataset_item.annotation_scene.annotations[2].id normalized_annotations[ 1].id = dataset_item.annotation_scene.annotations[3].id assert ( dataset_item.annotation_scene.annotations == full_box_annotations + normalized_annotations) # Checking annotations list returned after "append_annotations" method with incorrect shape annotation incorrect_shape_label = LabelEntity( name="Label for incorrect shape", domain=Domain.CLASSIFICATION, color=Color(red=80, green=70, blue=155), id=ID("incorrect_shape_label"), ) incorrect_polygon = Polygon( [Point(x=0.01, y=0.1), Point(x=0.35, y=0.1), Point(x=0.35, y=0.1)]) incorrect_shape_annotation = Annotation( shape=incorrect_polygon, labels=[ScoredLabel(incorrect_shape_label)], id=ID("incorrect_shape_annotation"), ) dataset_item.append_annotations([incorrect_shape_annotation]) assert ( dataset_item.annotation_scene.annotations == full_box_annotations + normalized_annotations)
def test_shape_set_labels(self): """ <b>Description:</b> Check Shape set_labels method for Rectangle, Ellipse and Polygon objects <b>Expected results:</b> Test passes if set_labels method returns expected values <b>Steps</b> 1. Check set_labels method to add labels list to Shape object with no labels specified 2. Check set_labels method to add empty labels list to Shape object with no labels specified 3. Check set_labels method to add labels list to Shape object with labels specified 4. Check set_labels method to add empty labels list to Shape object with labels specified """ not_empty_label = self.appendable_label() new_labels_list = [ not_empty_label, ScoredLabel( LabelEntity( name="new_label", domain=Domain.CLASSIFICATION, color=Color(red=183, green=31, blue=28), creation_date=datetime(year=2021, month=9, day=25), is_empty=True, )), ] expected_not_empty_labels_list = [not_empty_label] # Check for adding labels list to Shape with no labels specified for no_labels_shape in [ self.fully_covering_rectangle(), self.fully_covering_ellipse(), self.fully_covering_polygon(), ]: no_labels_shape.set_labels(new_labels_list) assert no_labels_shape.get_labels( ) == expected_not_empty_labels_list assert no_labels_shape.get_labels( include_empty=True) == new_labels_list # Check for adding empty labels list to Shape with no labels specified for no_labels_shape in [ self.fully_covering_rectangle(), self.fully_covering_ellipse(), self.fully_covering_polygon(), ]: no_labels_shape.set_labels([]) assert no_labels_shape.get_labels() == [] assert no_labels_shape.get_labels(include_empty=True) == [] # Check for adding labels list to Shape with labels specified for shape in [self.rectangle(), self.ellipse(), self.polygon()]: shape.set_labels(new_labels_list) assert shape.get_labels() == expected_not_empty_labels_list assert shape.get_labels(include_empty=True) == new_labels_list # Check for adding empty labels list to Shape with labels specified for shape in [self.rectangle(), self.ellipse(), self.polygon()]: shape.set_labels([]) assert shape.get_labels() == [] assert shape.get_labels(include_empty=True) == []
def roi_labels() -> List[LabelEntity]: creation_date = datetime.datetime(year=2021, month=12, day=9) roi_label = LabelEntity( name="ROI label", domain=Domain.DETECTION, color=Color(red=40, green=180, blue=80), creation_date=creation_date, id=ID("roi_label_1"), ) other_roi_label = LabelEntity( name="Second ROI label", domain=Domain.SEGMENTATION, color=Color(red=80, green=90, blue=70), creation_date=creation_date, is_empty=True, id=ID("roi_label_2"), ) return [roi_label, other_roi_label]
def appendable_label(empty=False) -> ScoredLabel: return ScoredLabel( LabelEntity( name="appended_label", domain=Domain.CLASSIFICATION, color=Color(red=181, green=28, blue=31), creation_date=datetime(year=2021, month=11, day=22), is_empty=empty, ))
def labels() -> List[LabelEntity]: creation_date = datetime.datetime(year=2021, month=12, day=9) detection_label = LabelEntity( name="Label for Detection", domain=Domain.DETECTION, color=Color(red=100, green=200, blue=150), creation_date=creation_date, id=ID("detection_label"), ) segmentation_label = LabelEntity( name="Label for Segmentation", domain=Domain.DETECTION, color=Color(red=50, green=80, blue=200), creation_date=creation_date, is_empty=True, id=ID("segmentation_label"), ) return [detection_label, segmentation_label]
def test_scored_label(self): """ <b>Description:</b> Check the ScoredLabel can correctly return the value <b>Input data:</b> LabelEntity <b>Expected results:</b> Test passes if the results match """ car = LabelEntity(id=ID(123456789), name="car", domain=Domain.DETECTION, is_empty=True) person = LabelEntity(id=ID(987654321), name="person", domain=Domain.DETECTION, is_empty=True) car_label = ScoredLabel(car) person_label = ScoredLabel(person) for attr in [ "id", "name", "color", "hotkey", "creation_date", "is_empty" ]: assert getattr(car_label, attr) == getattr(car, attr) assert car_label.get_label() == car assert car_label == ScoredLabel(car) assert car_label != car assert car_label != person_label assert hash(car_label) == hash(str(car_label)) probability = 0.0 assert car_label.probability == probability delta_probability = 0.4 probability += delta_probability car_label.probability += delta_probability assert car_label.probability == probability car.color = Color(red=16, green=15, blue=56, alpha=255) assert ( "ScoredLabel(123456789, name=car, probability=0.4, domain=DETECTION, color=" in repr(car_label)) assert "Color(red=16, green=15, blue=56, alpha=255), hotkey=ctrl+0)" in repr( car_label)
def __init__( self, name: str, domain: Domain, color: Optional[Color] = None, hotkey: str = "", creation_date: Optional[datetime.datetime] = None, is_empty: bool = False, id: Optional[ID] = None, ): id = ID() if id is None else id color = Color.random() if color is None else color creation_date = now() if creation_date is None else creation_date if not hotkey and is_empty: hotkey = "ctrl+0" self._name = name self._color = color self._hotkey = hotkey self._domain = domain self._is_empty = is_empty self._creation_date = creation_date self._id = id
def new_label_by_name(self, name: str) -> LabelEntity: label = LabelEntity(name=name, color=Color.random(), domain=self.label_domain) label.id = generate_unique_id() return label
class TestAnnotation: rectangle = Rectangle(x1=0.5, x2=1.0, y1=0.0, y2=0.5) labels: List[ScoredLabel] = [] annotation = Annotation(shape=rectangle, labels=labels) car = LabelEntity( id=ID(123456789), name="car", domain=Domain.DETECTION, color=Color(red=16, green=15, blue=56, alpha=255), is_empty=True, ) person = LabelEntity( id=ID(987654321), name="person", domain=Domain.DETECTION, color=Color(red=11, green=18, blue=38, alpha=200), is_empty=False, ) car_label = ScoredLabel(car) person_label = ScoredLabel(person) labels2 = [car_label, person_label] @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_default_property(self): """ <b>Description:</b> Check that Annotation can correctly return default property value <b>Input data:</b> Annotation class <b>Expected results:</b> Test passes if the Annotation return correct values <b>Steps</b> 1. Create Annotation instances 2. Check default values """ annotation = self.annotation assert type(annotation.id) == ID assert annotation.id is not None assert str(annotation.shape ) == "Rectangle(x=0.5, y=0.0, width=0.5, height=0.5)" assert annotation.get_labels() == [] @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_setters(self): """ <b>Description:</b> Check that Annotation can correctly return modified property value <b>Input data:</b> Annotation class <b>Expected results:</b> Test passes if the Annotation return correct values <b>Steps</b> 1. Create Annotation instances 2. Set another values 3. Check changed values """ annotation = self.annotation ellipse = Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3) annotation.shape = ellipse annotation.id = ID(123456789) assert annotation.id == ID(123456789) assert annotation.shape == ellipse @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_magic_methods(self): """ <b>Description:</b> Check Annotation __repr__, __eq__ methods <b>Input data:</b> Initialized instance of Annotation <b>Expected results:</b> Test passes if Annotation magic methods returns correct values <b>Steps</b> 1. Create Annotation instances 2. Check returning value of magic methods """ annotation = self.annotation other_annotation = self.annotation point1 = Point(0.3, 0.1) point2 = Point(0.8, 0.3) point3 = Point(0.6, 0.2) points = [point1, point2, point3] third_annotation = Annotation(shape=Polygon(points=points), labels=self.labels) assert ( repr(annotation) == "Annotation(shape=Ellipse(x1=0.5, y1=0.1, x2=0.8, y2=0.3), labels=[], id=123456789)" ) assert annotation == other_annotation assert annotation != third_annotation assert annotation != str @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_get_labels(self): """ <b>Description:</b> Check Annotation get_labels method <b>Input data:</b> Initialized instance of Annotation <b>Expected results:</b> Test passes if Annotation get_labels method returns correct values <b>Steps</b> 1. Create Annotation instances 2. Check returning value of get_labels method 3. Check returning value of get_labels method with include_empty=True """ annotation = Annotation(shape=self.rectangle, labels=self.labels2) assert ( "[ScoredLabel(987654321, name=person, probability=0.0, domain=DETECTION," in str(annotation.get_labels())) assert "color=Color(red=11, green=18, blue=38, alpha=200), hotkey=)]" in str( annotation.get_labels()) assert "[ScoredLabel(123456789, name=car" in str( annotation.get_labels(include_empty=True)) assert ", probability=0.0, domain=DETECTION," in str( annotation.get_labels(include_empty=True)) assert "color=Color(red=16, green=15," in str( annotation.get_labels(include_empty=True)) assert "blue=56, alpha=255), hotkey=ctrl+0)," in str( annotation.get_labels(include_empty=True)) @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_get_label_ids(self): """ <b>Description:</b> Check Annotation get_label_ids method <b>Input data:</b> Initialized instance of Annotation <b>Expected results:</b> Test passes if Annotation get_label_ids method returns correct values <b>Steps</b> 1. Create Annotation instances 2. Check returning value of get_label_ids method 3. Check returning value of get_label_ids method with include_empty=True """ annotation = Annotation(shape=self.rectangle, labels=self.labels2) assert annotation.get_label_ids() == {ID(987654321)} assert annotation.get_label_ids(include_empty=True) == { ID(987654321), ID(123456789), } @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_append_label(self): """ <b>Description:</b> Check Annotation append_label method <b>Input data:</b> Initialized instance of Annotation <b>Expected results:</b> Test passes if Annotation append_label method correct appending label <b>Steps</b> 1. Create Annotation instances 2. Append label 3. Check labels """ annotation = self.annotation annotation.append_label(label=self.car_label) assert annotation.get_labels() == [] # car_label is empty annotation.append_label(label=self.person_label) assert "name=person" in str(annotation.get_labels()) @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_annotation_set_labels(self): """ <b>Description:</b> Check Annotation set_labels method <b>Input data:</b> Initialized instance of Annotation <b>Expected results:</b> Test passes if Annotation set_labels method correct setting label <b>Steps</b> 1. Create Annotation instances 2. Set labels 3. Check labels """ annotation = self.annotation assert annotation.get_labels() != [] annotation.set_labels(labels=[]) assert annotation.get_labels() == [] annotation.set_labels(labels=self.labels2) assert "name=person" in str(annotation.get_labels()) assert "name=car" not in str( annotation.get_labels()) # car_label is empty
from ote_sdk.entities.color import Color, ColorEntity from ote_sdk.tests.constants.ote_sdk_components import OteSdkComponent from ote_sdk.tests.constants.requirements import Requirements red = 40 red_hex = "28" green = 210 green_hex = "d2" blue = 43 blue_hex = "2b" alpha = 255 alpha_hex = "ff" color_hex = f"{red_hex}{green_hex}{blue_hex}" color = Color.from_hex_str(color_hex) @pytest.mark.components(OteSdkComponent.OTE_SDK) class TestColor: @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_color(self): """ <b>Description:</b> Check that Color can correctly return the value <b>Expected results:</b> Test passes if the results match """
class TestLabelEntity: creation_date = now() label_car_params = { "name": "car", "domain": Domain.DETECTION, "color": Color(255, 0, 0), "hotkey": "ctrl+1", "creation_date": creation_date, "is_empty": False, "id": ID(123456789), } label_person_params = { "name": "person", "domain": Domain.DETECTION, "color": Color(255, 17, 17), "hotkey": "ctrl+2", "creation_date": creation_date, "is_empty": False, "id": ID(987654321), } car = LabelEntity(**label_car_params) # type: ignore empty = LabelEntity(name="empty", domain=Domain.SEGMENTATION, is_empty=True) person = LabelEntity(**label_person_params) # type: ignore @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_label_entity(self): """ <b>Description:</b> Check the LabelEntity can correctly return the value <b>Input data:</b> Dummy data <b>Expected results:</b> Test passes if incoming data is processed correctly <b>Steps</b> 1. Use already created dummy data 2. Check the processing of default values 3. Check the processing of changed values """ assert self.car == LabelEntity(**self.label_car_params) assert self.car != Domain assert self.car != self.person for attr in [ "name", "domain", "color", "hotkey", "creation_date", "is_empty", "id", ]: assert getattr(self.car, attr) == self.label_car_params[attr] label_car_new_name = "electric car" label_car_new_domain = Domain.CLASSIFICATION label_car_new_color = Color(0, 255, 0) label_car_new_hotkey = "ctrl+2" label_car_new_id = ID(987654321) setattr(self.car, "name", label_car_new_name) setattr(self.car, "domain", label_car_new_domain) setattr(self.car, "color", label_car_new_color) setattr(self.car, "hotkey", label_car_new_hotkey) setattr(self.car, "id", label_car_new_id) assert self.car.name == label_car_new_name assert self.car.domain == label_car_new_domain assert self.car.color == label_car_new_color assert self.car.hotkey == label_car_new_hotkey assert self.car.id == label_car_new_id test_label_entity_repr = [ f"{self.car.id}", f"name={self.car.name}", f"hotkey={self.car.hotkey}", f"domain={self.car.domain}", f"color={self.car.color}", ] for i in test_label_entity_repr: assert i in self.car.__repr__() assert hash(self.car) == hash(str(self.car)) assert self.car.__lt__(Domain) is False assert self.car.__gt__(Domain) is False @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_empty_label_entity(self): """ <b>Description:</b> Check the LabelEntity can correctly return the value for empty label <b>Input data:</b> Dummy data <b>Expected results:</b> Test passes if incoming data is processed correctly <b>Steps</b> 1. Use already created dummy data 2. Check the processing of default values """ assert self.empty.hotkey == "ctrl+0" assert self.empty.id == ID() assert type(self.empty.color) == Color @pytest.mark.priority_medium @pytest.mark.component @pytest.mark.reqids(Requirements.REQ_1) def test_label_comparison(self): """ <b>Description:</b> Check the LabelEntity __lt__, __gt__ methods with changed id <b>Input data:</b> Dummy data <b>Expected results:</b> Test passes if incoming data is processed correctly <b>Steps</b> 1. Use already created dummy data 2. Check the processing of changed id """ self.empty.id = ID(999999999) assert self.empty > self.car assert self.car < self.empty
def test_label_entity(self): """ <b>Description:</b> Check the LabelEntity can correctly return the value <b>Input data:</b> Dummy data <b>Expected results:</b> Test passes if incoming data is processed correctly <b>Steps</b> 1. Use already created dummy data 2. Check the processing of default values 3. Check the processing of changed values """ assert self.car == LabelEntity(**self.label_car_params) assert self.car != Domain assert self.car != self.person for attr in [ "name", "domain", "color", "hotkey", "creation_date", "is_empty", "id", ]: assert getattr(self.car, attr) == self.label_car_params[attr] label_car_new_name = "electric car" label_car_new_domain = Domain.CLASSIFICATION label_car_new_color = Color(0, 255, 0) label_car_new_hotkey = "ctrl+2" label_car_new_id = ID(987654321) setattr(self.car, "name", label_car_new_name) setattr(self.car, "domain", label_car_new_domain) setattr(self.car, "color", label_car_new_color) setattr(self.car, "hotkey", label_car_new_hotkey) setattr(self.car, "id", label_car_new_id) assert self.car.name == label_car_new_name assert self.car.domain == label_car_new_domain assert self.car.color == label_car_new_color assert self.car.hotkey == label_car_new_hotkey assert self.car.id == label_car_new_id test_label_entity_repr = [ f"{self.car.id}", f"name={self.car.name}", f"hotkey={self.car.hotkey}", f"domain={self.car.domain}", f"color={self.car.color}", ] for i in test_label_entity_repr: assert i in self.car.__repr__() assert hash(self.car) == hash(str(self.car)) assert self.car.__lt__(Domain) is False assert self.car.__gt__(Domain) is False
def backward(instance: dict) -> Color: """Deserializes from dict.""" return Color( instance["red"], instance["green"], instance["blue"], instance["alpha"] )