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
ImageAnnotation
is 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
auto
is 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.ndarray
object, 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
.id
is an integer that stores the object identifier, andclass
is a string that stores a class label.bbox
is a list consisting of four elements ([xmin, ymin, xmax, ymax]
) that represent the coordinates of the bounding box, andcontour
is a two-dimensional array (e.g.,[[x1, y1], [x2, y2], ..., [xn, yn]]
) that stores the coordinates of object contours.score
is 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 theclass
is 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
ImageAnnotation
mainly implements methodsformat
anddraw
for 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
format
converts class objectImageAnnotation
into 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 objectImageAnnotation
into a mask image represented by class objectnumpy.ndarray
.- Parameters:
annotation_format (str) – A string to specify the format to be formatted.
json
,coco
,voc
, ormask
can be specified.file_path (str) – A path to save the converted annotation. If
None
is given, return the converted annotation in string or dictionary.
- 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
>>> 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
draw
depicts 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
. Typemask
plots a mask image, whose background is shown in black and the objects are shown in white. Typergbmask
plots 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
. Typemasked
plots masked images with black background while the object area is the same as that of the original images. Typebbox
plots an image with bounding boxes around objects. Typecontour
plots the contours of objects. In addition, multiple types can be specified simultaneously, such asmask+bbox
andcontour+bbox
.- Parameters:
fig_type (str) – A string to specify the figure type to be plotted. One of
mask
,rgbmask
,masked
,bbox
, orcontour
can be specified.file_path (str) – A path to save the converted annotation. If
None
is given, return the image innumpy.ndarray
object.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
None
is given, the preset colors will be used.
- Returns:
If
file_path
isNone
, return the image data innumpy.ndarray
object. 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')