in gt_converter/convert_coco.py [0:0]
def _create_submask_annotation(sub_mask, image_id, category_id, annotation_id):
"""
Find contours (boundary lines) around each sub-mask
Note: there could be multiple contours if the object is partially occluded. (E.g. an elephant behind a tree)
:param sub_mask: Numpy array of single labels masks
:param image_id: Integer ID specifying which image this is
:param category_id: Label ID being processed
:param annotation_id: Integer ID specifying which annotation this is
:return: Dictionary of COCO annotation
"""
contours = measure.find_contours(sub_mask, 0.5, positive_orientation="low")
segmentations = []
polygons = []
for contour in contours:
# Flip from (row, col) representation to (x, y) and subtract the padding pixel
for i in range(len(contour)):
row, col = contour[i]
contour[i] = (col - 1, row - 1)
# Make a polygon and simplify it
poly = Polygon(contour)
poly = poly.simplify(1.0, preserve_topology=False)
polygons.append(poly)
segmentation = np.array(poly.exterior.coords).ravel().tolist()
segmentations.append(segmentation)
# Combine the polygons to calculate the bounding box and area
multi_poly = MultiPolygon(polygons)
x, y, max_x, max_y = multi_poly.bounds
width = max_x - x
height = max_y - y
bbox = (x, y, width, height)
area = multi_poly.area
annotation = {
"segmentation": segmentations,
"iscrowd": 0,
"image_id": image_id,
"category_id": category_id,
"id": annotation_id,
"bbox": bbox,
"area": area,
}
return annotation