本文整理汇总了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
示例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
示例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
示例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)
示例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
示例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
示例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
示例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
示例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)
示例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