def cocoSegmentationToPngDemo(dataDir='/home/zyq/data/coco', dataTypeAnn='train2014', dataTypeRes='examples', \
        pngFolderName='export_png', isAnnotation=True, exportImageLimit=10):
    '''
    Converts COCO segmentation .json files (GT or results) to one .png file per image.
    :param dataDir: location of the COCO root folder
    :param dataTypeAnn: identifier of the ground-truth annotation file
    :param dataTypeRes: identifier of the result annotation file (if any)
    :param pngFolderName: the name of the subfolder where we store .png images
    :param isAnnotation: whether the COCO file is a GT annotation or a result file
    :return: None
    '''

    # Define paths
    annPath = '%s/annotations/instances_%s.json' % (dataDir, dataTypeAnn)
    pngFolder = '%s/annotations/%s' % (dataDir, pngFolderName)

    # Initialize COCO ground-truth API
    coco = COCO(annPath)
    imgIds = coco.getImgIds()

    # if exportImageLimit < len(imgIds):
    #     imgIds = imgIds[0:exportImageLimit]
    txt_path = '%s/annotations/train_coco_seg.txt' % (dataDir)
    with open(txt_path, "w") as f:
        # Convert each image to a png
        imgCount = len(imgIds)
        for i in xrange(0, imgCount):
            imgId = imgIds[i]
            imgName = coco.loadImgs(ids=imgId)[0]['file_name'].replace(
                '.jpg', '')
            print('Exporting image %d of %d: %s' % (i + 1, imgCount, imgName))
            segmentationPath = '%s/%s.png' % (pngFolder, imgName)
            if cocoSegmentationToPng(coco, imgId, segmentationPath):
                f.write(imgName + "\n")
Esempio n. 2
0
 def __init__(self,
              dataset_root,
              transform,
              subset,
              batchsize,
              trainsizes,
              testsize,
              istrain,
              gt_pergrid=3):
     self.dataset_root = dataset_root
     self.image_dir = "{}/images/{}2017".format(dataset_root, subset)
     self.coco = COCO("{}/annotations/instances_{}2017.json".format(
         dataset_root, subset))
     self.istrain = istrain
     self.testsize = testsize
     self.batch_size = batchsize
     # get the mapping from original category ids to labels
     self.cat_ids = self.coco.getCatIds()
     self.numcls = len(self.cat_ids)
     self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
     self.img_ids, self.img_infos = self._filter_imgs()
     self._transform = transform
     self.multisizes = trainsizes
     self.strides = np.array([8, 16, 32])
     self._gt_per_grid = gt_pergrid
Esempio n. 3
0
def load_coco_annotation(index, anns_path, img_folder, num_classes=1):
    """
        coco ann: [u'segmentation', u'area', u'iscrowd', u'image_id', u'bbox', u'category_id', u'id']
        iscrowd:
            crowd instances are handled by marking their overlaps with all categories to -1
            and later excluded in training
        bbox:
            [x1, y1, w, h]
        :param index: coco image id
        :return: roidb entry
        """
    coco = COCO(anns_path)
    im_ann = coco.loadImgs(index)[0]
    width = im_ann['width']
    height = im_ann['height']

    annIds = coco.getAnnIds(imgIds=index, iscrowd=False)
    objs = coco.loadAnns(annIds)

    # sanitize bboxes
    valid_objs = []
    for obj in objs:
        x, y, w, h = obj['bbox']
        x1 = np.max((0, x))
        y1 = np.max((0, y))
        x2 = np.min((width - 1, x1 + np.max((0, w - 1))))
        y2 = np.min((height - 1, y1 + np.max((0, h - 1))))
        if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
            obj['clean_bbox'] = [x1, y1, x2, y2]
            valid_objs.append(obj)
    objs = valid_objs
    num_objs = len(objs)

    boxes = np.zeros((num_objs, 4), dtype=np.uint16)
    gt_classes = np.zeros((num_objs), dtype=np.int32)
    overlaps = np.zeros((num_objs, num_classes), dtype=np.float32)

    for ix, obj in enumerate(objs):
        cls = 0  # we only have 1 class
        boxes[ix, :] = obj['clean_bbox']
        gt_classes[ix] = cls
        if obj['iscrowd']:
            overlaps[ix, :] = -1.0
        else:
            overlaps[ix, cls] = 1.0

    roi_rec = {
        'image': os.path.join(img_folder,
                              coco.loadImgs(index)[0]['file_name']),
        'height': height,
        'width': width,
        'boxes': boxes,
        'gt_classes': gt_classes,
        'gt_overlaps': overlaps,
        'max_classes': overlaps.argmax(axis=1),
        'max_overlaps': overlaps.max(axis=1),
        'masks': rle_to_mask_arrs(coco, index),
        'flipped': False
    }
    return roi_rec
