Annotator for Object Detection. A tool to generate annotated images for... | by Anuradha Wickramarachchi | Towards Data Science

In computer vision, object detection is an interesting field with numerous application. Like any other supervised machine learning task, we need annotation or labels for this very task as well. However, annotation of images can be a tedious time-consuming task for many of us (lazy ones?). I felt it quite strongly when I was writing my previous article, linked below.
towardsdatascience.com
So the big question comes;
How can we generate annotated images, automatically, with desired backgrounds?
Well, the answer is simple, use existing objects and overlay them on some backgrounds! There are several blogs on the internet that would say mostly how to do that. But it was hard for me to find a proper tool that would do everything for me so I can focus on the main problem, Object Detection.
So I present, The Object Annotation Maker. What does it do and how does it do them? Well, this article on that very utility.
It is rather easy to find images of objects than annotations. This is because capturing object images is easy and less labour. One good source of fruit images is the Fruits 360 dataset in Kaggle.
The dataset contains 100x100 images from fruits with labels as to what the fruit is.
This step is quite tasks specific. For example, if you were to detect pedestrians in a train station, you are better off with photos from the particular station. Or photos of your room (or a supermarket) if you are locating fruits in the room. This is quite easy to do and requires less effort in doing.
You can download the annotator from the following GitHub repository.
github.com
Running instructions are available in the Readme of the repository.
Organize your images as follows;
data-Fruits--Banana---img-1.jpg---img-2.jpg--Apple---img-1.jpg---img-2.jpg...-Backgronds--bg-1.jpg--bg-2.jpg--bg-3.jpg
The tool can do the following tasks for you;
Rescale the images — this is helpful if you want to lower the image size. For example, the most common image size for training is 224x224. So you need to have objects of size roughly 80x80. This option can be set by using the parameter --resize-images 80,80. You can provide both height and width or just height for scaled resizing ( —-resize-images 80).
Rescaling the backgrounds — This is similar to the previous step. Usually, background images will be very large. They can be rescaled easily using the parameter --resize-backgrounds 224,224.
The number of images per object — This determines the number of images to be generating having a particular object. Use --n-images 10 for example.
The number of objects in an image — Currently the tool supports overlaying multiple instances of the same image on the background. For example, 2 apples in the kitchen image. Can be achieved with the parameter --n-objects 2,5. You can give a single value or a range of values for a generation.
The program also takes the number of threads and seed as inputs for multi-threading and random generations. The input images can be given as a wild card list. For example, if you were to pick bananas and apple from the Fruit 260 database your command would look like below. I have omitted the above options for clarity.
python3 Maker.py -i "fruit260/Training/Banana/*.jpg" "fruit260/Training/Apple/*.jpg" -b "data/Backgrounds/8.jpg"
This makes it very easy to select multiple folders.
Please note that the images must come from a folder with the label. For example, always have banana images in a folder named Banana. You can have several such folders in different directories.
You need to specify an output folder to save the images. For the above dataset, you’ll have the following structure in the output directory you mention.
output-tmp (used for temporary data)-images--banana---img-0-banana-1.jpg---img-0-banana-2.jpg--apple---img-0-apple-1.jpg---img-0-apple-1.jpg-annotations--banana---img-0-banana-1.xml---img-0-banana-2.xml--apple---img-0-apple-1.xml---img-0-apple-1.xml
The program will automatically generate the VOC format XML for training. Now you can use these images for training your machine learning model. Happy coding!