Skip to content

Commit

Permalink
Handle empty contour case
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jan 6, 2024
1 parent e618e49 commit 5a546a4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions labelme/ai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
import skimage

from labelme.logger import logger


def _get_contour_length(contour):
contour_start = contour
Expand All @@ -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(
Expand Down

0 comments on commit 5a546a4

Please # to comment.