Esempio n. 4
0
    def _load_all(self, anno_file, shuffle):
        """
        initialize all entries given annotation json file

        Parameters:
        ----------
        anno_file: str
            annotation json file
        shuffle: bool
            whether to shuffle image list
        """
        image_set_index = []
        labels = []
        coco = COCO(anno_file)
        print(coco)
        img_ids = coco.getImgIds()
        #print (anno_file)
        subdir = anno_file.split('/')[-1].split('.')[0].split('_')[2]
        #print (subdir)
        print('img_ids_len:', len(img_ids))
        for img_id in img_ids:
            # filename
            image_info = coco.loadImgs(img_id)[0]
            filename = image_info["file_name"]
            #print(image_info)
            height = image_info["height"]
            width = image_info["width"]
            # label
            anno_ids = coco.getAnnIds(imgIds=img_id)
            #print(anno_ids)
            annos = coco.loadAnns(anno_ids)
            #print(annos)
            label = []
            for anno in annos:
                print(len(annos))
                cat_id = int(anno["category_id"])
                cat_id = labeltrans_dict[cat_id]
                #                print (cat_id)
                bbox = anno["bbox"]
                assert len(bbox) == 4
                xmin = float(bbox[0]) / width
                ymin = float(bbox[1]) / height
                xmax = xmin + float(bbox[2]) / width
                ymax = ymin + float(bbox[3]) / height
                label.append([cat_id, xmin, ymin, xmax, ymax, 0])
            if label:
                labels.append(np.array(label))
                image_set_index.append(os.path.join(subdir, filename))

        if shuffle:
            import random
            indices = range(len(image_set_index))
            random.shuffle(indices)
            image_set_index = [image_set_index[i] for i in indices]
            labels = [labels[i] for i in indices]
        # store the results
        self.image_set_index = image_set_index
        self.labels = labels
Esempio n. 5
0
 def __init__(self, cfg, subset, istrain):
     super().__init__(cfg, subset, istrain)
     self.image_dir = "{}/images/{}2017".format(self.dataset_root, subset)
     self.coco = COCO("{}/annotations/instances_{}2017.json".format(
         self.dataset_root, subset))
     # get the mapping from original category ids to labels
     self.cat_ids = self.coco.getCatIds()
     self.numcls = len(self.cat_ids)
     self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
     self._ids, self.img_infos = self._filter_imgs()
Esempio n. 6
0
    def _load_all(self, anno_file, shuffle):
        """
        initialize all entries given annotation json file

        Parameters:
        ----------
        anno_file: str
            annotation json file
        shuffle: bool
            whether to shuffle image list
        """
        image_set_index = []
        labels = []
        coco = COCO(anno_file)
        img_ids = coco.getImgIds()
        # deal with class names
        cats = [cat['name'] for cat in coco.loadCats(coco.getCatIds())]
        class_to_coco_ind = dict(zip(cats, coco.getCatIds()))
        class_to_ind = dict(zip(self.classes, range(len(self.classes))))
        coco_ind_to_class_ind = dict([(class_to_coco_ind[cls], class_to_ind[cls])
                                     for cls in self.classes[0:]])
        for img_id in img_ids:
            # filename
            image_info = coco.loadImgs(img_id)[0]
            filename = image_info["file_name"]
            subdir = filename.split('_')[1]
            height = image_info["height"]
            width = image_info["width"]
            # label
            anno_ids = coco.getAnnIds(imgIds=img_id)
            annos = coco.loadAnns(anno_ids)
            label = []
            for anno in annos:
                cat_id = coco_ind_to_class_ind[anno['category_id']]
                bbox = anno["bbox"]
                assert len(bbox) == 4
                xmin = float(bbox[0]) / width
                ymin = float(bbox[1]) / height
                xmax = xmin + float(bbox[2]) / width
                ymax = ymin + float(bbox[3]) / height
                label.append([cat_id, xmin, ymin, xmax, ymax, 0])
            if label:
                labels.append(np.array(label))
                image_set_index.append(os.path.join(subdir, filename))

        if shuffle:
            import random
            indices = list(range(len(image_set_index)))
            random.shuffle(indices)
            image_set_index = [image_set_index[i] for i in indices]
            labels = [labels[i] for i in indices]
        # store the results
        self.image_set_index = image_set_index
        self.labels = labels
