Click here to Skip to main content
15,867,686 members
Articles / Artificial Intelligence / Tensorflow

Labelling Images for AI Model Training

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
27 Jan 2021CPOL3 min read 8.7K   8   1
In this article in the series, we label a face mask dataset.
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:

Image 1

Now, launch the app by issuing python3 labelImg.py. The application will start:

Image 2

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:

Image 3

Before starting the labeling process, make sure that the "YOLO" mode is selected on the left panel:

Image 4

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:

Image 5 

At the end of the process, you should get an image fully labeled:

Image 6

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:

Image 7

 

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!

This article is part of the series 'AI on the Edge: Face Mask Detection View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Sergio Virahonda grew up in Venezuela where obtained a bachelor's degree in Telecommunications Engineering. He moved abroad 4 years ago and since then has been focused on building meaningful data science career. He's currently living in Argentina writing code as a freelance developer.

Comments and Discussions

 
QuestionCan't Wait for the next step... Pin
adudley25628-Jan-21 2:58
adudley25628-Jan-21 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.