data_generator/object_detection_2d_data_generator_manga.py
def parse_json(self,
                   images_dirs,
                   annotations_filenames,
                   ground_truth_available=False,
                   include_classes = 'all',
                   ret=False):
        '''
        This is an JSON parser for the MS COCO datasets. It might be applicable to other datasets with minor changes to
        the code, but in its current form it expects the JSON format of the MS COCO datasets.

        Arguments:
            images_dirs (list, optional): A list of strings, where each string is the path of a directory that
                contains images that are to be part of the dataset. This allows you to aggregate multiple datasets
                into one (e.g. one directory that contains the images for MS COCO Train 2014, another one for MS COCO
                Val 2014, another one for MS COCO Train 2017 etc.).
            annotations_filenames (list): A list of strings, where each string is the path of the JSON file
                that contains the annotations for the images in the respective image directories given, i.e. one
                JSON file per image directory that contains the annotations for all images in that directory.
                The content of the JSON files must be in MS COCO object detection format. Note that these annotations
                files do not necessarily need to contain ground truth information. MS COCO also provides annotations
                files without ground truth information for the test datasets, called `image_info_[...].json`.
            ground_truth_available (bool, optional): Set `True` if the annotations files contain ground truth information.
            include_classes (list, optional): Either 'all' or a list of integers containing the class IDs that
                are to be included in the dataset. Defaults to 'all', in which case all boxes will be included
                in the dataset.
            ret (bool, optional): Whether or not the image filenames and labels are to be returned.

        Returns:
            None by default, optionally the image filenames and labels.
        '''
        self.images_dirs = images_dirs
        self.annotations_filenames = annotations_filenames
        self.include_classes = include_classes
        # Erase data that might have been parsed before.
        self.filenames = []
        self.image_ids = []
        self.labels = []
        if not ground_truth_available:
            self.labels = None

        # Build the dictionaries that map between class names and class IDs.
        with open(annotations_filenames[0], 'r') as f:
            annotations = json.load(f)
        # Unfortunately the 80 MS COCO class IDs are not all consecutive. They go
        # from 1 to 90 and some numbers are skipped. Since the IDs that we feed
        # into a neural network must be consecutive, we'll save both the original
        # (non-consecutive) IDs as well as transformed maps.
        # We'll save both the map between the original
        self.cats_to_names = {} # The map between class names (values) and their original IDs (keys)
        self.classes_to_names = [] # A list of the class names with their indices representing the transformed IDs
        self.classes_to_names.append('background') # Need to add the background class first so that the indexing is right.
        self.cats_to_classes = {} # A dictionary that maps between the original (keys) and the transformed IDs (values)
        self.classes_to_cats = {} # A dictionary that maps between the transformed (keys) and the original IDs (values)
        for i, cat in enumerate(annotations['categories']):
            self.cats_to_names[cat['id']] = cat['name']
            self.classes_to_names.append(cat['name'])
            self.cats_to_classes[cat['id']] = i + 1
            self.classes_to_cats[i + 1] = cat['id']

        # Iterate over all datasets.
        for images_dir, annotations_filename in zip(self.images_dirs, self.annotations_filenames):
            # Load the JSON file.
            with open(annotations_filename, 'r') as f:
                annotations = json.load(f)

            if ground_truth_available:
                # Create the annotations map, a dictionary whose keys are the image IDs
                # and whose values are the annotations for the respective image ID.
                image_ids_to_annotations = defaultdict(list)
                for annotation in annotations['annotations']:
                    image_ids_to_annotations[annotation['image_id']].append(annotation)

            # Iterate over all images in the dataset.
            for img in annotations['images']:

                self.filenames.append(os.path.join(images_dir, img['file_name']))
                self.image_ids.append(img['id'])

                if ground_truth_available:
                    # Get all annotations for this image.
                    annotations = image_ids_to_annotations[img['id']]
                    boxes = []
                    for annotation in annotations:
                        cat_id = annotation['category_id']
                        # Check if this class is supposed to be included in the dataset.
                        if (not self.include_classes == 'all') and (not cat_id in self.include_classes): continue
                        # Transform the original class ID to fit in the sequence of consecutive IDs.
                        class_id = self.cats_to_classes[cat_id]
                        xmin = annotation['bbox'][0]
                        ymin = annotation['bbox'][1]
                        width = annotation['bbox'][2]
                        height = annotation['bbox'][3]
                        # Compute `xmax` and `ymax`.
                        xmax = xmin + width
                        ymax = ymin + height
                        item_dict = {'image_name': img['file_name'],
                                     'image_id': img['id'],
                                     'class_id': class_id,
                                     'xmin': xmin,
                                     'ymin': ymin,
                                     'xmax': xmax,
                                     'ymax': ymax}
                        box = []
                        for item in self.labels_output_format:
                            box.append(item_dict[item])
                        boxes.append(box)
                    self.labels.append(boxes)

        if ret:
            return self.filenames, self.labels, self.image_ids

        This is an JSON parser for the MS COCO datasets. It might be applicable to other datasets with minor changes to
        the code, but in its current form it expects the JSON format of the MS COCO datasets.

        Arguments:
            images_dirs (list, optional): A list of strings, where each string is the path of a directory that
                contains images that are to be part of the dataset. This allows you to aggregate multiple datasets
                into one (e.g. one directory that contains the images for MS COCO Train 2014, another one for MS COCO
                Val 2014, another one for MS COCO Train 2017 etc.).
            annotations_filenames (list): A list of strings, where each string is the path of the JSON file
                that contains the annotations for the images in the respective image directories given, i.e. one
                JSON file per image directory that contains the annotations for all images in that directory.
                The content of the JSON files must be in MS COCO object detection format. Note that these annotations
                files do not necessarily need to contain ground truth information. MS COCO also provides annotations
                files without ground truth information for the test datasets, called `image_info_[...].json`.
            ground_truth_available (bool, optional): Set `True` if the annotations files contain ground truth information.
            include_classes (list, optional): Either 'all' or a list of integers containing the class IDs that
                are to be included in the dataset. Defaults to 'all', in which case all boxes will be included
                in the dataset.
            ret (bool, optional): Whether or not the image filenames and labels are to be returned.

        Returns:
            None by default, optionally the image filenames and labels.
        
