Here we’ll see how to properly label the resulting images in order to train a YOLO model.
Introduction
In the previous article of this series, we prepared face mask images for our AI model training. In this one, we’ll label these images. If you’re not very familiar with computer vision tasks, you may not have heard about LabelImg. It’s essentially an image labeling tool for computer vision. There are others – but this one is extremely easy to use, lightweight, YOLOv5-compatible, and free!
Get comfortable because this process will take hours.
To install the app, you can use several repositories. Clone this repository with git clone
. In the same terminal window, run the following commands for installation:
Ubuntu:
cd path/to/dir/LabelImg
sudo apt-get install pyqt5-dev-tools
sudo pip3 install -r requirements/requirements-linux-python3.txt
make qt5py3
MacOS (navigate first to the LabelImg directory)
brew install qt
pip3 install pyqt5 lxml
make qt5py3
Windows + Anaconda (navigate first to the LabelImg directory):
conda install pyqt=5
conda install -c anaconda lxml
pyrcc5 -o libs/resources.py resources.qrc
Once the installation has finished, you need to edit the predefined custom classes. Navigate to the labelImg/data directory and search for the predefined_classes.txt file. Open it and enter the labels you want to use in the app itself. For our face mask detection project, this is how they should look:
Now, launch the app by issuing python3 labelImg.py
. The application will start:
Now let’s get our hands dirty with image labeling. It requires a lot of patience, but you can do it!
Generating a Training Dataset from Images with LabelImg
Before initiating the labeling process, open up the file explorer and create a folder for the images you’ve augmented to be saved. Unzip the file you’ve obtained in the data augmentation step in that directory and create a folder within it called Labels, where we’ll save all the text files generated by LabelImg.
It’s time to label the images. Open LabelImg, click Open Dir on the left panel and select the folder that contains the image dataset. To select where the labels will be stored, click Change Save Dir and select the new Labels folder you’ve created a few lines above. Once that’s done, you’ll see the first image of the folder:
Before starting the labeling process, make sure that the "YOLO" mode is selected on the left panel:
Click Create RectBox and draw a square around each face in the image. Try to cover as much of each face as possible while not leaving large blank space inside the square. Every time you draw a square, a pop-up will appear prompting you to select a label that corresponds to the square:
At the end of the process, you should get an image fully labeled:
Notice the labeling summary on the right-hand side. Use it to keep track of what you’ve done on each image. Once you’re ready, click Save on the left panel, and then click Next Image.
After quite a few hours of fun, your dataset of more than 4,000 images is labeled. What do the resulting g .txt files contain? Let’s inspect one of them:
Each .txt file is linked to its corresponding image. For example, if there’s an image named 0.jpg, LabelImg will have generated a file called 0.txt that contains the coordinates for every label in the image. For each object, a new line is created, and its format is <object class number> <x> <y> <width> <height>
. This is known as the YOLO labeling format.
Next Step
With the dataset labeled, the time is ripe for the most important part. In the next article, we’re going to use the dataset to train a YOLOv5 object detection model. Stay tuned!