当前位置: 首页>>代码示例>>Python>>正文


Python mask.frPyObjects方法代码示例

本文整理汇总了Python中pycocotools.mask.frPyObjects方法的典型用法代码示例。如果您正苦于以下问题:Python mask.frPyObjects方法的具体用法?Python mask.frPyObjects怎么用?Python mask.frPyObjects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pycocotools.mask的用法示例。


在下文中一共展示了mask.frPyObjects方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __get_annotation__

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def __get_annotation__(self, mask, image=None):

        _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        segmentation = []
        for contour in contours:
            # Valid polygons have >= 6 coordinates (3 points)
            if contour.size >= 6:
                segmentation.append(contour.flatten().tolist())
        RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
        RLE = cocomask.merge(RLEs)
        # RLE = cocomask.encode(np.asfortranarray(mask))
        area = cocomask.area(RLE)
        [x, y, w, h] = cv2.boundingRect(mask)

        if image is not None:
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.drawContours(image, contours, -1, (0,255,0), 1)
            cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
            cv2.imshow("", image)
            cv2.waitKey(1)

        return segmentation, [x, y, w, h], area 
开发者ID:hazirbas,项目名称:coco-json-converter,代码行数:25,代码来源:generate_coco_json.py

示例2: annToRLE

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def annToRLE(self, ann, height, width):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        segm = ann['segmentation']
        if isinstance(segm, list):
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = maskUtils.frPyObjects(segm, height, width)
            rle = maskUtils.merge(rles)
        elif isinstance(segm['counts'], list):
            # uncompressed RLE
            rle = maskUtils.frPyObjects(segm, height, width)
        else:
            # rle
            rle = ann['segmentation']
        return rle 
开发者ID:dataiku,项目名称:dataiku-contrib,代码行数:20,代码来源:coco.py

示例3: polys_to_mask_wrt_box

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def polys_to_mask_wrt_box(polygons, box, M):
    """Convert from the COCO polygon segmentation format to a binary mask
    encoded as a 2D array of data type numpy.float32. The polygon segmentation
    is understood to be enclosed in the given box and rasterized to an M x M
    mask. The resulting mask is therefore of shape (M, M).
    """
    w = box[2] - box[0]
    h = box[3] - box[1]

    w = np.maximum(w, 1)
    h = np.maximum(h, 1)

    polygons_norm = []
    for poly in polygons:
        p = np.array(poly, dtype=np.float32)
        p[0::2] = (p[0::2] - box[0]) * M / w
        p[1::2] = (p[1::2] - box[1]) * M / h
        polygons_norm.append(p)

    rle = mask_util.frPyObjects(polygons_norm, M, M)
    mask = np.array(mask_util.decode(rle), dtype=np.float32)
    # Flatten in case polygons was a list
    mask = np.sum(mask, axis=2)
    mask = np.array(mask > 0, dtype=np.float32)
    return mask 
开发者ID:yihui-he,项目名称:KL-Loss,代码行数:27,代码来源:segms.py

示例4: to_mask

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def to_mask(polys, size):
    """Convert list of polygons to full size binary mask

    Parameters
    ----------
    polys : list of numpy.ndarray
        Numpy.ndarray with shape (N, 2) where N is the number of bounding boxes.
        The second axis represents points of the polygons.
        Specifically, these are :math:`(x, y)`.
    size : tuple
        Tuple of length 2: (width, height).

    Returns
    -------
    numpy.ndarray
        Full size binary mask of shape (height, width)
    """
    try_import_pycocotools()
    import pycocotools.mask as cocomask
    width, height = size
    polys = [p.flatten().tolist() for p in polys]
    rles = cocomask.frPyObjects(polys, height, width)
    rle = cocomask.merge(rles)
    return cocomask.decode(rle) 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:26,代码来源:mask.py

示例5: polys2mask_wrt_box

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def polys2mask_wrt_box(polygons, box, target_size):
        """Convert from the COCO polygon segmentation format to a binary mask
        encoded as a 2D array of data type numpy.float32. The polygon segmentation
        is understood to be enclosed in the given box and rasterized to an M x M
        mask. The resulting mask is therefore of shape (M, M).
        """
        w = box[2] - box[0]
        h = box[3] - box[1]

        w = np.maximum(w, 1)
        h = np.maximum(h, 1)

        polygons_norm = []
        for poly in polygons:
            p = np.array(poly, dtype=np.float32)
            p[0::2] = (p[0::2] - box[0]) * target_size[0] / w
            p[1::2] = (p[1::2] - box[1]) * target_size[1] / h
            polygons_norm.append(p)

        rle = mask_util.frPyObjects(polygons_norm, target_size[1], target_size[0])
        mask = np.array(mask_util.decode(rle), dtype=np.float32)
        # Flatten in case polygons was a list
        mask = np.sum(mask, axis=2)
        mask = np.array(mask > 0, dtype=np.float32)
        return mask 
