-
Notifications
You must be signed in to change notification settings - Fork 592
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
Another self-intersection corner case handling #982
Conversation
Hi @sergiev thank you for your contribution, I also had a chance to look at previous related PR. Could you maybe add some test data and extend the tests for shapely_utils ? I think that way we would've more control over these utilities. |
@devrimcavusoglu hi, thanks for the reply! If it's the suggestion to extend test_shapelyutils.py with some kind of |
Hi @sergiev. Yes, I exactly meant that, ofc. this cases are visible in PRs, but it would be nice also to track them in the code as well, so that people contributing to the package know that these cases are filed, and they may be able to add more to that test case. |
Hey @sergiev, thanks for your fantastic contribution! Do you plan to add some tests for your case into test_shapelyutils.py? |
Thank you @fcakyon for reminding! I plan to find time for it in a week, will update this PR branch before June, if you let me :) |
what do you think about this implementation @sergiev? It fixes the same problem in a different way. |
hi @fcakyon! def get_shapely_multipolygon(coco_segmentation: List[List]) -> MultiPolygon:
"""
Accepts coco style polygon coords and converts it to valid shapely multipolygon object
"""
def filter_polygons(geometry):
"""
Filters out and returns only Polygon or MultiPolygon components of a geometry.
If geometry is a Polygon, it converts it into a MultiPolygon.
If it's a GeometryCollection, it filters to create a MultiPolygon from any Polygons in the collection.
Returns an empty MultiPolygon if no Polygon or MultiPolygon components are found.
"""
if isinstance(geometry, Polygon):
return MultiPolygon([geometry])
elif isinstance(geometry, MultiPolygon):
return geometry
elif isinstance(geometry, GeometryCollection):
polygons = [geom for geom in geometry.geoms if isinstance(geom, Polygon)]
return MultiPolygon(polygons) if polygons else MultiPolygon()
return MultiPolygon()
polygon_list = []
for coco_polygon in coco_segmentation:
point_list = list(zip(coco_polygon[0::2], coco_polygon[1::2]))
shapely_polygon = Polygon(point_list)
polygon_list.append(shapely_polygon)
shapely_multipolygon = MultiPolygon(polygon_list)
if not shapely_multipolygon.is_valid:
shapely_multipolygon = filter_polygons(make_valid(shapely_multipolygon))
return shapely_multipolygon |
That makes a lot of sense! Would you be open to creating a pull request for a more general solution based on your suggestion? @sergiev |
Hi @fcakyon! I will be happy to. Don't you mind if I put the suggestion in this PR? |
Yes would be great! |
Hi @fcakyon i've just added everything discussed above but can't pass CI, though formatting with black. What I am missing? |
Hi @fcakyon! |
CI checks passed |
Hello again! This time I faced problem that seem very similar to the one from my previous PR #961
The neighboring points placed here in such order that gives us another invalid shape.
But this time my previous fix hadn't helped:
If you try to get intersection of original shape, it leads to empty intersection, which is the least expected behaviour (for me, at least).
So this time the solution is a bit tradeoff: you're not getting original shape, but only convex hull of it. If there's more accurate solution, I'll be happy to see it implemented in SAHI.
Below are information required to reproduce the bug:
Input image shape: h=2060, w=3790
slice_coco args: slice_height=slice_width=1280, overlap_height_ratio=overlap_width_ratio=0.02
Example of slightly corrupted annotation