def from_dict(cls, src_dict): assert 'counts' in src_dict assert 'size' in src_dict h = src_dict['size'][0] w = src_dict['size'][1] rle = mask.frPyObjects([src_dict], h, w) assert isinstance(rle, list) and isinstance(rle[0], dict) return cls(w, h, rle[0]['counts'])
def from_list(cls, w, h, src_list): rle = mask.frPyObjects(src_list, h, w) assert isinstance(rle, list) if len(rle) > 1: rle_bytes = mask.merge(rle)['counts'] else: rle_bytes = rle[0]['counts'] return cls(w, h, rle_bytes)
def getMask(self, ref): # return mask, area and mask-center ann = self.refToAnn[ref['ref_id']] image = self.Imgs[ref['image_id']] if type(ann['segmentation'][0]) == list: # polygon rle = mask.frPyObjects(ann['segmentation'], image['height'], image['width']) else: rle = ann['segmentation'] return {'rle': rle}
def getMask(self, ref): # return mask, area and mask-center ann = self.refToAnn[ref['ref_id']] image = self.Imgs[ref['image_id']] if type(ann['segmentation'][0]) == list: # polygon rle = mask.frPyObjects(ann['segmentation'], image['height'], image['width']) else: rle = ann['segmentation'] m = mask.decode(rle) m = np.sum(m, axis=2) # sometimes there are multiple binary map (corresponding to multiple segs) m = m.astype(np.uint8) # convert to np.uint8 # compute area area = sum(mask.area(rle)) # should be close to ann['area'] return {'mask': m, 'area': area}
def generateMasksAndQueries(MASK_DIR, QUERY_FILE_NAME, refer_obj): img_sg_counts = {} query_file_str = {} print "Creating {} mask files..".format(len(refer_obj.data['annotations'])) actual_processed = 0 for i,ann in enumerate(refer_obj.data['annotations']): if i % 1000 == 0: with open(QUERY_FILE_NAME, 'w') as fp: json.dump(query_file_str, fp) print "On annotation #{}, processed = {}".format(i,actual_processed) if ann['iscrowd'] == 1 or ann['id'] not in refer_obj.annToRef: continue actual_processed += 1 image = refer_obj.Imgs[ann['image_id']] if type(ann['segmentation']) == list: # polygon rle = mask.frPyObjects(ann['segmentation'], image['height'], image['width']) else: rle = ann['segmentation'] m = mask.decode(rle) m = np.sum(m, axis=2) matlab_dict = {'segimg_t': m} if ann['image_id'] in img_sg_counts: img_sg_counts[ann['image_id']] += 1 filename = "{}_{}".format(ann['image_id'],img_sg_counts[ann['image_id']]) else: img_sg_counts[ann['image_id']] = 1 filename = "{}_{}".format(ann['image_id'],img_sg_counts[ann['image_id']]) io.savemat(osp.join(MASK_DIR,filename),matlab_dict) ref = refer_obj.annToRef[ann['id']] if filename in query_file_str: val = query_file_str[filename] else: val = [] for sentence in ref['sentences']: val.append(sentence['raw']) query_file_str[filename] = val