Real-Time Mask Detection with YOLOv3 | Towards Data Science

We are all aware of the disastrous start of 2020, thanks to the Coronavirus pandemic. Life as we know it has come to a halt. Research has consistently shown that basic hygiene, such as hand washing and covering your mouth and nose while sneezing or coughing goes a long way. In such situations, wearing a mask in public places is essential.
In this post, I will talk about a custom object detector that I trained using Darknet, which detects if you have worn a mask or not. Darknet is an open-source neural network framework written in C and CUDA and supports CPU and GPU computation. Using Darknet, I trained a YOLO (You Only Look Once) object detector.
Very briefly speaking, YOLO detects all the objects in an image in one go. This post will not discuss the details of YOLO. Particulars of YOLO can be found in the papers here and here.
To get started, you will need a system running Linux with a GPU, preferably. Although you can setup Darknet on Windows using AlexAB’s implementation, I would strongly advise sticking to Linux, as the process is much simpler. Ok, I get it, you’re using Windows and you don’t want to go through the hassle of dual booting your laptop. Irrespective of your choice, this post will help get your object detector up and running! Other than this, I will be using OpenCV with Python 3.7. Feel free to use the version of Python you have installed. Make sure you also have the numpy and argparse python libraries installed.
TLDR; If you don’t want to go through the hassle of training the model for whatever reason, I’ve got you covered! Jump to the last section.
Installing Darknet on Linux is very straightforward. I suggest reading the official guide here. To get started, open up your terminal and type the following:
git clone https://github.com/pjreddie/darknet.gitcd darknetmake
If you have an NVIDIA supported GPU, you should consider installing CUDA as it reduces the training time significantly. This post has made the installation process simple and will save you a lot of time. To compile Darknet with CUDA and OpenCV, just open /darknet/Makefile and edit GPU=1 , CUDNN=1, OPENCV=1 and build the software with the make command.
For Windows, the process is a bit more complicated. Firstly, clone AlexAB’s repository:
git clone https://github.com/AlexeyAB/darknet.git
Once this is done, go to the requirements page of the repository:
https://github.com/AlexeyAB/darknet#requirements
Make sure you have all the requirements satisfied before continuing, and then head to this section:
https://github.com/AlexeyAB/darknet#how-to-compile-on-windows-using-cmake
Choose the method you want to build the software with. I suggest using the CMake method (which is also the recommended method by the author), as working with vcpkg can get tiresome.
Go ahead and clone my repository:
git clone https://github.com/rushad7/mask-detection.git
The folder structure should look something like:
.├── annotations├── cfg├── class_names├── data│   ├── mask│   └── no-mask├── misc├── results├── weights└── yolo
The data folder contains the mask and no-mask folders that contain the images of people with and without masks. The annotations folder contains all the images in the data folder, but with their respective .txt annotated files.
Copy the images and .txt files in the /mask-detection/annotations folder to the /darknet/obj/ folder. If you wish to add your training images, add them too to the folder above. To annotate the images, I used LabelImg, which is is a free and easy to use labeling tool supporting Linux, Windows, and Mac. While using it, just make sure you are saving the files in the YOLO format (can be set by clicking the button near the save button). Also, copy the /mask-detection/cfg/yolov3-custom.cfg file to the /darknet/cfg/folder and /mask-detection/yolo/voc.data file to the /darknet/cfg/ .
Run the /mask-detection/misc/test_train_split.py python script from the terminal(check the script for usage), it will generate train.txt and test.txt files in the /mask-detector/train_test/ folder. Open the /mask-detection/yolo/voc.data and edit the train and test variables with the path of the files above.
P.S. You might need to modify the paths depending on where on your system you are using Darknet and my repository. If you are using Windows, you will have to change the paths to the Windows equivalent ie. C:/Users/...
Lastly, download the convolutional weights of the darknet53 model from here and place it in your Darknet directory.
Now we’re all set up for training! To start training the model, go to your darknet directory and type:
./darknet detector train /cfg/voc.data cfg/yolov3-custom.cfg darknet53.conv.74
Depending on how many images you are training and whether it is on a CPU or GPU, the training time will vary. I trained this dataset on an NVIDIA GTX 1050, and it took me roughly 6 hours.
P.S. If you get an error regarding CUDA getting out of memory, try changing the batch size in the yolov3-voc.cfg file.
Now all you have to do is wait for the training to end! Darknet saves the weights every 100 iterations till the 1000th iteration, and then every 10,000 iterations in the /darknet/backup/ folder.
Go to /darknet/backup and copy the last generated .weights file to my repository.
For those who did not train the model, clone my GitHub repository, I have included the weights I trained in there.
To run the real-time mask detection simply run the yolo-live-cv2.py script from the terminal like:
python yolo-live-cv2.py --yolo yolo
Voila! , your YOLO Mask Detector is working! The detector performs reasonably well, but with about 10 to 15 FPS. It came to my attention that the accuracy of the model can be improved by increasing with size and quality of the data. I found this dataset on GitHub, which might help.