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

coco_segm_annotation_to_normalized_vertices does not handle nested segmentation field #1751

Open
robmarkcole opened this issue Jul 24, 2024 · 1 comment

Comments

@robmarkcole
Copy link

robmarkcole commented Jul 24, 2024

In the importing_coco.ipynb example, the function coco_segm_annotation_to_normalized_vertices doesn't handle nested segmentation. This is demonstrated below:

def test_coco_segm_annotation_to_normalized_vertices():
    # Sample COCO annotation data
    coco_ann = {
        "segmentation": [
                [
                    [
                        1842.8,
                        1322.2,
                        1848.1,
                        1320.2,
                        1845,
                        1311.8,
                        1839.7,
                        1313.8
                    ]
                ]
            ],
        "iscrowd": 0
    }
    img_width = 8000  # Example image width
    img_height = 6000  # Example image height

    normalized_vertices = coco_segm_annotation_to_normalized_vertices(
        coco_ann=coco_ann,
        img_width=img_width,
        img_height=img_height
    )

    print("Normalized Vertices:")
    for vertices in normalized_vertices:
        for vertex in vertices:
            print(vertex)

test_coco_segm_annotation_to_normalized_vertices()

Ideally I would be able to upload cocojson to the platform, but in the meantime I suggest improving this example

@robmarkcole
Copy link
Author

Updated to handle nested segmentation:

def coco_segm_annotation_to_normalized_vertices(coco_ann, *, img_width, img_height):
    """
    Convert a COCO segmentation annotation to normalized vertices.
    Handles segmentation data that is a list of list of list (nested list for each polygon).
    """
    coco_segmentations = coco_ann["segmentation"]
    ret = []
    for coco_segm_group in coco_segmentations:  # Additional loop to handle extra nesting
        for coco_segm in coco_segm_group:
            if coco_ann["iscrowd"] == 0:
                # a single object (iscrowd=0) where polygons are used
                vertices = [
                    point_to_normalized_point(
                        point={"x": x, "y": y},
                        img_width=img_width,
                        img_height=img_height,
                        origin_location="top_left",
                    )
                    for x, y in zip(coco_segm[::2], coco_segm[1::2])
                ]
                ret.append(vertices)
    return ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant