def segm_to_rle(segm, w, h): """ Convert segmentation map which can be polygons, uncompressed RLE to RLE. Reference: https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py Args: segm (list<list<int>> or list<int>): A segmentation map which can be polygons. w (int): Image width h (int): Image hight Returns: rle: RLE """ 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 rle
def poly2mask_single(h, w, poly): # TODO: write test for poly2mask, using mask2poly convert mask to poly', compare poly with poly' # visualize the mask rles = maskUtils.frPyObjects(poly, h, w) rle = maskUtils.merge(rles) mask = maskUtils.decode(rle) return mask
def poly2mask_single(h, w, poly): # TODO: write test for poly2mask, using mask2poly convert mask to poly', compare poly with poly' # visualize the mask rles = maskUtils.frPyObjects(poly, h, w) rle = maskUtils.merge(rles) mask = maskUtils.decode(rle) # sum = mask.sum() # print("{} {} {} {}".format(sum, h, w, poly)) # if not mask.any(): # pass return mask
def annToRLE(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
def __getitem__(self, index): img_file = os.path.join(self._img_dir, str(self._infos.data[index].id) + '.jpg') img = transforms.ToTensor()(Image.open(img_file).convert('RGB')) img_w, img_h = img.size(2), img.size(1) target = torch.LongTensor(img_h, img_w).zero_() for inst in self._infos.data[index].insts: polys = [] # { bg, person, bicycle, car, motorcycle, truck, bus, train } if self._n_class == 7: if inst.category_idx == 8: inst.category_idx = 5 if inst.category_idx <= 6: for poly in inst.seg: polys.append(poly.tolist()) # { bg, person } elif self._n_class == 2: if inst.category_idx == 1: for poly in inst.seg: polys.append(poly.tolist()) if polys: rles = maskUtils.frPyObjects(polys, img_h, img_w) rle = maskUtils.merge(rles) mask = maskUtils.decode(rle) target.masked_fill_(torch.from_numpy(mask), inst.category_idx) p_w = self._infos.patchSize.w p_h = self._infos.patchSize.h x0 = random.randint(0, (img_w - p_w)) y0 = random.randint(0, (img_h - p_h)) img = img[:, y0:y0+p_h, x0:x0+p_w] target = target[y0:y0+p_h, x0:x0+p_w] return img, target
def annToRLE(self, ann): """ Convert annotation which can be polygons, uncompressed RLE to RLE. :return: binary mask (numpy 2D array) """ im = self.images.xs(ann.image_id) h, w = im.height, im.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