(binary_mask, tolerance=0)
return polygons
Converts a binary mask to COCO polygon representation

    Args:
        binary_mask: a 2D binary numpy array where '1's represent the object
        tolerance: Maximum distance from original points of polygon to approximated
            polygonal chain. If tolerance is 0, the original coordinate array is returned.
Converts a binary mask to COCO polygon representation
Converts a binary mask to COCO polygon representation
def binary_mask_to_polygon(binary_mask, tolerance=0):
    """Converts a binary mask to COCO polygon representation

    Args:
        binary_mask: a 2D binary numpy array where '1's represent the object
        tolerance: Maximum distance from original points of polygon to approximated
            polygonal chain. If tolerance is 0, the original coordinate array is returned.

    """
    polygons = []
    # pad mask to close contours of shapes which start and end at an edge
    padded_binary_mask = np.pad(binary_mask, pad_width=1, mode='constant', constant_values=0)
    contours = measure.find_contours(padded_binary_mask, 0.5)
    contours = np.subtract(contours, 1)
    for contour in contours:
        contour = close_contour(contour)
        contour = measure.approximate_polygon(contour, tolerance)
        if len(contour) < 3:
            continue
        contour = np.flip(contour, axis=1)
        segmentation = contour.ravel().tolist()
        # after padding and subtracting 1 we may get -0.5 points in our segmentation 
        segmentation = [0 if i < 0 else i for i in segmentation]
        polygons.append(segmentation)

    return polygons
def binary_mask_to_polygon ( binary_mask , tolerance = 0 ) : polygons = [ ] # pad mask to close contours of shapes which start and end at an edge padded_binary_mask = np . pad ( binary_mask , pad_width = 1 , mode = 'constant' , constant_values = 0 ) contours = measure . find_contours ( padded_binary_mask , 0.5 ) contours = np . subtract ( contours , 1 ) for contour in contours : contour = close_contour ( contour ) contour = measure . approximate_polygon ( contour , tolerance ) if len ( contour ) < 3 : continue contour = np . flip ( contour , axis = 1 ) segmentation = contour . ravel ( ) . tolist ( ) # after padding and subtracting 1 we may get -0.5 points in our segmentation  segmentation = [ 0 if i < 0 else i for i in segmentation ] polygons . append ( segmentation ) return polygons