This is an JSON parser for the MS COCO datasets. It might be applicable to other datasets with minor changes to
the code, but in its current form it expects the JSON format of the MS COCO datasets.
This is an JSON parser for the MS COCO datasets . It might be applicable to other datasets with minor changes to the code but in its current form it expects the JSON format of the MS COCO datasets .
def parse_json(self,
                   images_dirs,
                   annotations_filenames,
                   ground_truth_available=False,
                   include_classes = 'all',
                   ret=False):
        self.images_dirs = images_dirs
        self.annotations_filenames = annotations_filenames
        self.include_classes = include_classes
        self.filenames = []
        self.image_ids = []
        self.labels = []
        if not ground_truth_available:
            self.labels = None
        with open(annotations_filenames[0], 'r') as f:
            annotations = json.load(f)
        self.cats_to_names = {} 
        self.classes_to_names = [] 
        self.classes_to_names.append('background') 
        self.cats_to_classes = {} 
        self.classes_to_cats = {} 
        for i, cat in enumerate(annotations['categories']):
            self.cats_to_names[cat['id']] = cat['name']
            self.classes_to_names.append(cat['name'])
            self.cats_to_classes[cat['id']] = i + 1
            self.classes_to_cats[i + 1] = cat['id']
        for images_dir, annotations_filename in zip(self.images_dirs, self.annotations_filenames):
            with open(annotations_filename, 'r') as f:
                annotations = json.load(f)
            if ground_truth_available:
                image_ids_to_annotations = defaultdict(list)
                for annotation in annotations['annotations']:
                    image_ids_to_annotations[annotation['image_id']].append(annotation)
            for img in annotations['images']:
                self.filenames.append(os.path.join(images_dir, img['file_name']))
                self.image_ids.append(img['id'])
                if ground_truth_available:
                    annotations = image_ids_to_annotations[img['id']]
                    boxes = []
                    for annotation in annotations:
                        cat_id = annotation['category_id']
                        if (not self.include_classes == 'all') and (not cat_id in self.include_classes): continue
                        class_id = self.cats_to_classes[cat_id]
                        xmin = annotation['bbox'][0]
                        ymin = annotation['bbox'][1]
                        width = annotation['bbox'][2]
                        height = annotation['bbox'][3]
                        xmax = xmin + width
                        ymax = ymin + height
                        item_dict = {'image_name': img['file_name'],
                                     'image_id': img['id'],
                                     'class_id': class_id,
                                     'xmin': xmin,
                                     'ymin': ymin,
                                     'xmax': xmax,
                                     'ymax': ymax}
                        box = []
                        for item in self.labels_output_format:
                            box.append(item_dict[item])
                        boxes.append(box)
                    self.labels.append(boxes)
        if ret:
            return self.filenames, self.labels, self.image_ids
