justdeepit.utils.ImageAnnotations
- class justdeepit.utils.ImageAnnotations(x=None)[source]
List of
ImageAnnocationobjectsImageAnnotationsstoresImageAnnotationclass objects as a list object. To retrieve elements from anImageAnnotationsobject, indexing[i]and aforloop can be used. To add new elements to the end ofImageAnnotations, methodsappendandextendcan be used. In addition, this class implements methodformatto convert annotations into files in a specific format, such as COCO or Pascal VOC. Compared withformatimplemented in classImageAnnotationthat generates annotations in a specific format for a single image,formatin this class can generate annotations for multiple images.- Parameters:
x – If
xisNone, generate an emptyImageAnnocationsobject. IfxisImageAnnocationorImageAnnocationsobject, convert them toImageAnnocationsobject.
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
ImageAnnotationobject to the end of theImageAnnotationsobjectAdd an
ImageAnnotationobject to the end of theImageAnnotationsobject.- Parameters:
image_annotation (ImageAnnotation) – An
ImageAnnotationobject.
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
ImageAnnotationsobject to the end of theImageAnnotationsobjectConcatenate
ImageAnnotationsobject or a list ofImageAnnotationobjects to the end of theImageAnnotationsobject.- Parameters:
image_annotations (ImageAnnotations, list) – An
ImageAnnotationsobject or a list ofImageAnnotationobjects.
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
ImageAnnotationsobject to a specific format. COCO (.json) and Pascal VOC (.xml) are supported in current version. Different toformatimplemented inImageAnnotation, 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
Noneis given, return the converted annotation in string or dictionary.dataset_name (str) – Data set name. Some annotaiton requires dataset name.
- Returns:
If
file_pathisNone, return a string (for Pascal VOC format), a dictionary (for COCO), ornumpy.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')