Exemplo n.º 1
0
    def load_data_trainval(self, set_name, image_dir, annotation_path):
        """
        Load train+val data
        """
        data = {}

        # load annotations file
        if self.verbose:
            print('  > Loading annotation file: ' + annotation_path)
        annotations = load_json(annotation_path)

        # progressbar
        if self.verbose:
            prgbar = progressbar.ProgressBar(
                max_value=len(annotations['annotations']))

        # ---------------------------------------------------------
        #  parse annotations
        # ---------------------------------------------------------

        if self.verbose:
            print('  > Processing image annotations... ')
        # get all image filenames + ids into a list
        filename_ids, images_annot_by_fname, images_fname_by_id = self.parse_image_annotations(
            image_dir, annotations)

        if self.verbose:
            print('  > Processing category annotations... ')
        parsed_annots = self.parse_category_annotations(annotations)
        categories, category_list, supercategory_list, category_id = parsed_annots

        if self.verbose:
            print('  > Processing object annotations... ')
        # group annotations by file name
        annotation_id_dict = {}
        for i, annot in enumerate(annotations['annotations']):
            filename = images_fname_by_id[annot['image_id']]
            category_annot = categories[annot['category_id']]
            obj_id = annot["id"]
            annotation_id_dict[obj_id] = i

            if isinstance(annot["segmentation"], list):
                segmentation = squeeze_list(annot["segmentation"],
                                            -1)  # squeeze list
            elif isinstance(annot["segmentation"]['counts'], list):
                segmentation = annot["segmentation"]["counts"]
            else:
                segmentation = annot["segmentation"]

            # convert from [x,y,w,h] to [xmin,ymin,xmax,ymax]
            bbox = [
                annot['bbox'][0],  # xmin
                annot['bbox'][1],  # ymin
                annot['bbox'][0] + annot['bbox'][2] - 1,  # ymax
                annot['bbox'][1] + annot['bbox'][3] - 1
            ]  # ymax

            obj = {
                "category": category_annot['name'],
                "supercategory": category_annot['supercategory'],
                "area": annot['area'],
                "iscrowd": annot['iscrowd'],
                "segmentation": segmentation,
                "bbox": bbox,
                "image_id": annot['image_id'],
                "category_id": annot['category_id'],
                "id": annot["id"],
                "annotation_id": i
            }

            # add annotations to the image data
            try:
                images_annot_by_fname[filename]["object"].update({obj_id: obj})
            except KeyError:
                images_annot_by_fname[filename]["object"] = {obj_id: obj}

            # update progressbar
            if self.verbose:
                prgbar.update(i)

        # reset progressbar
        if self.verbose:
            prgbar.finish()

        return {
            set_name: [
                OrderedDict(sorted(images_annot_by_fname.items())),
                annotations, annotation_id_dict, category_list,
                supercategory_list, category_id, filename_ids,
                images_fname_by_id
            ]
        }
Exemplo n.º 2
0
def test_squeeze_list(sample, output, fill_value):
    assert (output == squeeze_list(sample, fill_value))