def parse_json ( self , images_dirs , annotations_filenames , ground_truth_available = False , include_classes = 'all' , ret = False ) : self . images_dirs = images_dirs self . annotations_filenames = annotations_filenames self . include_classes = include_classes self . filenames = [ ] self . image_ids = [ ] self . labels = [ ] if not ground_truth_available : self . labels = None with open ( annotations_filenames [ 0 ] , 'r' ) as f : annotations = json . load ( f ) self . cats_to_names = { } self . classes_to_names = [ ] self . classes_to_names . append ( 'background' ) self . cats_to_classes = { } self . classes_to_cats = { } for i , cat in enumerate ( annotations [ 'categories' ] ) : self . cats_to_names [ cat [ 'id' ] ] = cat [ 'name' ] self . classes_to_names . append ( cat [ 'name' ] ) self . cats_to_classes [ cat [ 'id' ] ] = i + 1 self . classes_to_cats [ i + 1 ] = cat [ 'id' ] for images_dir , annotations_filename in zip ( self . images_dirs , self . annotations_filenames ) : with open ( annotations_filename , 'r' ) as f : annotations = json . load ( f ) if ground_truth_available : image_ids_to_annotations = defaultdict ( list ) for annotation in annotations [ 'annotations' ] : image_ids_to_annotations [ annotation [ 'image_id' ] ] . append ( annotation ) for img in annotations [ 'images' ] : self . filenames . append ( os . path . join ( images_dir , img [ 'file_name' ] ) ) self . image_ids . append ( img [ 'id' ] ) if ground_truth_available : annotations = image_ids_to_annotations [ img [ 'id' ] ] boxes = [ ] for annotation in annotations : cat_id = annotation [ 'category_id' ] if ( not self . include_classes == 'all' ) and ( not cat_id in self . include_classes ) : continue class_id = self . cats_to_classes [ cat_id ] xmin = annotation [ 'bbox' ] [ 0 ] ymin = annotation [ 'bbox' ] [ 1 ] width = annotation [ 'bbox' ] [ 2 ] height = annotation [ 'bbox' ] [ 3 ] xmax = xmin + width ymax = ymin + height item_dict = { 'image_name' : img [ 'file_name' ] , 'image_id' : img [ 'id' ] , 'class_id' : class_id , 'xmin' : xmin , 'ymin' : ymin , 'xmax' : xmax , 'ymax' : ymax } box = [ ] for item in self . labels_output_format : box . append ( item_dict [ item ] ) boxes . append ( box ) self . labels . append ( boxes ) if ret : return self . filenames , self . labels , self . image_ids
This is an JSON parser for the MS COCO datasets.
This is an JSON parser for the MS COCO datasets .
'''
        This is an JSON parser for the MS COCO datasets. It might be applicable to other datasets with minor changes to
        the code, but in its current form it expects the JSON format of the MS COCO datasets.

        Arguments:
            images_dirs (list, optional): A list of strings, where each string is the path of a directory that
                contains images that are to be part of the dataset. This allows you to aggregate multiple datasets
                into one (e.g. one directory that contains the images for MS COCO Train 2014, another one for MS COCO
                Val 2014, another one for MS COCO Train 2017 etc.).
            annotations_filenames (list): A list of strings, where each string is the path of the JSON file
                that contains the annotations for the images in the respective image directories given, i.e. one
                JSON file per image directory that contains the annotations for all images in that directory.
                The content of the JSON files must be in MS COCO object detection format. Note that these annotations
                files do not necessarily need to contain ground truth information. MS COCO also provides annotations
                files without ground truth information for the test datasets, called `image_info_[...].json`.
            ground_truth_available (bool, optional): Set `True` if the annotations files contain ground truth information.
            include_classes (list, optional): Either 'all' or a list of integers containing the class IDs that
                are to be included in the dataset. Defaults to 'all', in which case all boxes will be included
                in the dataset.
            ret (bool, optional): Whether or not the image filenames and labels are to be returned.

        Returns:
            None by default, optionally the image filenames and labels.
        ''' # Erase data that might have been parsed before. # Build the dictionaries that map between class names and class IDs. # Unfortunately the 80 MS COCO class IDs are not all consecutive. They go # from 1 to 90 and some numbers are skipped. Since the IDs that we feed # into a neural network must be consecutive, we'll save both the original # (non-consecutive) IDs as well as transformed maps. # We'll save both the map between the original # The map between class names (values) and their original IDs (keys) # A list of the class names with their indices representing the transformed IDs # Need to add the background class first so that the indexing is right. # A dictionary that maps between the original (keys) and the transformed IDs (values) # A dictionary that maps between the transformed (keys) and the original IDs (values) # Iterate over all datasets. # Load the JSON file. # Create the annotations map, a dictionary whose keys are the image IDs # and whose values are the annotations for the respective image ID. # Iterate over all images in the dataset. # Get all annotations for this image. # Check if this class is supposed to be included in the dataset. # Transform the original class ID to fit in the sequence of consecutive IDs. # Compute `xmax` and `ymax`.
