Ejemplo n.º 1
0
def _convert(file_data: Dict[str, Any], path) -> AnnotationFile:
    filename: str = str(file_data.get("External ID"))
    label: Dict[str, Any] = cast(Dict[str, Any], file_data.get("Label"))
    label_objects: List[Dict[str, Any]] = cast(List[Dict[str, Any]],
                                               label.get("objects"))
    label_classifications: List[Dict[str,
                                     Any]] = cast(List[Dict[str, Any]],
                                                  label.get("classifications"))

    classification_annotations: List[Annotation] = []
    if len(label_classifications) > 0:
        classification_annotations = _flat_map_list(
            _map_list(_convert_label_classifications, label_classifications))

    object_annotations: List[Annotation] = _map_list(_convert_label_objects,
                                                     label_objects)
    annotations: List[
        Annotation] = object_annotations + classification_annotations

    classes: Set[AnnotationClass] = set(map(_get_class, annotations))
    return AnnotationFile(annotations=annotations,
                          path=path,
                          filename=filename,
                          annotation_classes=classes,
                          remote_path="/")
Ejemplo n.º 2
0
def test_complete_annotation_file():
    annotation_class = AnnotationClass(name="test", annotation_type="polygon")
    annotation = Annotation(annotation_class=annotation_class,
                            data={"path": []},
                            subs=[])

    annotation_file = AnnotationFile(
        path=Path("test.json"),
        filename="test.json",
        annotation_classes=[annotation_class],
        annotations=[annotation],
        image_height=1080,
        image_width=1920,
        image_url="https://darwin.v7labs.com/image.jpg",
    )

    assert build_image_annotation(annotation_file) == {
        "annotations": [{
            "name": "test",
            "polygon": {
                "path": []
            }
        }],
        "image": {
            "filename": "test.json",
            "height": 1080,
            "url": "https://darwin.v7labs.com/image.jpg",
            "width": 1920
        },
    }
Ejemplo n.º 3
0
def test_empty_annotation_file():
    annotation_file = AnnotationFile(path=Path("test.json"),
                                     filename="test.json",
                                     annotation_classes=[],
                                     annotations=[])

    assert build_image_annotation(annotation_file) == {
        "annotations": [],
        "image": {
            "filename": "test.json",
            "height": None,
            "url": None,
            "width": None
        },
    }
Ejemplo n.º 4
0
    def test_xml_has_bounding_boxes_of_complex_polygons():
        annotation_class = AnnotationClass(name="rubber",
                                           annotation_type="complex_polygon",
                                           annotation_internal_type="polygon")
        annotation = Annotation(
            annotation_class=annotation_class,
            data={
                "paths": [{...}],
                "bounding_box": {
                    "x": 1174.28,
                    "y": 2379.17,
                    "w": 824.9000000000001,
                    "h": 843.52
                }
            },
            subs=[],
        )

        annotation_file = AnnotationFile(
            path=Path("/annotation_test.json"),
            filename="annotation_test.jpg",
            annotation_classes={annotation_class},
            annotations=[annotation],
            frame_urls=None,
            image_height=4000,
            image_width=6000,
            is_video=False,
        )

        xml = build_xml(annotation_file)

        object = get_xml_element(xml, "object")
        assert_xml_element_text(object, "name", "rubber")
        assert_xml_element_text(object, "pose", "Unspecified")
        assert_xml_element_text(object, "truncated", "0")
        assert_xml_element_text(object, "difficult", "0")

        bndbox = get_xml_element(object, "bndbox")
        assert_xml_element_text(bndbox, "xmin", "1174")
        assert_xml_element_text(bndbox, "ymin", "2379")
        assert_xml_element_text(bndbox, "xmax", "1999")
        assert_xml_element_text(bndbox, "ymax", "3223")