Esempio n. 7
0
 def __init__(self, dataset_root, transform, subset, batchsize, netsize, istrain):
   self.dataset_root = dataset_root
   self.image_dir = "{}/images/{}2017".format(dataset_root, subset)
   self.coco = COCO("{}/annotations/instances_{}2017.json".format(dataset_root, subset))
   self.anchors = np.array(eval("COCO_ANCHOR_{}".format(netsize)))
   self.istrain = istrain
   self.netsize = netsize
   self.batch_size = batchsize
   # get the mapping from original category ids to labels
   self.cat_ids = self.coco.getCatIds()
   self.cat2label = {
     cat_id: i
     for i, cat_id in enumerate(self.cat_ids)
   }
   self.img_ids, self.img_infos = self._filter_imgs()
   self._transform = transform
   self.multisizes = TRAIN_INPUT_SIZES_COCO
Esempio n. 8
0
 def build_GT(self):
     self.cocoGt = COCO(
         os.path.join(self.dataset_root,
                      'annotations/instances_val2017.json'))
Esempio n. 9
0
    def __init__(self,
                 image_set,
                 root_path,
                 data_path,
                 result_path=None,
                 mask_size=-1,
                 binary_thresh=None,
                 load_mask=False):
        """
        fill basic information to initialize imdb
        :param image_set: train2014, val2014, test2015
        :param root_path: 'data', will write 'rpn_data', 'cache'
        :param data_path: 'data/coco'
        """
        super(coco, self).__init__('COCO', image_set, root_path, data_path,
                                   result_path)
        self.root_path = root_path
        self.data_path = data_path
        self.coco = COCO(self._get_ann_file())
        # train0829.txt
        print('>>>>>>>>>>> image set {}'.format(image_set))
        self.trainsetpath = '/private/luyujie/obstacle_detector/obstacle_detector/data/obstacle2d/ImageSets/train0829.txt'
        self.valsetpath = '/private/luyujie/obstacle_detector/obstacle_detector/data/obstacle2d/ImageSets/val0629.txt'
        self.imagepath = '/private/luyujie/obstacle_detector/obstacle_detector/data/obstacle2d/JPGImages'
        self.trainset = ['index0']
        self.valset = ['index0']
        with open(self.trainsetpath) as tsf:
            for line in tsf:
                self.trainset.append(
                    os.path.join(self.imagepath,
                                 line.strip() + '.jpg'))
        #print self.trainset[1]

        # val0629.txt
        with open(self.valsetpath) as vsf:
            for line in vsf:
                self.valset.append(
                    os.path.join(self.imagepath,
                                 line.strip() + '.jpg'))
        # deal with class names
        #print self.valset[2]
        cats = [
            cat['name'] for cat in self.coco.loadCats(self.coco.getCatIds())
        ]
        self.classes = ['__background__'] + cats
        #print('>>>> cats {}'.format(cats))
        self.num_classes = len(self.classes)
        self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))
        self._class_to_coco_ind = dict(zip(cats, self.coco.getCatIds()))
        self._coco_ind_to_class_ind = dict([(self._class_to_coco_ind[cls],
                                             self._class_to_ind[cls])
                                            for cls in self.classes[1:]])

        # load image file names
        self.image_set_index = self._load_image_set_index()
        self.num_images = len(self.image_set_index)
        print 'num_images', self.num_images
        self.mask_size = mask_size
        self.binary_thresh = binary_thresh
        self.load_mask = load_mask

        # deal with data name
        view_map = {
            'minival2014': 'val2014',
            'sminival2014': 'val2014',
            'valminusminival2014': 'val2014',
            'test-dev2015': 'test2015',
            'test2015': 'test2015'
        }

        self.data_name = view_map[
            image_set] if image_set in view_map else image_set