justdeepit.utils.ImageAnnotations

class justdeepit.utils.ImageAnnotations(x=None)[source]

List of ImageAnnocation objects

ImageAnnotations stores ImageAnnotation class objects as a list object. To retrieve elements from an ImageAnnotations object, indexing [i] and a for loop can be used. To add new elements to the end of ImageAnnotations, methods append and extend can be used. In addition, this class implements method format to convert annotations into files in a specific format, such as COCO or Pascal VOC. Compared with format implemented in class ImageAnnotation that generates annotations in a specific format for a single image, format in this class can generate annotations for multiple images.

Parameters:

x – If x is None, generate an empty ImageAnnocations object. If x is ImageAnnocation or ImageAnnocations object, convert them to ImageAnnocations object.

Examples

>>> from justdeepit.utils import ImageAnnotation, ImageAnnotations
>>> ann_1 = ImageAnnotation('image_1.jpg', 'image_1.xml')
>>> ann_2 = ImageAnnotation('image_2.jpg', 'image_2.xml')
>>> ann_3 = ImageAnnotation('image_3.jpg', 'image_3.xml')
>>> ann = justdeepit.utils.ImageAnnotations([ann_1, ann_2, ann_3])
append(image_annotation)[source]

Add an ImageAnnotation object to the end of the ImageAnnotations object

Add an ImageAnnotation object to the end of the ImageAnnotations object.

Parameters:

image_annotation (ImageAnnotation) – An ImageAnnotation object.

Examples

>>> import glob
>>> from justdeepit.utils import ImageAnnotation, ImageAnnotations
>>>
>>> anns = ImageAnnotations()
>>>
>>> for image_fpath in glob.glob(os.path.join(images_dpath, '*.jpg')):
>>>     ann_fpath = os.split.ext(image_fpath)[0] + '.xml'
>>>     ann = ImageAnnotation(image_fpath, ann_fpath, annotation_format='voc')
>>>     anns.append(ann)
>>>
>>> anns.fomrat('COCO', file_path='./images_annotation.json')
extend(image_annotations)[source]

Concatenate ImageAnnotations object to the end of the ImageAnnotations object

Concatenate ImageAnnotations object or a list of ImageAnnotation objects to the end of the ImageAnnotations object.

Parameters:

image_annotations (ImageAnnotations, list) – An ImageAnnotations object or a list of ImageAnnotation objects.

Examples

>>> import glob
>>> from justdeepit.utils import ImageAnnotation, ImageAnnotations
>>>
>>> anns_1 = ImageAnnotations()
>>> for image_fpath in glob.glob(os.path.join('subset_1', '*.jpg')):
>>>     ann_fpath = os.split.ext(image_fpath)[0] + '.xml'
>>>     ann = ImageAnnotation(image_fpath, ann_fpath, annotation_format='voc')
>>>     anns_1.append(ann)
>>>
>>> anns_2 = ImageAnnotations()
>>> for image_fpath in glob.glob(os.path.join('subset_2', '*.jpg')):
>>>     ann_fpath = os.split.ext(image_fpath)[0] + '.xml'
>>>     ann = ImageAnnotation(image_fpath, ann_fpath, annotation_format='voc')
>>>     anns_2.append(ann)
>>>
>>>
>>> anns = ImageAnnotations()
>>> anns.extend(anns_1)
>>> anns.extend(anns_2)
>>> anns.fomrat('COCO', file_path='./images_annotation.json')
>>>
format(annotation_format, file_path=None, dataset_name='dataset')[source]

Format annotaitons to specific format

Rearrange ImageAnnotations object to a specific format. COCO (.json) and Pascal VOC (.xml) are supported in current version. Different to format implemented in ImageAnnotation, COCO format annotations of multiple images are saved into single JSON file.

Parameters:
  • annotation_format (str) – A string to specify the format to be formatted.

  • file_path (str) – A path to save the converted annotation. If None is given, return the converted annotation in string or dictionary.

  • dataset_name (str) – Data set name. Some annotaiton requires dataset name.

Returns:

If file_path is None, return a string (for Pascal VOC format), a dictionary (for COCO), or numpy.ndarray (for image data). Otherwise, save the data in the given path.

Examples

>>> import glob
>>> from justdeepit.utils import ImageAnnotation, ImageAnnotations
>>>
>>> anns = ImageAnnotations()
>>>
>>> for image_fpath in glob.glob(os.path.join(images_dpath, '*.jpg')):
>>>     ann_fpath = os.split.ext(image_fpath)[0] + '.xml'
>>>     ann = ImageAnnotation(image_fpath, ann_fpath, annotation_format='voc')
>>>     anns.append(ann)
>>>
>>> anns.fomrat('COCO', file_path='./images_annotation.json')