开发者ID:openseg-group,项目名称:openseg.pytorch,代码行数:27,代码来源:mask_helper.py

示例6: polys_to_mask_wrt_box

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def polys_to_mask_wrt_box(polygons, box, M):
  """Convert from the COCO polygon segmentation format to a binary mask
    encoded as a 2D array of data type numpy.float32. The polygon segmentation
    is understood to be enclosed in the given box and rasterized to an M x M
    mask. The resulting mask is therefore of shape (M, M).
    """
  w = box[2] - box[0]
  h = box[3] - box[1]

  w = np.maximum(w, 1)
  h = np.maximum(h, 1)

  polygons_norm = []
  for poly in polygons:
    p = np.array(poly, dtype=np.float32)
    p[0::2] = (p[0::2] - box[0]) * M / w
    p[1::2] = (p[1::2] - box[1]) * M / h
    polygons_norm.append(p)

  rle = mask_util.frPyObjects(polygons_norm, M, M)
  mask = np.array(mask_util.decode(rle), dtype=np.float32)
  # Flatten in case polygons was a list
  mask = np.sum(mask, axis=2)
  mask = np.array(mask > 0, dtype=np.float32)
  return mask 
开发者ID:roytseng-tw,项目名称:Detectron.pytorch,代码行数:27,代码来源:segms.py

示例7: annToRLE

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def annToRLE(ann, h, w):
    """
    Convert annotation which can be polygons, uncompressed RLE to RLE.
    :return: binary mask (numpy 2D array)
    """
    segm = ann['segmentation']
    if type(segm) == list:
        # polygon -- a single object might consist of multiple parts
        # we merge all parts into one mask rle code
        rles = maskUtils.frPyObjects(segm, h, w)
        rle = maskUtils.merge(rles)
    elif type(segm['counts']) == list:
        # uncompressed RLE
        rle = maskUtils.frPyObjects(segm, h, w)
    else:
        # rle
        rle = ann['segmentation']
    return rle 
开发者ID:alicranck,项目名称:coco2voc,代码行数:20,代码来源:coco2voc_aux.py

示例8: annToRLE

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = maskUtils.frPyObjects(segm, h, w)
            rle = maskUtils.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = maskUtils.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 
开发者ID:mlperf,项目名称:training_results_v0.5,代码行数:22,代码来源:coco.py

示例9: _segm_to_mask

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def _segm_to_mask(self, segm, size):
        # Copied from pycocotools.coco.COCO.annToMask
        H, W = size
        if isinstance(segm, list):
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            mask = np.zeros((H, W), dtype=np.uint8)
            mask = PIL.Image.fromarray(mask)
            for sgm in segm:
                xy = np.array(sgm).reshape((-1, 2))
                xy = [tuple(xy_i) for xy_i in xy]
                PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
            mask = np.asarray(mask)
        elif isinstance(segm['counts'], list):
            rle = coco_mask.frPyObjects(segm, H, W)
            mask = coco_mask.decode(rle)
        else:
            mask = coco_mask.decode(segm)
        return mask.astype(np.bool) 
开发者ID:chainer,项目名称:chainercv,代码行数:21,代码来源:coco_instances_base_dataset.py

示例10: annToRLE

# 需要导入模块: from pycocotools import mask [as 别名]
# 或者: from pycocotools.mask import frPyObjects [as 别名]
def annToRLE(self, ann, height, width):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        segm = ann['segmentation']
        if isinstance(segm, list):
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, height, width)
            rle = mask.merge(rles)
        elif isinstance(segm['counts'], list):
            # uncompressed RLE
            rle = mask.frPyObjects(segm, height, width)
        else:
            # rle
            rle = ann['segmentation']
        return rle 
开发者ID:nearthlab,项目名称:image-segmentation,代码行数:20,代码来源:coco_dataset.py


注:本文中的pycocotools.mask.frPyObjects方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。