None by default, optionally the image filenames and labels.
None by default optionally the image filenames and labels .
self
images_dirs
A list of strings, where each string is the path of a directory that
contains images that are to be part of the dataset. This allows you to aggregate multiple datasets
into one .
A list of strings where each string is the path of a directory that contains images that are to be part of the dataset . This allows you to aggregate multiple datasets into one .
annotations_filenames
A list of strings, where each string is the path of the JSON file
that contains the annotations for the images in the respective image directories given, i.e. one
JSON file per image directory that contains the annotations for all images in that directory.
The content of the JSON files must be in MS COCO object detection format. Note that these annotations
files do not necessarily need to contain ground truth information. MS COCO also provides annotations
files without ground truth information for the test datasets, called `image_info_[...].json`.
A list of strings where each string is the path of the JSON file that contains the annotations for the images in the respective image directories given i . e . one JSON file per image directory that contains the annotations for all images in that directory . The content of the JSON files must be in MS COCO object detection format . Note that these annotations files do not necessarily need to contain ground truth information . MS COCO also provides annotations files without ground truth information for the test datasets called ` image_info_ [ ... ] . json ` .
ground_truth_available
Set `True` if the annotations files contain ground truth information.
Set ` True ` if the annotations files contain ground truth information .
include_classes
Either 'all' or a list of integers containing the class IDs that
are to be included in the dataset. Defaults to 'all', in which case all boxes will be included
in the dataset.
Either ' all ' or a list of integers containing the class IDs that are to be included in the dataset . Defaults to ' all ' in which case all boxes will be included in the dataset .
ret
Whether or not the image filenames and labels are to be returned.
Whether or not the image filenames and labels are to be returned .