コード例 #1
0
ファイル: captions.py プロジェクト: gracecocks/dbcollection
    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
        # images
        if self.verbose:
            print('  > Processing image annotations... ')
        images = {}
        for i, annot in enumerate(annotations['images']):
            images[annot['id']] = {
                "file_name": os.path.join(image_dir, annot['file_name']),
                "width": annot['width'],
                "height": annot['height'],
                "id": annot['id'],
                "coco_url": annot['coco_url']
            }

        if self.verbose:
            print('  > Processing data annotations... ')
        for i, annot in enumerate(annotations['annotations']):
            img_id = annot['image_id']
            img_annotation = images[img_id]

            caption = annot["caption"]

            if img_id in data.keys():
                data[img_id]['captions'].append(caption)
            else:
                img_annotation = images[img_id]
                data[img_id] = {
                    "file_name": img_annotation['file_name'],
                    "width": img_annotation['width'],
                    "height": img_annotation['height'],
                    "id": img_annotation['id'],
                    "coco_url": img_annotation['coco_url'],
                    "captions": [caption]
                }

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

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

        return {set_name: [OrderedDict(sorted(data.items())),
                           annotations]}
コード例 #2
0
def load_data_test(set_name, image_dir, annotation_path, verbose=True):
    """
    Load test data annotations.
    """
    data = {}

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

    # parse annotations
    # images
    if verbose:
        print('> Processing image annotations...')
    for i, annot in enumerate(annotations['images']):
        data[annot['file_name']] = {
            "file_name": os.path.join(image_dir, annot['file_name']),
            "width": annot['width'],
            "height": annot['height'],
            "id": annot['id'],
            "coco_url": annot['coco_url'],
        }

    filename_ids = {}
    for i, annot in enumerate(annotations['images']):
        filename_ids[annot['file_name']] = i

    # categories
    if verbose:
        print('> Processing category annotations...')
    categories = {}
    category_list, supercategory_list, category_id = [], [], []
    for i, annot in enumerate(annotations['categories']):
        categories[annot['id']] = {
            "name": annot['name'],
            "supercategory": annot['supercategory'],
            "id": annot['id']
        }
        category_id.append(annot['id'])
        category_list.append(annot['name'])
        supercategory_list.append(annot['supercategory'])
    supercategory_list = list(set(supercategory_list))

    return {
        set_name: [
            OrderedDict(sorted(data.items())), filename_ids, annotations,
            category_list, supercategory_list, category_id
        ]
    }
