sahi icon indicating copy to clipboard operation
sahi copied to clipboard

fix(sahi): Fix Polygon Repair and Empty Polygon Issues

Open SunHao-AI opened this issue 11 months ago • 5 comments

  • Added repair_polygon and repair_multipolygon functions to repair invalid polygons and multipolygons.
  • Implemented coco_segmentation_to_shapely function to convert COCO format segmentation data into Shapely objects.
  • Enhanced the get_union_polygon function to handle empty polygons using the newly implemented conversion and repair methods.

SunHao-AI avatar Jan 22 '25 01:01 SunHao-AI

@SunHao-AI can you please fix the failing tests 🤗

fcakyon avatar Mar 05 '25 01:03 fcakyon

@fcakyon, I need this fix, as it probably also addresses #1094 but with a cleaner implementation. Should I open a new PR, implement the changes and make sure the tests are running?

mario-dg avatar Mar 11 '25 07:03 mario-dg

您能否修复失败的测试 🤗

def coco_segmentation_to_shapely(
    segmentation: Union[List, List[List]]
):
    """
    Fix segment data in COCO format

    :param segmentation: segment data in COCO format
    :return:
    """
    if isinstance(segmentation, List) and all([not isinstance(seg, List) for seg in segmentation]):
        segmentation = [segmentation]
    elif isinstance(segmentation, List) and all([isinstance(seg, List) for seg in segmentation]):
        pass
    else:
        raise ValueError("segmentation must be list or list[list]")

    polygon_list = []

    for coco_polygon in segmentation:
        point_list = list(zip(coco_polygon[::2], coco_polygon[1::2]))
        shapely_polygon = Polygon(point_list)
        polygon_list.append(repair_polygon(shapely_polygon))

    shapely_multipolygon = repair_multipolygon(MultiPolygon(polygon_list))
    return shapely_multipolygon

SunHao-AI avatar Mar 12 '25 05:03 SunHao-AI

,我需要这个修复程序,因为它可能也解决了 #1094,但实现了更简洁。我应该打开一个新的 PR,实施更改并确保测试正在运行吗?

ok

SunHao-AI avatar Mar 12 '25 05:03 SunHao-AI

@SunHao-AI, if you are already at it, you might just make the fix yourself, as you already posted the solution 😄

mario-dg avatar Mar 12 '25 06:03 mario-dg

The first checks fail because of the type annotation, i.e. tuple is not available in older python versions as a type annotation. You can either use Tuple from the typing package as has been done in the rest of the codebase, or have to implement fallbacks for older python versions.

gboeer avatar Jul 01 '25 10:07 gboeer