From 5a546a49abce7d8101a89dbf71bb5353072f0876 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Sat, 6 Jan 2024 22:31:23 +0900 Subject: [PATCH] Handle empty contour case --- labelme/ai/_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/labelme/ai/_utils.py b/labelme/ai/_utils.py index 2619ac137..6806a5b06 100644 --- a/labelme/ai/_utils.py +++ b/labelme/ai/_utils.py @@ -2,6 +2,8 @@ import numpy as np import skimage +from labelme.logger import logger + def _get_contour_length(contour): contour_start = contour @@ -11,6 +13,10 @@ def _get_contour_length(contour): def compute_polygon_from_mask(mask): contours = skimage.measure.find_contours(np.pad(mask, pad_width=1)) + if len(contours) == 0: + logger.warning("No contour found, so returning empty polygon.") + return np.empty((0, 2), dtype=np.float32) + contour = max(contours, key=_get_contour_length) POLYGON_APPROX_TOLERANCE = 0.004 polygon = skimage.measure.approximate_polygon(