Skip to content

Conversion from YOLO polygons to masks is inaccurate #2023

@StijnPruijssers

Description

@StijnPruijssers

Search before asking

  • I have searched the Supervision issues and found no similar bug report.

Bug

When loading in YOLO annotations with polygons from file the polygons are not parsed correctly to a mask due to integer casting.

When calling loading in YOLO polygon annotations to detections using yolo.py in supervision.datasets.formats the mask is not the same as the original mask used to create the polygon points. I tested this by converting a binary mask to polygons and then back to a mask. Theoretically this should be the same.

Original mask

Image

Reconstructed mask

Image

Difference image
Green = Pixels in original mask but not reconstructed mask, i.e. missing pixels
Red = Pixels in reconstructed mask but not original mask, i.e. extra pixels

Image

The error arises because in yolo.py on line 119 in the yolo_annotations_to_detections method. The

polygons = [
        (polygon * np.array(resolution_wh)).astype(int) for polygon in relative_polygon
    ]

This causes all float values in polygon to be floored due to the multiplication with an int typed array. Now all values with the first decimal > 5 are wrong. This causes a shift of the mask to the top left as in the example.

A simple np.round can solve this.

Environment

  • Supervision 0.27.0
  • Python 3.12
  • Windows 11

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • [] Yes I'd like to help by submitting a PR!
    When I get some time I want to but I do not have heaps right now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions