Exemplo n.º 1
0
 def _toMask(objs, coco):
     # modify segmentation by reference
     for obj in objs:
         t = coco.imgs[obj['image_id']]
         for region in obj['regions']:
             if type(region['segmentation']) == list:
                 # format is xyxy, convert to RLE
                 region['segmentation'] = mask.frPyObjects(
                     [region['segmentation']], t['height'], t['width'])
                 if len(region['segmentation']) == 1:
                     region['segmentation'] = region['segmentation'][0]
                 else:
                     # an object can have multiple polygon regions
                     # merge them into one RLE mask
                     region['segmentation'] = mask.merge(
                         obj['segmentation'])
                 if 'area' not in region:
                     region['area'] = mask.area(
                         [region['segmentation']])[0]
             elif type(region['segmentation']) == dict and type(
                     region['segmentation']['counts']) == list:
                 region['segmentation'] = mask.frPyObjects(
                     [region['segmentation']], t['height'],
                     t['width'])[0]
             elif type(region['segmentation']) == dict and \
                 type(region['segmentation']['counts'] == unicode or type(region['segmentation']['counts']) == str):
                 # format is already RLE, do nothing
                 if 'area' not in region:
                     region['area'] = mask.area(
                         [region['segmentation']])[0]
             else:
                 raise Exception('segmentation format not supported.')
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
def annToMask(segm, h, w):
    """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """

    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 = segm

    return maskUtils.decode(rle)
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 def _toMask(objs, coco):
     # modify segmentation by reference
     for obj in objs:
         t = coco.imgs[obj['image_id']]
         if type(obj['segmentation']) == list:
             if type(obj['segmentation'][0]) == dict:
                 print 'debug'
             obj['segmentation'] = mask.frPyObjects(obj['segmentation'],t['height'],t['width'])
             if len(obj['segmentation']) == 1:
                 obj['segmentation'] = obj['segmentation'][0]
             else:
                 # an object can have multiple polygon regions
                 # merge them into one RLE mask
                 obj['segmentation'] = mask.merge(obj['segmentation'])
         elif type(obj['segmentation']) == dict and type(obj['segmentation']['counts']) == list:
             obj['segmentation'] = mask.frPyObjects([obj['segmentation']],t['height'],t['width'])[0]
         elif type(obj['segmentation']) == dict and \
              type(obj['segmentation']['counts'] == unicode or type(obj['segmentation']['counts']) == str):
             pass
         else:
             raise Exception('segmentation format not supported.')
Exemplo n.º 6
0
 def _toMask(objs, coco):
     # modify segmentation by reference
     for obj in objs:
         t = coco.imgs[obj['image_id']]
         if type(obj['segmentation']) == list:
             if type(obj['segmentation'][0]) == dict:
                 print('debug')
             obj['segmentation'] = mask.frPyObjects(obj['segmentation'],t['height'],t['width'])
             if len(obj['segmentation']) == 1:
                 obj['segmentation'] = obj['segmentation'][0]
             else:
                 # an object can have multiple polygon regions
                 # merge them into one RLE mask
                 obj['segmentation'] = mask.merge(obj['segmentation'])
         elif type(obj['segmentation']) == dict and type(obj['segmentation']['counts']) == list:
             obj['segmentation'] = mask.frPyObjects([obj['segmentation']],t['height'],t['width'])[0]
         elif type(obj['segmentation']) == dict and \
              type(obj['segmentation']['counts'] == unicode or type(obj['segmentation']['counts']) == str):
             pass
         else:
             raise Exception('segmentation format not supported.')
Exemplo n.º 7
0
 def _toMask(objs, coco):
     # modify segmentation by reference
     for obj in objs:
         t = coco.imgs[obj['image_id']]
         for region in obj['regions']:
             if type(region['segmentation']) == list:
                 # format is xyxy, convert to RLE
                 region['segmentation'] = mask.frPyObjects(
                     [region['segmentation']], t['height'], t['width'])
                 region['segmentation'][0]['counts'] = region[
                     'segmentation'][0]['counts'].decode('utf-8')
                 if len(region['segmentation']) == 1:
                     region['segmentation'] = region['segmentation'][0]
                 else:
                     # an object can have multiple polygon regions
                     # merge them into one RLE mask
                     region['segmentation'] = mask.merge(
                         obj['segmentation'])
                 if 'area' not in region:
                     region['area'] = mask.area(
                         [region['segmentation']])[0]
             elif type(region['segmentation']) == dict and type(
                     region['segmentation']['counts']) == list:
                 region['segmentation'] = mask.frPyObjects(
                     [region['segmentation']], t['height'],
                     t['width'])[0]
                 region['segmentation']['counts'] = region[
                     'segmentation']['counts'].decode('utf-8')
             elif type(region['segmentation']) == dict and \
                 type(region['segmentation']['counts']) == str or type(region['segmentation']['counts']) == bytes:
                 # Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.
                 # format is already RLE, do nothing
                 if 'area' not in region:
                     region['area'] = mask.area(
                         [region['segmentation']])[0]
             else:
                 print('???')
                 raise Exception('segmentation format not supported.')