justdeepit.utils.ImageAnnotation
- class justdeepit.utils.ImageAnnotation(image_path, annotation, annotation_format='auto', class2rgb=None, rgb2class=None)[source]
A container to store image annotations
Class
ImageAnnotationis a container that stores image annotations such as file paths, coordinates of bounding boxes and object contours, and object names.- Parameters:
image_path (str) – A path to an image.
annotation (str) – A path to an annotation file that corresponds to the image
image_path.annotation_format (str) – A string to specify an annotation format. If
autois specified, then automatically estimate the annotation format.rgb2class (dict) – A dictionary mapping RGB values to class name.
The image annotations are stored with the following attributes:
- image_path
A path to an image.
- Type:
str
- image
An image data in
numpy.ndarrayobject, which is generated byskimage.io.imread.- Type:
numpy.ndarray
- exif_orientation
An integer ranged from 1 until 8 to specify EXIF Orientation.
- Type:
int
- regions
A list of dictionaries consists of image annotations, and each dictionary consists of keys
id,class,bbox,contour, andscore.idis an integer that stores the object identifier, andclassis a string that stores a class label.bboxis a list consisting of four elements ([xmin, ymin, xmax, ymax]) that represent the coordinates of the bounding box, andcontouris a two-dimensional array (e.g.,[[x1, y1], [x2, y2], ..., [xn, yn]]) that stores the coordinates of object contours.scoreis the confidence score of the class, which is usually output from object detection models. If the object is a donut-shaped object (i.e., object with several holes), the holes are also annotated as an object, but theclassis set to __background__ in this case.- Type:
list
- mask
A mask of image stored in
numpy.ndarray.- Type:
numpy.ndarray
- class2rgb
A dictionary mapping class name to RGB values.
- Type:
dict
- rgb2class
A dictionary mapping RGB values to class name.
- Type:
dict
Class
ImageAnnotationmainly implements methodsformatanddrawfor format conversion and image visualization, respectively. By using methodformat, the annotations can be converted to any format, such as the COCO format and Pascal VOC format. By using methoddraw, the image is plotted with annotations.Examples
>>> from justdeepit.utils import ImageAnnotation >>> >>> image_fpath = './path/to/image.jpg' >>> ann_fpath = './path/to/image.json' >>> >>> ann = ImageAnnotation(image_fpath, ann_fpath)
- format(annotation_format, file_path=None)[source]
Format annotation to specific format
Method
formatconverts class objectImageAnnotationinto a specific format. COCO (.json) and Pascal VOC (.xml) are supported formats in the current version of JustDeepIt. Additionally, this method supports the conversion of class objectImageAnnotationinto a mask image represented by class objectnumpy.ndarray.- Parameters:
annotation_format (str) – A string to specify the format to be formatted.
json,coco,voc, ormaskcan be specified.file_path (str) – A path to save the converted annotation. If
Noneis given, return the converted annotation in string or dictionary.
- 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
>>> from justdeepit.utils import ImageAnnotation >>> >>> image_fpath = './path/to/image.jpg' >>> ann_fpath = './path/to/image.json' >>> ann = ImageAnnotation(image_fpath, ann_fpath) >>> >>> ann.format('voc', './path/to/image.xml')
- draw(fig_type='mask', file_path=None, label=False, score=False, alpha=1.0, class2rgb=None)[source]
Plot an image with annotations
Method
drawdepicts an image with annotations of class objectImageAnnotation. The output type can be specified by argumentfig_type, which can be specified asmask,rgbmask,masked,bbox, orcontour. Typemaskplots a mask image, whose background is shown in black and the objects are shown in white. Typergbmaskplots an RGB mask image, where the background is shown in black while the objects are shown with different colors, unlike type mask. Objects belonging to the same class are plotted with the same colors according toclass2rgb. Typemaskedplots masked images with black background while the object area is the same as that of the original images. Typebboxplots an image with bounding boxes around objects. Typecontourplots the contours of objects. In addition, multiple types can be specified simultaneously, such asmask+bboxandcontour+bbox.- Parameters:
fig_type (str) – A string to specify the figure type to be plotted. One of
mask,rgbmask,masked,bbox, orcontourcan be specified.file_path (str) – A path to save the converted annotation. If
Noneis given, return the image innumpy.ndarrayobject.label (bool) – Show class label of object on the image.
score (bool) – Show confidence score of object on the image if the score is not
None.alpha (float) – A decimal number between 0.0 and 1.0 to specify the transparence of mask.
class2rgb (dict) – A dictionary with a key as a class name and value as a RGB value. If
Noneis given, the preset colors will be used.
- Returns:
If
file_pathisNone, return the image data innumpy.ndarrayobject. Otherwise, save the data in the given path.
Examples
>>> import matplotlib.pyplot as plt >>> from justdeepit.utils import ImageAnnotation >>> >>> image_fpath = './path/to/image.jpg' >>> ann_fpath = './path/to/image.json' >>> >>> ann = justdeepit.utils.ImageAnnotation(image_fpath, ann_fpath) >>> >>> ann.draw('bbox')