fix(sahi): Fix Polygon Repair and Empty Polygon Issues
- Added
repair_polygonandrepair_multipolygonfunctions to repair invalid polygons and multipolygons. - Implemented
coco_segmentation_to_shapelyfunction to convert COCO format segmentation data into Shapely objects. - Enhanced the
get_union_polygonfunction to handle empty polygons using the newly implemented conversion and repair methods.
@SunHao-AI can you please fix the failing tests 🤗
@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?
您能否修复失败的测试 🤗
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
,我需要这个修复程序,因为它可能也解决了 #1094,但实现了更简洁。我应该打开一个新的 PR,实施更改并确保测试正在运行吗?
ok
@SunHao-AI, if you are already at it, you might just make the fix yourself, as you already posted the solution 😄
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.