Python pycocotools.mask.encode() Examples
The following are 30
code examples of pycocotools.mask.encode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pycocotools.mask
, or try the search function
.
Example #1
Source File: generate_coco_json.py From coco-json-converter with GNU General Public License v3.0 | 14 votes |
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
Example #2
Source File: inference.py From Res2Net-maskrcnn with MIT License | 7 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #3
Source File: coco.py From dataiku-contrib with Apache License 2.0 | 7 votes |
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks): """Arrange resutls to match COCO specs in http://cocodataset.org/#format """ # If no results, return an empty list if rois is None: return [] results = [] for image_id in image_ids: # Loop through detections for i in range(rois.shape[0]): class_id = class_ids[i] score = scores[i] bbox = np.around(rois[i], 1) mask = masks[:, :, i] result = { "image_id": image_id, "category_id": dataset.get_source_class_id(class_id, "coco"), "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]], "score": score, "segmentation": maskUtils.encode(np.asfortranarray(mask)) } results.append(result) return results
Example #4
Source File: coco.py From deep-learning-explorer with Apache License 2.0 | 6 votes |
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks): """Arrange resutls to match COCO specs in http://cocodataset.org/#format """ # If no results, return an empty list if rois is None: return [] results = [] for image_id in image_ids: # Loop through detections for i in range(rois.shape[0]): class_id = class_ids[i] score = scores[i] bbox = np.around(rois[i], 1) mask = masks[:, :, i] result = { "image_id": image_id, "category_id": dataset.get_source_class_id(class_id, "coco"), "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]], "score": score, "segmentation": maskUtils.encode(np.asfortranarray(mask)) } results.append(result) return results
Example #5
Source File: inference.py From remote_sensing_object_detection_2019 with MIT License | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #6
Source File: inference.py From remote_sensing_object_detection_2019 with MIT License | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #7
Source File: inference.py From remote_sensing_object_detection_2019 with MIT License | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #8
Source File: coco.py From DeepTL-Lane-Change-Classification with MIT License | 6 votes |
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks): """Arrange resutls to match COCO specs in http://cocodataset.org/#format """ # If no results, return an empty list if rois is None: return [] results = [] for image_id in image_ids: # Loop through detections for i in range(rois.shape[0]): class_id = class_ids[i] score = scores[i] bbox = np.around(rois[i], 1) mask = masks[:, :, i] result = { "image_id": image_id, "category_id": dataset.get_source_class_id(class_id, "coco"), "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]], "score": score, "segmentation": maskUtils.encode(np.asfortranarray(mask)) } results.append(result) return results
Example #9
Source File: utils.py From mmdetection with Apache License 2.0 | 6 votes |
def encode_mask_results(mask_results): """Encode bitmap mask to RLE code. Args: mask_results (list | tuple[list]): bitmap mask results. In mask scoring rcnn, mask_results is a tuple of (segm_results, segm_cls_score). Returns: list | tuple: RLE encoded mask. """ if isinstance(mask_results, tuple): # mask scoring cls_segms, cls_mask_scores = mask_results else: cls_segms = mask_results num_classes = len(cls_segms) encoded_mask_results = [[] for _ in range(num_classes)] for i in range(len(cls_segms)): for cls_segm in cls_segms[i]: encoded_mask_results[i].append( mask_util.encode( np.array( cls_segm[:, :, np.newaxis], order='F', dtype='uint8'))[0]) # encoded with RLE if isinstance(mask_results, tuple): return encoded_mask_results, cls_mask_scores else: return encoded_mask_results
Example #10
Source File: io.py From MOTSFusion with MIT License | 6 votes |
def load_image(filename, id_divisor=1000): img = np.array(Image.open(filename)) obj_ids = np.unique(img) objects = [] mask = np.zeros(img.shape, dtype=np.uint8, order="F") # Fortran order needed for pycocos RLE tools for idx, obj_id in enumerate(obj_ids): if obj_id == 0: # background continue mask.fill(0) pixels_of_elem = np.where(img == obj_id) mask[pixels_of_elem] = 1 objects.append(SegmentedObject( rletools.encode(mask), obj_id // id_divisor, obj_id )) return objects
Example #11
Source File: inference.py From R2CNN.pytorch with MIT License | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #12
Source File: eval_instance_segmentation_coco.py From chainercv with MIT License | 6 votes |
def _create_anno(msk, lb, sc, img_id, anno_id, ar=None, crw=None): H, W = msk.shape if crw is None: crw = False msk = np.asfortranarray(msk.astype(np.uint8)) rle = mask_tools.encode(msk) if ar is None: # We compute dummy area to pass to pycocotools. # Note that area dependent scores are ignored afterwards. ar = mask_tools.area(rle) if crw is None: crw = False # Rounding is done to make the result consistent with COCO. anno = { 'image_id': img_id, 'category_id': lb, 'segmentation': rle, 'area': ar, 'id': anno_id, 'iscrowd': crw} if sc is not None: anno.update({'score': sc}) return anno
Example #13
Source File: inference.py From Clothing-Detection with GNU General Public License v3.0 | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #14
Source File: coco.py From Mask-RCNN-Pedestrian-Detection with MIT License | 6 votes |
def build_coco_results(dataset, image_ids, rois, class_ids, scores, masks): """Arrange resutls to match COCO specs in http://cocodataset.org/#format """ # If no results, return an empty list if rois is None: return [] results = [] for image_id in image_ids: # Loop through detections for i in range(rois.shape[0]): class_id = class_ids[i] score = scores[i] bbox = np.around(rois[i], 1) mask = masks[:, :, i] result = { "image_id": image_id, "category_id": dataset.get_source_class_id(class_id, "coco"), "bbox": [bbox[1], bbox[0], bbox[3] - bbox[1], bbox[2] - bbox[0]], "score": score, "segmentation": maskUtils.encode(np.asfortranarray(mask)) } results.append(result) return results
Example #15
Source File: mask_rcnn.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #16
Source File: inference.py From DetNAS with MIT License | 6 votes |
def forward(self, x, boxes): import pycocotools.mask as mask_util import numpy as np results = super(MaskPostProcessorCOCOFormat, self).forward(x, boxes) for result in results: masks = result.get_field("mask").cpu() rles = [ mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] for mask in masks ] for rle in rles: rle["counts"] = rle["counts"].decode("utf-8") result.add_field("mask", rles) return results # the next two functions should be merged inside Masker # but are kept here for the moment while we need them # temporarily gor paste_mask_in_image
Example #17
Source File: coco_tools.py From Gun-Detector with Apache License 2.0 | 5 votes |
def _RleCompress(masks): """Compresses mask using Run-length encoding provided by pycocotools. Args: masks: uint8 numpy array of shape [mask_height, mask_width] with values in {0, 1}. Returns: A pycocotools Run-length encoding of the mask. """ return mask.encode(np.asfortranarray(masks))
Example #18
Source File: coco_tools.py From Traffic-Rule-Violation-Detection-System with MIT License | 5 votes |
def _RleCompress(masks): """Compresses mask using Run-length encoding provided by pycocotools. Args: masks: uint8 numpy array of shape [mask_height, mask_width] with values in {0, 1}. Returns: A pycocotools Run-length encoding of the mask. """ return mask.encode(np.asfortranarray(masks))
Example #19
Source File: coco_metric.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _update_op(self, detections, instance_masks): """Update detection results and groundtruth data. Append detection/mask results to self.detections and self.masks to aggregate results from all validation set. Args: detections: Detection results in a tensor with each row representing [image_id, x, y, width, height, score, class]. Numpy array shape [batch_size, num_detections, 7]. instance_masks: Instance mask predictions associated with each detections [batch_size, num_detections, height, width]. """ for i in range(len(detections)): if detections[i].shape[0] == 0: continue # Convert the mask to uint8 and then to fortranarray for RLE encoder. encoded_mask_list = [maskUtils.encode( np.asfortranarray(instance_mask.astype(np.uint8))) for instance_mask in instance_masks[i]] self.detections.extend(detections[i]) # The encoder returns a list of RLE-encoded instance masks here. self.masks.append( encoded_mask_list )
Example #20
Source File: coco_metric.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _update_op(self, detections, instance_masks): """Update detection results and groundtruth data. Append detection/mask results to self.detections and self.masks to aggregate results from all validation set. Args: detections: Detection results in a tensor with each row representing [image_id, x, y, width, height, score, class]. Numpy array shape [batch_size, num_detections, 7]. instance_masks: Instance mask predictions associated with each detections [batch_size, num_detections, height, width]. """ for i in range(len(detections)): if detections[i].shape[0] == 0: continue # Convert the mask to uint8 and then to fortranarray for RLE encoder. encoded_mask_list = [maskUtils.encode( np.asfortranarray(instance_mask.astype(np.uint8))) for instance_mask in instance_masks[i]] self.detections.extend(detections[i]) # The encoder returns a list of RLE-encoded instance masks here. self.masks.append( encoded_mask_list )
Example #21
Source File: coco_metric.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def _update_op(self, detections, instance_masks): """Update detection results and groundtruth data. Append detection/mask results to self.detections and self.masks to aggregate results from all validation set. Args: detections: Detection results in a tensor with each row representing [image_id, x, y, width, height, score, class]. Numpy array shape [batch_size, num_detections, 7]. instance_masks: Instance mask predictions associated with each detections [batch_size, num_detections, height, width]. """ for i in range(len(detections)): if detections[i].shape[0] == 0: continue # Convert the mask to uint8 and then to fortranarray for RLE encoder. encoded_mask_list = [maskUtils.encode( np.asfortranarray(instance_mask.astype(np.uint8))) for instance_mask in instance_masks[i]] self.detections.extend(detections[i]) # The encoder returns a list of RLE-encoded instance masks here. self.masks.append( encoded_mask_list )
Example #22
Source File: segment.py From MOTSFusion with MIT License | 5 votes |
def do_refinement(detections, image, refinement_net): data = refinement_net.valid_data boxes = [[det['bbox'][0], det['bbox'][1], det['bbox'][2] - det['bbox'][0], det['bbox'][3] - det['bbox'][1]] for det in detections] image_data = data.set_up_data_for_image(image, boxes) segmentations = [] for idx in range(len(boxes)): segmentation = {} feed_dict = data.get_feed_dict_for_next_step(image_data, idx) step_res = refinement_net.trainer.validation_step(feed_dict=feed_dict, extraction_keys=[ Extractions.SEGMENTATION_POSTERIORS_ORIGINAL_SIZE, Extractions.SEGMENTATION_MASK_ORIGINAL_SIZE, DataKeys.OBJ_TAGS]) extractions = step_res[Extractions.EXTRACTIONS] predicted_segmentation = extract(Extractions.SEGMENTATION_MASK_ORIGINAL_SIZE, extractions) mask = predicted_segmentation.astype("uint8") * 255 encoded_mask = encode(np.asfortranarray(mask)) segmentation['counts'] = encoded_mask['counts'].decode("UTF-8") posteriors = extract(Extractions.SEGMENTATION_POSTERIORS_ORIGINAL_SIZE, extractions) conf_scores = posteriors.copy() conf_scores[predicted_segmentation == 0] = 1 - posteriors[predicted_segmentation == 0] conf_scores = 2 * conf_scores - 1 conf_score = conf_scores[:].mean() segmentation['score'] = str(conf_score) segmentation['size'] = [image.shape[0], image.shape[1]] segmentations.append(segmentation) return segmentations
Example #23
Source File: io.py From MOTSFusion with MIT License | 5 votes |
def load_txt(path): objects_per_frame = {} track_ids_per_frame = {} # To check that no frame contains two objects with same id combined_mask_per_frame = {} # To check that no frame contains overlapping masks with open(path, "r") as f: for line in f: line = line.strip() fields = line.split(" ") frame = int(fields[0]) if frame not in objects_per_frame: objects_per_frame[frame] = [] if frame not in track_ids_per_frame: track_ids_per_frame[frame] = set() if int(fields[1]) in track_ids_per_frame[frame]: assert False, "Multiple objects with track id " + fields[1] + " in frame " + fields[0] else: track_ids_per_frame[frame].add(int(fields[1])) class_id = int(fields[2]) if not(class_id == 1 or class_id == 2 or class_id == 10): assert False, "Unknown object class " + fields[2] mask = {'size': [int(fields[3]), int(fields[4])], 'counts': fields[5].encode(encoding='UTF-8')} if frame not in combined_mask_per_frame: combined_mask_per_frame[frame] = mask elif rletools.area(rletools.merge([combined_mask_per_frame[frame], mask], intersect=True)) > 0.0: assert False, "Objects with overlapping masks in frame " + fields[0] else: combined_mask_per_frame[frame] = rletools.merge([combined_mask_per_frame[frame], mask], intersect=False) objects_per_frame[frame].append(SegmentedObject( mask, class_id, int(fields[1]) )) return objects_per_frame
Example #24
Source File: tracked_sequence.py From MOTSFusion with MIT License | 5 votes |
def get_mask(self, t, id, decode=True, postprocess=False): if decode: if postprocess: mask = cocomask.decode(self.masks[t][id]) return cv2.erode(mask, np.ones((5, 5), np.uint8), iterations=1) else: return cocomask.decode(self.masks[t][id]) else: if postprocess: mask = cocomask.decode(self.masks[t][id]) return cocomask.encode(cv2.erode(mask, np.ones((5, 5), np.uint8), iterations=1)) else: return self.masks[t][id]
Example #25
Source File: transforms.py From PolarMask with Apache License 2.0 | 5 votes |
def bbox_mask2result(bboxes, masks, labels, num_classes, img_meta): """Convert detection results to a list of numpy arrays. Args: bboxes (Tensor): shape (n, 5) masks (Tensor): shape (n, 2, 36) labels (Tensor): shape (n, ) num_classes (int): class number, including background class Returns: list(ndarray): bbox results of each class """ ori_shape = img_meta['ori_shape'] img_h, img_w, _ = ori_shape mask_results = [[] for _ in range(num_classes - 1)] for i in range(masks.shape[0]): im_mask = np.zeros((img_h, img_w), dtype=np.uint8) mask = [masks[i].transpose(1,0).unsqueeze(1).int().data.cpu().numpy()] im_mask = cv2.drawContours(im_mask, mask, -1,1,-1) rle = mask_util.encode( np.array(im_mask[:, :, np.newaxis], order='F'))[0] label = labels[i] mask_results[label].append(rle) if bboxes.shape[0] == 0: bbox_results = [ np.zeros((0, 5), dtype=np.float32) for i in range(num_classes - 1) ] return bbox_results, mask_results else: bboxes = bboxes.cpu().numpy() labels = labels.cpu().numpy() bbox_results = [bboxes[labels == i, :] for i in range(num_classes - 1)] return bbox_results, mask_results
Example #26
Source File: masks.py From open-solution-ship-detection with MIT License | 5 votes |
def coco_rle_from_binary(prediction): prediction = np.asfortranarray(prediction) return cocomask.encode(prediction)
Example #27
Source File: segms.py From Detectron.pytorch with MIT License | 5 votes |
def flip_segms(segms, height, width): """Left/right flip each mask in a list of masks.""" def _flip_poly(poly, width): flipped_poly = np.array(poly) flipped_poly[0::2] = width - np.array(poly[0::2]) - 1 return flipped_poly.tolist() def _flip_rle(rle, height, width): if 'counts' in rle and type(rle['counts']) == list: # Magic RLE format handling painfully discovered by looking at the # COCO API showAnns function. rle = mask_util.frPyObjects([rle], height, width) mask = mask_util.decode(rle) mask = mask[:, ::-1, :] rle = mask_util.encode(np.array(mask, order='F', dtype=np.uint8)) return rle flipped_segms = [] for segm in segms: if type(segm) == list: # Polygon format flipped_segms.append([_flip_poly(poly, width) for poly in segm]) else: # RLE format assert type(segm) == dict flipped_segms.append(_flip_rle(segm, height, width)) return flipped_segms
Example #28
Source File: CocoDatasetInfo.py From Pose2Seg with MIT License | 5 votes |
def flip_segms(self, segms, height, width): """Left/right flip each mask in a list of masks.""" flipped_segms = [] for segm in segms: mask = np.fliplr(annToMask(segm, height, width)) rle = mask_util.encode(np.array(mask, order='F', dtype=np.uint8)) flipped_segms.append(rle) return flipped_segms
Example #29
Source File: segms.py From FPN-Pytorch with MIT License | 5 votes |
def flip_segms(segms, height, width): """Left/right flip each mask in a list of masks.""" def _flip_poly(poly, width): flipped_poly = np.array(poly) flipped_poly[0::2] = width - np.array(poly[0::2]) - 1 return flipped_poly.tolist() def _flip_rle(rle, height, width): if 'counts' in rle and type(rle['counts']) == list: # Magic RLE format handling painfully discovered by looking at the # COCO API showAnns function. rle = mask_util.frPyObjects([rle], height, width) mask = mask_util.decode(rle) mask = mask[:, ::-1, :] rle = mask_util.encode(np.array(mask, order='F', dtype=np.uint8)) return rle flipped_segms = [] for segm in segms: if type(segm) == list: # Polygon format flipped_segms.append([_flip_poly(poly, width) for poly in segm]) else: # RLE format assert type(segm) == dict flipped_segms.append(_flip_rle(segm, height, width)) return flipped_segms
Example #30
Source File: coco_tools.py From models with Apache License 2.0 | 5 votes |
def _RleCompress(masks): """Compresses mask using Run-length encoding provided by pycocotools. Args: masks: uint8 numpy array of shape [mask_height, mask_width] with values in {0, 1}. Returns: A pycocotools Run-length encoding of the mask. """ return mask.encode(np.asfortranarray(masks))