Пример #1
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)
Пример #2
0
def createInstanceImage_old(annotations, img):
	# create and draw annotation mask.
	semImg = np.zeros((img['height'], img['width']), dtype = np.uint8)
	for ann in annotations:
		# pdb.set_trace()
		catgr_id = ann['category_id']
		# c = ([catgr_id, catgr_id,catgr_id]).tolist()[0]
		if type(ann['segmentation']) == list:
			# polygon
			for seg in ann['segmentation']:
				seg = [round(elem, 0) for elem in seg] # rounding
				seg = map(int, seg) # convert to int, floor
				poly = np.array(seg).reshape((len(seg)/2), 2)

				canvas = np.zeros((img['height'],img['width'],1), dtype = int)
				poly = poly.tolist()
				poly = [tuple([elem[1],elem[0]]) for elem in poly]
				mahotas.polygon.fill_polygon(poly, canvas, catgr_id)

				idx = canvas.nonzero()
				annImg[idx[0],idx[1]] = catgr_id

		else: # if type(ann['segmentation']) == dict
			# mask
			if type(ann['segmentation']['counts']) == list:
				rle = mask.frPyObjects([ann['segmentation']], img['height'], img['width'])
			else:
				rle = [ann['segmentation']]
			m = mask.decode(rle)

			idx = m.nonzero()
			annImg[idx[0],idx[1]] = catgr_id

	return annImg
Пример #3
0
    def load_annotations(self, image_index, mask=True):
        # get ground truth annotations
        annotations_ids = self.coco.getAnnIds(
            imgIds=self.image_ids[image_index], iscrowd=False)
        annotations = np.zeros((0, 5))

        # some images appear to miss annotations (like image with id 257034)
        if len(annotations_ids) == 0:
            return annotations, None

        # parse annotations
        coco_annotations = self.coco.loadAnns(annotations_ids)
        for idx, a in enumerate(coco_annotations):

            # some annotations have basically no width / height, skip them
            if a['bbox'][2] < 1 or a['bbox'][3] < 1:
                continue

            annotation = np.zeros((1, 5))
            annotation[0, :4] = a['bbox']
            annotation[0, 4] = self.coco_label_to_label(a['category_id'])
            annotations = np.append(annotations, annotation, axis=0)

        # transform from [x, y, w, h] to [x1, y1, x2, y2]
        annotations[:, 2] = annotations[:, 0] + annotations[:, 2]
        annotations[:, 3] = annotations[:, 1] + annotations[:, 3]

        if all([("counts" in x) for x in coco_annotations]):
            masks = decode(coco_annotations)
            classes_ = annotations[:, -1].astype(np.uint64)
            if self.sparse:
                mask = np.zeros(masks.shape[:2], dtype=np.uint8)
                for nn, cc in enumerate(classes_):
                    cc = int(cc + 1)
                    mask = np.maximum(cc * (masks[:, :, nn] > 0), mask)
            else:
                mask = np.zeros((masks.shape[:2] + (1 + len(self.classes), )),
                                dtype=np.uint8)
                for nn, cc in enumerate(classes_):
                    cc = int(cc + 1)
                    mask[:, :, cc] = np.maximum(mask[:, :, cc], masks[:, :,
                                                                      nn])
                # ADD BACKGROUND CHANNEL
                mask[:, :, 0] = 1 - mask.sum(2, )
            #padded_masks = .cat((bgmask, padded_masks), dim=1)
        else:
            mask = None
        return annotations, mask
Пример #4
0
def createInstanceImage(annotations, img):

    # an image we want to create
    instanceImg = Image.new("I", (img.shape[1], img.shape[0]), 0)
    semanticImg = Image.new("I", (img.shape[1], img.shape[0]), 0)
    #directionImg = Image.new("I", (img['width'], img['height']), 0)

    # a drawer to draw into the image
    drawer_ins = ImageDraw.Draw(instanceImg)
    drawer_sem = ImageDraw.Draw(semanticImg)
    #drawer_dir = ImageDraw.Draw(directionImg)

    #loop over all objects
    insId_list = []
    instanceId = 0
    for ann in annotations:
        if ann['id'] not in insId_list:
            insId_list.append(ann['id'])
            instanceId = instanceId + 1

        if type(ann['segmentation']) == list:
            # polygon
            for seg in ann['segmentation']:
                poly = np.array(seg).reshape((len(seg) / 2), 2)
                poly = poly.tolist()
                poly = [tuple([elem[0], elem[1]]) for elem in poly]

                # pdb.set_trace()
                drawer_ins.polygon(poly, fill=instanceId,
                                   outline=None)  # outline = None
                drawer_sem.polygon(poly, fill=ann['category_id'], outline=None)
        elif False:  # (type(ann['segmentation'])== dict): ignore crowd object.
            # mask
            if type(ann['segmentation']['counts']) == list:
                rle = mask.frPyObjects([ann['segmentation']], img.shape[0],
                                       img.shape[1])
            else:
                rle = [ann['segmentation']]
            m = mask.decode(rle)

            idx = m.nonzero()
            points = [
                tuple([elem[1], elem[0]]) for elem in zip(idx[0], idx[1])
            ]
            drawer_ins.point(points, fill=instanceId)
            drawer_sem.point(points, fill=ann['category_id'])

    return instanceImg, semanticImg
Пример #5
0
def decode(rleObjs):
    if type(rleObjs) == list:
        return _mask.decode(rleObjs)
    else:
        return _mask.decode([rleObjs])[:,:,0]
Пример #6
0
def decode(rleObjs):
    if isinstance(rleObjs, list):
        return _mask.decode(rleObjs)
    else:
        return _mask.decode([rleObjs])[:, :, 0]
Пример #7
0
 def decode(self):
     return decode([self.enc])[:, :, 0]
Пример #8
0
 def mask_as_poly_list(mask):
     polygons = Mask(_mask.decode([mask])).polygons().points
     polygons = map(lambda p: p.reshape(-1), polygons)  # Detectron requirement
     polygons = filter(lambda p: len(p) >= 6, polygons)  # Detectron requirement
     return list(polygons)
Пример #9
0
def decode(rleObjs):
    if type(rleObjs) == list:
        return _mask.decode(rleObjs)
    else:
        return _mask.decode([rleObjs])[:,:,0]