Ejemplo n.º 5
0
    def test_xml_has_bounding_boxes_of_polygons():
        annotation_class = AnnotationClass(name="car",
                                           annotation_type="polygon",
                                           annotation_internal_type=None)
        annotation = Annotation(
            annotation_class=annotation_class,
            data={
                "path": [{...}],
                "bounding_box": {
                    "x": 94.0,
                    "y": 438.0,
                    "w": 1709.0,
                    "h": 545.0
                },
            },
            subs=[],
        )
        annotation_file = AnnotationFile(
            path=Path("/annotation_test.json"),
            filename="annotation_test.jpg",
            annotation_classes={annotation_class},
            annotations=[annotation],
            frame_urls=None,
            image_height=1080,
            image_width=1920,
            is_video=False,
        )

        xml = build_xml(annotation_file)

        object = get_xml_element(xml, "object")
        assert_xml_element_text(object, "name", "car")
        assert_xml_element_text(object, "pose", "Unspecified")
        assert_xml_element_text(object, "truncated", "0")
        assert_xml_element_text(object, "difficult", "0")

        bndbox = get_xml_element(object, "bndbox")
        assert_xml_element_text(bndbox, "xmin", "94")
        assert_xml_element_text(bndbox, "ymin", "438")
        assert_xml_element_text(bndbox, "xmax", "1803")
        assert_xml_element_text(bndbox, "ymax", "983")
Ejemplo n.º 6
0
def _convert(
    instances: List[Dict[str, Any]],
    annotation_file_path: Path,
    superannotate_classes: List[Dict[str, Any]],
    metadata: Dict[str, Any],
    tags: List[str],
) -> AnnotationFile:
    conver_to_darwin_object = partial(_convert_instance, superannotate_classes=superannotate_classes)

    filename: str = str(metadata.get("name"))
    darwin_tags: List[Annotation] = _map_to_list(make_tag, tags)
    darwin_objects: List[Annotation] = _map_to_list(conver_to_darwin_object, instances)
    annotations: List[Annotation] = darwin_objects + darwin_tags
    classes: Set[AnnotationClass] = _map_to_set(_get_class, annotations)

    return AnnotationFile(
        annotations=annotations,
        path=annotation_file_path,
        filename=filename,
        annotation_classes=classes,
        remote_path="/",
    )
Ejemplo n.º 7
0
    def test_xml_has_bounding_boxes():
        annotation_class = AnnotationClass(name="tire",
                                           annotation_type="bounding_box",
                                           annotation_internal_type=None)
        annotation = Annotation(
            annotation_class=annotation_class,
            data={
                "x": 574.88,
                "y": 427.0,
                "w": 137.04,
                "h": 190.66
            },
            subs=[],
        )
        annotation_file = AnnotationFile(
            path=Path("/annotation_test.json"),
            filename="annotation_test.jpg",
            annotation_classes={annotation_class},
            annotations=[annotation],
            frame_urls=None,
            image_height=853,
            image_width=1400,
            is_video=False,
        )

        xml = build_xml(annotation_file)

        object = get_xml_element(xml, "object")
        assert_xml_element_text(object, "name", "tire")
        assert_xml_element_text(object, "pose", "Unspecified")
        assert_xml_element_text(object, "truncated", "0")
        assert_xml_element_text(object, "difficult", "0")

        bndbox = get_xml_element(object, "bndbox")
        assert_xml_element_text(bndbox, "xmin", "575")
        assert_xml_element_text(bndbox, "ymin", "427")
        assert_xml_element_text(bndbox, "xmax", "712")
        assert_xml_element_text(bndbox, "ymax", "618")
Ejemplo n.º 8
0
    def it_returns_empty_file_if_there_are_no_annotations(
            annotations_file_path: Path, classes_file_path: Path):
        annotations_json: str = """
         {
            "instances": [],
            "tags": [],
            "metadata": {
               "name": "demo-image-0.jpg"
            }
         }
         """
        classes_json: str = """[]"""

        annotations_file_path.write_text(annotations_json)
        classes_file_path.write_text(classes_json)

        assert parse_path(annotations_file_path) == AnnotationFile(
            annotations=[],
            path=annotations_file_path,
            filename="demo-image-0.jpg",
            annotation_classes=set(),
            remote_path="/",
        )