コード例 #3
0
    def add_data_to_default(self, hdf5_handler, data, set_name):
        """
        Add data of a set to the default file.
        """
        object_fields = ['image_filenames', 'classes', 'boxes', 'boxesv', 'id', 'occlusion']
        image_filenames = []
        bbox = []
        bboxv = []
        lbl_id = []
        occlusion = []
        object_id = []

        list_image_filenames_per_class = []
        list_boxes_per_image = []
        list_boxesv_per_image = []
        list_object_ids_per_image = []
        list_objects_ids_per_class = []
        # list_objects_ids_per_id = []
        # list_objects_ids_per_occlusion= []

        if self.verbose:
            print('> Adding data to default file...')
            prgbar = progressbar.ProgressBar(max_value=len(data))

        img_counter = 0
        obj_counter = 0
        for i, set_data in enumerate(sorted(data)):
            for video in sorted(data[set_data]):
                img_fnames = data[set_data][video]["images"]
                annot_fnames = data[set_data][video]["annotations"]

                # cycle all images + annotations
                for j in range(0, len(img_fnames)):
                    # add image filename
                    image_filenames.append(img_fnames[j])

                    # load annotation file
                    annotation = load_json(annot_fnames[j])

                    obj_per_img = []
                    if any(annotation):
                        for obj in annotation:
                            # convert [x,y,w,h] to [xmin,ymin,xmax,ymax]
                            # discard any bbox smaller than 5px wide/high
                            if obj['pos'][2] >= 5 and obj['pos'][3] >= 5:
                                bb_correct_format = [obj['pos'][0],
                                                     obj['pos'][1],
                                                     obj['pos'][0] + obj['pos'][2] - 1,
                                                     obj['pos'][1] + obj['pos'][3] - 1]
                                bbox.append(bb_correct_format)
                                if isinstance(obj['posv'], list):
                                    # convert [x,y,w,h] to [xmin,ymin,xmax,ymax]
                                    bbv_correct_format = [obj['posv'][0],
                                                          obj['posv'][1],
                                                          obj['posv'][0] + obj['posv'][2] - 1,
                                                          obj['posv'][1] + obj['posv'][3] - 1]
                                    bboxv.append(bbv_correct_format)
                                else:
                                    bboxv.append([0, 0, 0, 0])
                                if isinstance(obj['id'], int):
                                    lbl_id.append(obj['id'])
                                else:
                                    lbl_id.append(0)
                                occlusion.append(obj['occl'])
                                class_lbl = self.classes.index(obj['lbl'])

                                # img, class, bbox, bboxv, id, occlusion
                                object_id.append([img_counter, class_lbl, obj_counter,
                                                  obj_counter, obj_counter, obj_counter])

                                obj_per_img.append(obj_counter)

                                # increment counter
                                obj_counter += 1

                    # add to lists
                    list_boxes_per_image.append(obj_per_img)
                    list_boxesv_per_image.append(obj_per_img)
                    list_object_ids_per_image.append(obj_per_img)

                    # increment counter
                    img_counter += 1

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

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

        if self.verbose:
            print('> Processing lists...')

        # Process lists
        for i in range(len(self.classes)):
            imgs_per_class = [val[0] for j, val in enumerate(object_id) if val[1] == i]
            imgs_per_class = list(set(imgs_per_class))  # get unique values
            imgs_per_class.sort()
            list_image_filenames_per_class.append(imgs_per_class)

        for i in range(len(self.classes)):
            objs_per_class = [j for j, val in enumerate(object_id) if val[1] == i]
            objs_per_class = list(set(objs_per_class))  # get unique values
            objs_per_class.sort()
            list_objects_ids_per_class.append(objs_per_class)

        # add data to hdf5 file
        hdf5_write_data(hdf5_handler, 'image_filenames', str2ascii(image_filenames),
                        dtype=np.uint8, fillvalue=0)
        hdf5_write_data(hdf5_handler, 'classes', str2ascii(self.classes),
                        dtype=np.uint8, fillvalue=0)
        hdf5_write_data(hdf5_handler, 'boxes', np.array(bbox, dtype=np.float),
                        fillvalue=-1)
        hdf5_write_data(hdf5_handler, 'boxesv', np.array(bboxv, dtype=np.float),
                        fillvalue=-1)
        hdf5_write_data(hdf5_handler, 'id', np.array(lbl_id, dtype=np.int32),
                        fillvalue=-1)
        hdf5_write_data(hdf5_handler, 'occlusion', np.array(occlusion, dtype=np.float),
                        fillvalue=-1)
        hdf5_write_data(hdf5_handler, 'object_ids', np.array(object_id, dtype=np.int32),
                        fillvalue=-1)
        hdf5_write_data(hdf5_handler, 'object_fields', str2ascii(object_fields),
                        dtype=np.uint8, fillvalue=0)

        pad_value = -1
        hdf5_write_data(hdf5_handler, 'list_image_filenames_per_class',
                        np.array(pad_list(list_image_filenames_per_class, pad_value),
                                 dtype=np.int32),
                        fillvalue=pad_value)
        hdf5_write_data(hdf5_handler, 'list_boxes_per_image',
                        np.array(pad_list(list_boxes_per_image, pad_value), dtype=np.int32),
                        fillvalue=pad_value)
        hdf5_write_data(hdf5_handler, 'list_boxesv_per_image',
                        np.array(pad_list(list_boxesv_per_image, pad_value), dtype=np.int32),
                        fillvalue=pad_value)
        hdf5_write_data(hdf5_handler, 'list_object_ids_per_image',
                        np.array(pad_list(list_object_ids_per_image, pad_value), dtype=np.int32),
                        fillvalue=pad_value)
        hdf5_write_data(hdf5_handler, 'list_objects_ids_per_class',
                        np.array(pad_list(list_objects_ids_per_class, pad_value), dtype=np.int32),
                        fillvalue=pad_value)

        if self.verbose:
            print('> Done.')
コード例 #4
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
            ]
        }
コード例 #5
0
ファイル: detection.py プロジェクト: gracecocks/dbcollection
 def load_annotation_file(self, path):
     """Loads the annotation's file data from disk."""
     return load_json(path)