def mask_to_image(mask):
    # Encoding classes with a colormap, see /usr/local/lib/python2.7/dist-packages/labelme/utils/_io.py
    assert mask.min() >= -1 and mask.max() < 255
    mask_pil = Image.fromarray(mask.astype(np.uint8), mode='P')
    colormap = label_colormap(255)
    mask_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
    return mask_pil
Exemplo n.º 2
0
def lblsave(filename, lbl):
    if osp.splitext(filename)[1] != '.png':
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        colormap = label_colormap(255)
        lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
        lbl_pil.save(filename)
Exemplo n.º 3
0
def lblsave(filename, lbl):
    if osp.splitext(filename)[1] != '.png':
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        colormap = label_colormap(255)
        lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
        lbl_pil.save(filename)
    else:
        logger.warn('[%s] Cannot save the pixel-wise class label as PNG, '
                    'so please use the npy file.' % filename)
Exemplo n.º 4
0
def lblsave(filename, lbl):
    if osp.splitext(filename)[1] != '.png':
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        colormap = label_colormap(255)
        lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
        lbl_pil.save(filename)
    else:
        raise ValueError('[%s] Cannot save the pixel-wise class label as PNG. '
                         'Please consider using the .npy format.' % filename)
Exemplo n.º 5
0
Arquivo: _io.py Projeto: MrHwc/labelme
def lblsave(filename, lbl):
    if osp.splitext(filename)[1] != '.png':
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        colormap = label_colormap(255)
        lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
        lbl_pil.save(filename)
    else:
        logger.warn(
            '[%s] Cannot save the pixel-wise class label as PNG, '
            'so please use the npy file.' % filename
        )
Exemplo n.º 6
0
def lblsave(filename, lbl):
    if osp.splitext(filename)[1] != '.png':
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        colormap = label_colormap(255)
        lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
        lbl_pil.save(filename)
    else:
        raise ValueError(
            '[%s] Cannot save the pixel-wise class label as PNG. '
            'Please consider using the .npy format.' % filename
        )
Exemplo n.º 7
0
def org_lbl_save(filename, org, label):
    if os.path.splitext(filename)[1] not in ['.png', '.PNG']:
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if label.min() >= -1 and label.max() < 255:

        org_img = PIL.Image.fromarray(org.astype(np.uint8), mode='RGB')
        lbl_pil = PIL.Image.fromarray(label.astype(np.uint8), mode='P')
        colormap = (label_colormap(255) * 255).astype(np.uint8)
        lbl_pil.putpalette(colormap.flatten())

        plt.figure(figsize=(13, 10))
        grid_spec = gridspec.GridSpec(2,
                                      2,
                                      width_ratios=[5, 5],
                                      height_ratios=[8, 2])

        plt.subplot(grid_spec[0])
        plt.imshow(org_img)
        plt.axis('off')
        plt.title('input image')

        plt.subplot(grid_spec[1])
        plt.imshow(lbl_pil)
        plt.axis('off')
        plt.title('GT(label image)')

        label_names, full_color_map = get_label_name_and_map(colormap)
        unique_labels = range(0, len(label_names))
        ax = plt.subplot(grid_spec[3])
        plt.imshow(full_color_map[unique_labels].astype(np.uint8),
                   interpolation='nearest')
        ax.yaxis.tick_right()
        plt.yticks(range(len(unique_labels)), label_names[unique_labels])
        plt.xticks([], [])
        ax.tick_params(width=0.0)
        plt.grid('off')

        plt.savefig(filename)
        plt.close()
    else:
        logger.warn('[%s] Cannot save the pixel-wise class label as PNG, '
                    'so please use the npy file.' % filename)
Exemplo n.º 8
0
def lblsave(filename, lbl, size=None):
    if os.path.splitext(filename)[1] not in ['.png', '.PNG']:
        filename += '.png'
    # Assume label ranses [-1, 254] for int32,
    # and [0, 255] for uint8 as VOC.
    if lbl.min() >= -1 and lbl.max() < 255:
        lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P')
        if size is not None:
            if isinstance(size, tuple):
                lbl_pil = lbl_pil.resize(size)
            else:
                raise AttributeError(
                    'size is not set properly. given size:{}'.format(size))

        colormap = (label_colormap(255) * 255).astype(np.uint8)
        lbl_pil.putpalette(colormap.flatten())
        lbl_pil.save(filename)
    else:
        logger.warn('[%s] Cannot save the pixel-wise class label as PNG, '
                    'so please use the npy file.' % filename)
Exemplo n.º 9
0
        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
        # 保存图片
        out_dir = os.path.join(os.path.abspath(os.path.join(json_path,os.path.pardir)), 'mask')
        if not os.path.exists(out_dir):
            os.mkdir(out_dir)
        save_name = os.path.join(out_dir, json_file.split('.')[0]+'.jpg')
        if MODE == 'BW':
            lbl_tmp = lbl * 255
            lbl_tmp[lbl_tmp>255] = 255
            lbl_pil = PIL.Image.fromarray(lbl_tmp.astype(np.uint8))
            lbl_pil.convert('1').save(save_name)
        elif MODE == 'RGB':
            lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8))
            colormap = label_colormap(255)
            lbl_pil.putpalette((colormap * 255).astype(np.uint8).flatten())
            lbl_pil.convert('RGB').save(save_name)
        else:
            print('save mode error!')
            sys.exit(0)

    print('Saved to: %s' % out_dir)

Exemplo n.º 10
0
def test_label_colormap():
    N = 255
    colormap = draw_module.label_colormap(N=N)
    assert colormap.shape == (N, 3)
Exemplo n.º 11
0
def test_label_colormap():
    N = 255
    colormap = draw_module.label_colormap(N=N)
    assert colormap.shape == (N, 3)