Python Library for Image Annotation Conversion

nipun deelaka
4 min readApr 11, 2022

This article target to give an introduction to the ImgAnn python library.

Photo by Kevin Crosby on Unsplash

Importance of Image Annotation conversion

Since deep learning came into the mainstream, it became a major role in the computer vision field. Interestingly, DL-based object detection, person/object recognition, semantic segmentation, Panoptical segmentation, etc. The research field solely depends on the bounding box annotated datasets whether those are rectangle, polygon, or boundary-string. On the one hand, the industry has several standard bounding box annotation formats. On the other hand, most research studies targeted only one annotation format, and different studies preferred different annotation formats.

Despite all generally, you would have a dataset with an annotation file that represents only one annotation format, whether you annotated a custom dataset by yourself or download some well-known dataset. However, in the development process or research project, you would have to run/implemented several fundamental models, [FR-CNN, R-CN, YoloV4, Yolo-Darknet] and most probably they are based on more than one bounding box annotation type. Ultimately, every good research based on bonding box annotation has a stage of converting annotation type.

Why ImgAnn

The story of the ‘imgann’, also starts with an object detection pet project for pedestrian counting. As you guessed, it ends with wasting numerous hours of training and post-processing with an incorrectly converted annotation dataset.

So as a result, the imgann library was created to avoid reinventing the wheel every time that we want to convert annotation datatype. As a functionality, the library provides single line annotation type convertibility from any acceptable [COCO, PascalVOC, CSV] to an acceptable type with very high precision of transformation.

Also, in the middle of the development process, we encountered several other important visualizations that will be worth providing with this library, such as a visual set of samples with their annotations from the dataset, giving a summary of annotation and the dataset itself.

The high-level architecture of the ImgAnn

*This part is dedicated to developers who face the same kind of challenge to give away that could implement the solution.

Functionalities of ImgAnn

Annotation type conversion

the library supports 3 main annotation types; COCO, PascalVOC, and CSV, in addition, the library has two extensions for both COCO and CSV annotation formats. In COCO, library support both KITTI bounding box annotation formats; 1 > [x-center, y-center, width, height] and 2 > [x-min, y-min, width, height]. In CSV, the library provides two downloading/resulting formats that targeted two domains of models; the first one for multi-object recognition task in TF standard form, the second one for multi-class multi-label classification task in standard one-hot encoded format. [visit library docs for more information]

Example code for converting annotation file from COCO to PascalVOC format:

from imgann import Convertor
Convertor.coco2voc(dataset_dir='../data/Hard Hat Sample.v5i.coco/test',
coco_ann_dir='../data/Hard Hat Sample.v5i.coco/test/_annotations.coco.json',
save_dir='../data/coco2voc)

as Illustrated, for whole tedious operation done within single line, and you have to only provide image data path and annotation file path only.

Annotation data preview

It’s really valuable to test the annotation of newly downloaded datasets or after some data preprocessing or annotation type conversation visually than by just looking at the annotation file. Hence, the ImgAnn library provides in-built support to preview datasets with annotation [supports any acceptable annotation type].

Example code for the annotation data preview:

from imgann import Sample
Sample.show_samples(data_path='../data/Hard Hat Sample.v5.voc/test',
ann_path='../data/Hard Hat Sample.v5.voc/test',
num_of_samples=5,
ann_type='voc',
seed=123,
image_shape=[500, 500])
an image out of function output

As Illustrated function has parameters to tweak to get outputs randomly, in different sizes. [visit library docs for more information]

Get Summary statistics of the dataset

In research and practice having summary statistics of the dataset is important in many ways. Evermore, It’s gives you a holistic idea of the datasets you are going to deal with.

Example code for the dataset description:

from imgann import Sample
Sample.describe_ann(data_path='../data/Hard Hat Sample.v5i.coco/train',
ann_path='../data/Hard Hat Sample.v5i.coco/train/_annotations.coco.json',
ann_type='coco')

>>> Result
INFO:imgann.sample:
IMAGE ANNOTATION SUMMARY
====================================================================
number of images : 210
folder image counts :
> train : 210
number of image sizes : 1
image_size : 416 X 416
number of object classes : 4
object classes : Workers | head | helmet | person
number of objects : 760
class object count :
> head : 186
> helmet : 553
> person : 21
====================================================================

under describing capabilities the library provided two distinct functions, those separately targeted on image datasets itself summarizing and image datasets summarizing with its’ annotations.

we really appreciate your input for the library in the form of GitHub PR for new functionality or issues with existing functionality. Also, we welcome any idea to improve library performance and aspect of user-friendliness.

Thank you for the reading, and hope you have gotten to know the new exciting library.

--

--