justdeepit.utils.ImageAnnotations
- class justdeepit.utils.ImageAnnotations(x=None)[source]
List of
ImageAnnocation
objectsImageAnnotations
storesImageAnnotation
class objects as a list object. To retrieve elements from anImageAnnotations
object, indexing[i]
and afor
loop can be used. To add new elements to the end ofImageAnnotations
, methodsappend
andextend
can be used. In addition, this class implements methodformat
to convert annotations into files in a specific format, such as COCO or Pascal VOC. Compared withformat
implemented in classImageAnnotation
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
isNone
, generate an emptyImageAnnocations
object. Ifx
isImageAnnocation
orImageAnnocations
object, convert them toImageAnnocations
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 theImageAnnotations
objectAdd an
ImageAnnotation
object to the end of theImageAnnotations
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 theImageAnnotations
objectConcatenate
ImageAnnotations
object or a list ofImageAnnotation
objects to the end of theImageAnnotations
object.- Parameters:
image_annotations (ImageAnnotations, list) – An
ImageAnnotations
object or a list ofImageAnnotation
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 toformat
implemented 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
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
isNone
, 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')