From 3252a738ac78a6fba276ef58e8adca4f04619116 Mon Sep 17 00:00:00 2001 From: Gorkem Polat Date: Thu, 15 Jun 2023 15:38:16 +0100 Subject: [PATCH] fix: filter polygon objects according to their complexity --- src/encord_active/lib/common/utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/encord_active/lib/common/utils.py b/src/encord_active/lib/common/utils.py index 33802aeb0..0b8511f57 100644 --- a/src/encord_active/lib/common/utils.py +++ b/src/encord_active/lib/common/utils.py @@ -26,7 +26,7 @@ from loguru import logger from PIL import Image from shapely.errors import ShapelyDeprecationWarning -from shapely.geometry import Polygon +from shapely.geometry import MultiPolygon, Polygon from tqdm.auto import tqdm from encord_active.lib.labels.object import BoxShapes, ObjectShape, SimpleShapes @@ -160,8 +160,17 @@ def get_geometry_from_encord_object(obj: dict, w: int, h: int) -> Optional[np.nd polygon = get_polygon(obj, force_simple_polygons=False) if polygon: + polygon_buffered = polygon.buffer(0) + if isinstance(polygon_buffered, MultiPolygon): + if abs(polygon_buffered.area - polygon.area) < 1e-6: + final_polygon = polygon + else: + return None + else: + final_polygon = polygon_buffered + img_size = np.array([[w, h]]) - return (np.array(polygon.exterior.xy).T * img_size).astype(int) + return (np.array(final_polygon.exterior.xy).T * img_size).astype(int) else: return None