Posted on

In this article, we will talk about how we can train a face mask detector using YOLO and real-time face mask detection through web camera/image/video.


Let’s Start!

1. Train a Face Mask Detection Model

Before we start to train a deep learning model, we need to prepare dataset and I prepared it. You can download it here ( This dataset contains 3 classes of people, which are people who wearing mask, people who not wearing mask and people who wearing mask incorrectly. Few configuration files are required before we feed the dataset into Darknet for Object Detection Training. Files below we need to generate:

  • train.txt (A list of training images’ path)
  • valid.txt (A list of validation images’ path)
  • mask.names (A list of classes name)
  • (A list of configuration files’ path)
  • face.cfg (Neutral Network Configuration for Training)

a) train,txt

Content Examples of train.txt

b) valid.txt

Content Examples of valid.txt

c) face.names

Contents of face.names


Contents of

Inside, 1st line is number of classes (number of objects), 2nd is path to train.txt, 3rd is path to valid.txt, 4th is path to face.names and last line is the folder to save the model (weights file). In my case, I save the weights file in backup folder.

e) face.cfg

In our case, we will use YOLOv4-tiny as our network model. Inside Darknet cfg folder, there’s a file named yolov4-tiny.cfg. Open it and change configuration below:

  • Line 220 and 269: Change classes number to 3.
  • Line 212 and 263: Change filter number to 21.
  • Line 20: Change max batches to 6000.

After change the configuration above, save this config file (facev4-tiny.cfg) into a new folder named face_details and put this folder into Darknet folder. Next, put train.txt, valid.txt,, face.names into face_details too. Remember to double check the path for each files in

After all the configuration done, we can start training now. Open command prompt, change directory to Darknet folder using command cd. After that, run command below to start training:

darknet.exe detector train face_details/ face_details/facev4-tiny.cfg -map

After you run the command above, a visualization of loss value windows will show. The training time taken might very long depending on your GPU.

2. Result

Once the training process is done, your graph should shown similar as below.

Training Result

As you can see, the average loss is around 0.22 and mAP (Mean Average Precision) is 67%. I’m quite satisfy with this result. Later I will talk about how we can improve the average precision and decrease the training loss.

Let’s take a look on the result. If you not able to perform the training, you can download the model here. (

a) People who wearing a mask

People who wearing a mask

b) People who not wear a mask

People who not wear a mask

c) People who wear a mask incorrectly

People who wearing a mask incorrectly

As you can see, the result is quite good even the average precision is around 67%. In order to increase the performance, We can adjust input width and height of input image, number of batch size, and increase the iteration number in the configuration file.

Command to Perform Detection on:

  1. Image
darknet.exe detector test face_details/ face_details/facev4-tiny.cfg face_details/backup/facev4-tiny_final.weights <<Image Path>>
  1. Video
darknet.exe detector demo face_details/ face_details/facev4-tiny.cfg face_details/backup/facev4-tiny_final.weights -ext_output <<Video Path>>
  1. Web Camera
darknet.exe detector demo face_details/ face_details/facev4-tiny.cfg face_details/backup/facev4-tiny_final.weights -c 0


In conclusion, YOLO is a best Object Detection architecture to perform object detection process since it not required not much coding. Other than Object Detection, YOLO able us to perform Image Classification too. If you wish to have more customization, you can try Pytorch or TensorFlow to perform Image Classification. Feel free to leave comments below if you are facing any problems. Thanks.

2 Replies to “Face Mask Detector using YOLO”

  1. I’m amazed, I must say. Rarely do I encounter a blog that’s
    both educative and engaging, and let me tell you, you’ve hit the
    nail on the head. The problem is something which not enough men and women are speaking intelligently about.

    I am very happy I stumbled across this in my hunt for something regarding this.

Leave a Reply

Your email address will not be published. Required fields are marked *