예제 #1
0
파일: utils.py 프로젝트: v7labs/darwin-py
def parse_darwin_annotation(
        annotation: Dict[str, Any]) -> Optional[dt.Annotation]:
    name: str = annotation["name"]
    main_annotation: Optional[dt.Annotation] = None
    if "polygon" in annotation:
        bounding_box = annotation.get("bounding_box")
        if "additional_paths" in annotation["polygon"]:
            paths = [annotation["polygon"]["path"]
                     ] + annotation["polygon"]["additional_paths"]
            main_annotation = dt.make_complex_polygon(name, paths,
                                                      bounding_box)
        else:
            main_annotation = dt.make_polygon(name,
                                              annotation["polygon"]["path"],
                                              bounding_box)
    elif "complex_polygon" in annotation:
        bounding_box = annotation.get("bounding_box")
        if "additional_paths" in annotation["complex_polygon"]:
            paths = annotation["complex_polygon"]["path"] + annotation[
                "complex_polygon"]["additional_paths"]
            main_annotation = dt.make_complex_polygon(name, paths,
                                                      bounding_box)
        else:
            main_annotation = dt.make_complex_polygon(
                name, annotation["complex_polygon"]["path"], bounding_box)
    elif "bounding_box" in annotation:
        bounding_box = annotation["bounding_box"]
        main_annotation = dt.make_bounding_box(name, bounding_box["x"],
                                               bounding_box["y"],
                                               bounding_box["w"],
                                               bounding_box["h"])
    elif "tag" in annotation:
        main_annotation = dt.make_tag(name)
    elif "line" in annotation:
        main_annotation = dt.make_line(name, annotation["line"]["path"])
    elif "keypoint" in annotation:
        main_annotation = dt.make_keypoint(name, annotation["keypoint"]["x"],
                                           annotation["keypoint"]["y"])
    elif "ellipse" in annotation:
        main_annotation = dt.make_ellipse(name, annotation["ellipse"])
    elif "cuboid" in annotation:
        main_annotation = dt.make_cuboid(name, annotation["cuboid"])
    elif "skeleton" in annotation:
        main_annotation = dt.make_skeleton(name,
                                           annotation["skeleton"]["nodes"])

    if not main_annotation:
        print(f"[WARNING] Unsupported annotation type: '{annotation.keys()}'")
        return None

    if "instance_id" in annotation:
        main_annotation.subs.append(
            dt.make_instance_id(annotation["instance_id"]["value"]))
    if "attributes" in annotation:
        main_annotation.subs.append(
            dt.make_attributes(annotation["attributes"]))
    if "text" in annotation:
        main_annotation.subs.append(dt.make_text(annotation["text"]["text"]))

    return main_annotation
예제 #2
0
    def test_it_returns_annotation_with_bounding_box():
        class_name: str = "class_name"
        points: List[List[Point]] = [
            [{
                "x": 1,
                "y": 2
            }, {
                "x": 3,
                "y": 4
            }, {
                "x": 1,
                "y": 2
            }],
            [{
                "x": 4,
                "y": 5
            }, {
                "x": 6,
                "y": 7
            }, {
                "x": 4,
                "y": 5
            }],
        ]
        bbox: Dict[str, float] = {"x": 1, "y": 2, "w": 2, "h": 2}
        annotation = make_complex_polygon(class_name, points, bbox)

        assert_annoation_class(annotation, class_name, "complex_polygon",
                               "polygon")

        paths = annotation.data.get("paths")
        assert paths == points

        class_bbox = annotation.data.get("bounding_box")
        assert class_bbox == bbox
예제 #3
0
    def test_it_returns_annotation_with_default_params():
        class_name: str = "class_name"
        points: List[List[Point]] = [
            [{
                "x": 1,
                "y": 2
            }, {
                "x": 3,
                "y": 4
            }, {
                "x": 1,
                "y": 2
            }],
            [{
                "x": 4,
                "y": 5
            }, {
                "x": 6,
                "y": 7
            }, {
                "x": 4,
                "y": 5
            }],
        ]
        annotation = make_complex_polygon(class_name, points)

        assert_annoation_class(annotation, class_name, "complex_polygon",
                               "polygon")

        paths = annotation.data.get("paths")
        assert paths == points
예제 #4
0
def parse_annotation(
        annotation: Dict[str, Any],
        category_lookup_table: Dict[str, Any]) -> Optional[dt.Annotation]:
    category = category_lookup_table[annotation["category_id"]]
    segmentation = annotation["segmentation"]
    iscrowd = annotation.get("iscrowd") == 1

    if iscrowd:
        print("Warning, unsupported RLE, skipping")
        return None

    if len(segmentation) == 0 and len(annotation["bbox"]) == 4:
        x, y, w, h = map(int, annotation["bbox"])
        return dt.make_bounding_box(category["name"], x, y, w, h)
    elif len(segmentation) == 0 and len(annotation["bbox"]) == 1 and len(
            annotation["bbox"][0]) == 4:
        x, y, w, h = map(int, annotation["bbox"][0])
        return dt.make_bounding_box(category["name"], x, y, w, h)
    elif isinstance(segmentation, dict):
        print(
            "warning, converting complex coco rle mask to polygon, could take some time"
        )
        if isinstance(segmentation["counts"], list):
            mask = rle_decode(segmentation["counts"],
                              segmentation["size"][::-1])
        else:
            counts = decode_binary_rle(segmentation["counts"])
            mask = rle_decode(counts, segmentation["size"][::-1])

        _labels, external, _internal = find_contours(mask)
        paths = []
        for external_path in external:
            # skip paths with less than 2 points
            if len(external_path) // 2 <= 2:
                continue
            path = []
            points = iter(external_path)
            while True:
                try:
                    x, y = next(points), next(points)
                    path.append({"x": x, "y": y})
                except StopIteration:
                    break
            paths.append(path)
        return dt.make_complex_polygon(category["name"], paths)
    elif isinstance(segmentation, list):
        path = []
        points = iter(segmentation[0] if isinstance(segmentation[0], list
                                                    ) else segmentation)
        while True:
            try:
                x, y = next(points), next(points)
                path.append({"x": x, "y": y})
            except StopIteration:
                break
        return dt.make_polygon(category["name"], path)
    else:
        return None
예제 #5
0
def parse_annotation(annotation, category_lookup_table):
    category = category_lookup_table[annotation["category_id"]]
    segmentation = annotation["segmentation"]
    iscrowd = annotation.get("iscrowd") == 1

    if iscrowd:
        print("Warning, unsupported RLE, skipping")
        return None

    if len(segmentation) == 0 and len(annotation["bbox"]) == 4:
        x, y, w, h = map(int, annotation["bbox"])
        return dt.make_bounding_box(category["name"], x, y, w, h)
    elif len(segmentation) > 1:
        print("warning, converting complex coco rle mask to polygon, could take some time")
        mask = rle_decoding(segmentation["counts"], segmentation["size"])
        _labels, external, _internal = find_contours(mask)
        paths = []
        for external_path in external:
            path = []
            points = iter(external_path)
            while True:
                try:
                    x, y = next(points), next(points)
                    path.append({"x": x, "y": y})
                except StopIteration:
                    break
            paths.append(path)
        return dt.make_complex_polygon(category["name"], paths)
    elif len(segmentation) == 1:
        path = []
        points = iter(segmentation[0])
        while True:
            try:
                x, y = next(points), next(points)
                path.append({"x": x, "y": y})
            except StopIteration:
                break
        return dt.make_polygon(category["name"], path)
    else:
        return None