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

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

SunHao-AI
Copy link

  • 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 and others added 4 commits January 10, 2025 10:08
- 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.
- 在 slice_image 和 SliceDataset 中添加 inference_org_image 参数- 如果 inference_org_image 为 True,将原图添加到裁剪列表中
- 更新函数文档和注释以支持新功能
@fcakyon fcakyon requested a review from Copilot March 5, 2025 01:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR fixes issues related to invalid and empty polygons in SAHI by adding repair functions and converting COCO segmentation data into valid Shapely objects. It also enhances the union logic in mask processing and adds an inference image flag to the slicing functions.

  • Added repair_polygon, repair_multipolygon, and coco_segmentation_to_shapely functions for polygon corrections.
  • Updated get_merged_mask to handle empty polygons using the new conversion methods.
  • Introduced an inference_org_image parameter to slicing functions to optionally include the original image in the output.

Reviewed Changes

File Description
sahi/postprocess/utils.py Added functions to repair invalid polygons and update union mask logic.
sahi/slicing.py Added and propagated the inference_org_image parameter in slicing logic.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

sahi/slicing.py:89

  • [nitpick] Consider using consistent naming for coordinate variables (e.g., use 'x_max' instead of 'xmax') to maintain clarity across the code.
xmax = min(image_width, x_max)

sahi/postprocess/utils.py:245

  • [nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.
###################################################sunhao###################################################

@@ -64,6 +65,81 @@ def tolist(self):
else:
return self.list

###################################################sunhao###################################################
Copy link
Preview

Copilot AI Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.

Suggested change
###################################################sunhao###################################################
# Polygon repair functions

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@fcakyon
Copy link
Collaborator

fcakyon commented Mar 5, 2025

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

@mario-dg
Copy link

@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?

@SunHao-AI
Copy link
Author

您能否修复失败的测试 🤗

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
Copy link
Author

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

ok

@mario-dg
Copy link

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

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

Successfully merging this pull request may close these issues.

3 participants