-
Notifications
You must be signed in to change notification settings - Fork 631
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
base: main
Are you sure you want to change the base?
Conversation
- 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,将原图添加到裁剪列表中 - 更新函数文档和注释以支持新功能
There was a problem hiding this 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################################################### |
There was a problem hiding this comment.
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.
###################################################sunhao################################################### | |
# Polygon repair functions |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
@SunHao-AI can you please fix the failing tests 🤗 |
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 |
ok |
@SunHao-AI, if you are already at it, you might just make the fix yourself, as you already posted the solution 😄 |
repair_polygon
andrepair_multipolygon
functions to repair invalid polygons and multipolygons.coco_segmentation_to_shapely
function to convert COCO format segmentation data into Shapely objects.get_union_polygon
function to handle empty polygons using the newly implemented conversion and repair methods.