Пример #1
0
def encode(bimask, binary=False):
    if len(bimask.shape) == 3:
        return _mask.encode(bimask, binary=binary)
    elif len(bimask.shape) == 2:
        h, w = bimask.shape
        return _mask.encode(bimask.reshape((h, w, 1), order='F'),
                            binary=binary)[0]
Пример #2
0
def encode_binary_mask(mask: np.ndarray) -> t.Text:
    """Converts a binary mask into OID challenge encoding ascii text."""

    # check input mask --
    if mask.dtype != np.bool:
        raise ValueError(
            "encode_binary_mask expects a binary mask, received dtype == %s" %
            mask.dtype)

    mask = np.squeeze(mask)
    if len(mask.shape) != 2:
        raise ValueError(
            "encode_binary_mask expects a 2d mask, received shape == %s" %
            mask.shape)

    # convert input mask to expected COCO API input --
    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
    mask_to_encode = mask_to_encode.astype(np.uint8)
    mask_to_encode = np.asfortranarray(mask_to_encode)

    # RLE encode mask --
    encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]

    # compress and base64 encoding --
    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
    base64_str = base64.b64encode(binary_str)
    return base64_str
Пример #3
0
def polygons_to_bitmask(polygons: List[np.ndarray], height: int,
                        width: int) -> np.ndarray:
    """
    Args:
        polygons (list[ndarray]): each array has shape (Nx2,)
        height, width (int)

    Returns:
        ndarray: a bool mask of shape (height, width)
    """
    assert len(polygons) > 0, "COCOAPI does not support empty polygons"
    rles = mask_util.frPyObjects(polygons, height, width)
    if len(rles) >= 2:
        import pycocotools._mask as _mask
        rle = _mask.decode(rles)
        for i in range(rle.shape[2] - 1):
            if i == 0:
                temp = rle[:, :, i]
            temp = np.logical_xor(temp, rle[:, :, i + 1])
        rle = temp.astype('uint8').reshape([28, 28, 1])
        rle = _mask.encode(rle)
        rle = mask_util.merge(rle)
    else:
        rle = mask_util.merge(rles)

    return mask_util.decode(rle).astype(np.bool)
Пример #4
0
def convert_mask_to_format(mask):
    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
    mask_to_encode = mask_to_encode.astype(np.uint8)
    mask_to_encode = np.asfortranarray(mask_to_encode)

    # RLE encode mask --
    encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]

    # compress and base64 encoding --
    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
    base64_str = base64.b64encode(binary_str)
    return base64_str
Пример #5
0
def encode_binary_mask(mask, mask_id): #contour, image_shape):
  """Converts a binary mask into OID challenge encoding ascii text."""
  mask = np.where(mask==mask_id, 1, 0).astype(np.bool)
  # check input mask --
  if mask.dtype != np.bool:
      raise ValueError(f"encode_binary_mask expects a binary mask, received dtype == {mask.dtype}")

  mask = np.squeeze(mask)
  assert len(mask.shape) == 2

  # convert input mask to expected COCO API input --
  mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
  mask_to_encode = mask_to_encode.astype(np.uint8)
  mask_to_encode = np.asfortranarray(mask_to_encode)

  # RLE encode mask --
  encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]

  # compress and base64 encoding --
  binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
  base64_str = base64.b64encode(binary_str)
  return base64_str.decode() #'ascii'
Пример #6
0
def encode(bimask):
    if len(bimask.shape) == 3:
        return _mask.encode(bimask)
    elif len(bimask.shape) == 2:
        h, w = bimask.shape
        return _mask.encode(bimask.reshape((h, w, 1), order='F'))[0]
def gen_new_validation_csv(seg_file, bbox_file, output_file):
    # First get "isGroupOf" information. Key is ImageID, LabelName, XMin (rounded to 2 decimals) concatenated together
    bbox_group_dict = {}
    with open(bbox_file, 'r') as f:
        next(f)  # Skip header line
        for line in f:
            image_id, label_name, x_min, _, _, _, is_group_of = line.strip(
            ).split(',')
            # Rounding is necessary because the decimal precision of the bounding box coordinates differs between files...
            key = image_id + '_' + label_name + '_' + str(
                round(float(x_min), 2))
            bbox_group_dict[key] = is_group_of
    print('Generated IsGroupOf dictionary.')

    f_out = open(output_file, 'w')
    header = 'ImageID,LabelName,ImageWidth,ImageHeight,XMin,YMin,XMax,YMax,Score,Mask'
    f_out.write(header + '\n')

    counter = 0
    with open(seg_file, 'r') as f:
        next(f)  # Skip header line
        for line in f:
            mask_name, image_id, label_name, _, x_min, y_min, x_max, y_max, _, _ = line.strip(
            ).split(',')
            # Form full mask and image path. Assuming data is structured per the readme.
            mask_folder_prefix = mask_name[0]
            mask_path = '/home/dfan/datasets/open_images_segmentation/masks/validation/validation-masks-{}/{}'.format(
                mask_folder_prefix,
                mask_name)  # e.g. validation-masks-a/blahblah.png
            image_path = '/home/dfan/datasets/open_images_segmentation/images/validation/{}.jpg'.format(
                image_id)

            # Get image width and height
            im = cv2.imread(image_path)
            height = im.shape[0]
            width = im.shape[1]

            # Figure out IsGroupOf value
            # Rounding is necessary because the decimal precision of the bounding box coordinates differs between files...
            dict_key = image_id + '_' + label_name + '_' + str(
                round(float(x_min), 2))
            is_group_of = bbox_group_dict[dict_key]

            # convert input mask to expected COCO API input --
            mask = cv2.imread(mask_path, 0)
            mask = mask.reshape(mask.shape[0], mask.shape[1], 1)
            mask = mask.astype(np.uint8)
            mask = np.asfortranarray(mask)

            # RLE encode mask
            encoded_mask = str(coco_mask.encode(mask)[0]["counts"], 'utf-8')

            # Write ImageID,LabelName,ImageWidth,ImageHeight,XMin,YMin,XMax,YMax,IsGroupOf,Mask
            f_out.write(image_id + ',' + label_name + ',' + str(width) + ',' +
                        str(height) + ',' + x_min + ',' + y_min + ',' + x_max +
                        ',' + y_max + ',' + is_group_of + ',' + encoded_mask +
                        '\n')

            counter += 1
            if counter % 1000 == 0:
                print('Processed {} masks.'.format(counter))

    f_out.close()
    print('Successfully combined validation mask information into CSV format!')
Пример #8
0
def encode(bimask):
    if len(bimask.shape) == 3:
        return _mask.encode(bimask)
    elif len(bimask.shape) == 2:
        h, w = bimask.shape
        return _mask.encode(bimask.reshape((h, w, 1), order='F'))[0]