mask_voc2coco.py
Go to the documentation of this file.
00001 # --------------------------------------------------------
00002 # Deformable Convolutional Networks
00003 # Copyright (c) 2017 Microsoft
00004 # Licensed under The Apache-2.0 License [see LICENSE for details]
00005 # Written by Yi Li
00006 # --------------------------------------------------------
00007 
00008 from skimage.draw import polygon
00009 import numpy as np
00010 import cv2
00011 from utils.tictoc import tic, toc
00012 from dataset.pycocotools.mask import encode as encodeMask_c
00013 
00014 def encodeMask(M):
00015     """
00016     Encode binary mask M using run-length encoding.
00017     :param   M (bool 2D array)  : binary mask to encode
00018     :return: R (object RLE)     : run-length encoding of binary mask
00019     """
00020     [h, w] = M.shape
00021     M = M.flatten(order='F')
00022     N = len(M)
00023     counts_list = []
00024     pos = 0
00025     # counts
00026     counts_list.append(1)
00027     diffs = np.logical_xor(M[0:N - 1], M[1:N])
00028     for diff in diffs:
00029         if diff:
00030             pos += 1
00031             counts_list.append(1)
00032         else:
00033             counts_list[pos] += 1
00034     # if array starts from 1. start with 0 counts for 0
00035     if M[0] == 1:
00036         counts_list = [0] + counts_list
00037     return {'size': [h, w],
00038             'counts': counts_list,
00039             }
00040 
00041 def mask_voc2coco(voc_masks, voc_boxes, im_height, im_width, binary_thresh = 0.4):
00042     num_pred = len(voc_masks)
00043     assert(num_pred==voc_boxes.shape[0])
00044     mask_img = np.zeros((im_height, im_width, num_pred), dtype=np.uint8, order='F')
00045     for i in xrange(num_pred):
00046         pred_box = np.round(voc_boxes[i, :4]).astype(int)
00047         pred_mask = voc_masks[i]
00048         pred_mask = cv2.resize(pred_mask.astype(np.float32), (pred_box[2] - pred_box[0] + 1, pred_box[3] - pred_box[1] + 1))
00049         mask_img[pred_box[1]:pred_box[3]+1, pred_box[0]:pred_box[2]+1, i] = pred_mask >= binary_thresh
00050     coco_mask = encodeMask_c(mask_img)
00051     return coco_mask


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30