How to use the labelme.utils.shape_to_mask function in labelme

To help you get started, we’ve selected a few labelme examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wkentaro / labelme / tests / test_utils.py View on Github external
def test_shape_to_mask():
    img, data = _get_img_and_data()
    for shape in data['shapes']:
        points = shape['points']
        mask = labelme.utils.shape_to_mask(img.shape[:2], points)
        assert mask.shape == img.shape[:2]
github wkentaro / labelme / examples / instance_segmentation / labelme2coco.py View on Github external
license=0,
            url=None,
            file_name=osp.relpath(out_img_file, osp.dirname(out_ann_file)),
            height=img.shape[0],
            width=img.shape[1],
            date_captured=None,
            id=image_id,
        ))

        masks = {}                                     # for area
        segmentations = collections.defaultdict(list)  # for segmentation
        for shape in label_data['shapes']:
            points = shape['points']
            label = shape['label']
            shape_type = shape.get('shape_type', None)
            mask = labelme.utils.shape_to_mask(
                img.shape[:2], points, shape_type
            )

            if label in masks:
                masks[label] = masks[label] | mask
            else:
                masks[label] = mask

            points = np.asarray(points).flatten().tolist()
            segmentations[label].append(points)

        for label, mask in masks.items():
            cls_name = label.split('-')[0]
            if cls_name not in class_name_to_id:
                continue
            cls_id = class_name_to_id[cls_name]
github veraposeidon / labelme2Datasets / dataset_split_region.py View on Github external
:param shapes:
    :param label_name_to_value:
    :return:
    """
    cls = np.zeros(img_shape[:2], dtype=np.int32)
    for shape in shapes:
        xmin = shape['bndbox'][0]
        ymin = shape['bndbox'][1]
        xmax = shape['bndbox'][2]
        ymax = shape['bndbox'][3]
        points = [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]
        label = shape['name']
        shape_type = shape.get('shape_type', None)
        cls_name = label
        cls_id = label_name_to_value[cls_name]
        mask = shape_to_mask(img_shape[:2], points, shape_type)
        cls[mask] = cls_id
    return cls