Skip to content

Commit 5041e07

Browse files
author
Matt Sokoloff
committed
ensure valid polygon for metrics
1 parent ff0bea4 commit 5041e07

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

labelbox/data/metrics/iou/calculation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,18 @@ def _get_mask_pairs(
254254

255255
def _polygon_iou(poly1: Polygon, poly2: Polygon) -> ScalarMetricValue:
256256
"""Computes iou between two shapely polygons."""
257+
poly1, poly2 = _ensure_valid_poly(poly1), _ensure_valid_poly(poly2)
257258
if poly1.intersects(poly2):
258259
return poly1.intersection(poly2).area / poly1.union(poly2).area
259260
return 0.
260261

261262

263+
def _ensure_valid_poly(poly):
264+
if not poly.is_valid:
265+
return poly.buffer(0)
266+
return poly
267+
268+
262269
def _mask_iou(mask1: np.ndarray, mask2: np.ndarray) -> ScalarMetricValue:
263270
"""Computes iou between two binary segmentation masks."""
264271
return np.sum(mask1 & mask2) / np.sum(mask1 | mask2)

0 commit comments

Comments
 (0)