Mask Detection using YOLOv5
Date: 2021-6-21
Author: 박성령, 김희락
Github:
Introduction
Due to Covid-19, the social distancing level is elevated and gathering more than 5 people is prohibited in Korea. We designed this program in order to detect face mask and the gathering of people using deep learning and computer vision. We use YOLOv5 which is a pre-trained object detection model in Google Colab and Visual Studio Code through Anaconda virtual environment.
1. Download Image and Annotation files
1.1. Download Zip file
(Annotation file's format : XML)
1.2. XML to Text(Yolov5) Conversion
Object detection by using YOLOv5 requires a compatible label format. Since the annotation downloaded from kaggle has a .xml format, we convert it to a .txt file according to the method presented.
This website provides data transformation method: https://app.roboflow.com
Sing up with E-mail or Github for free.
Skip tutorial and create new object detection project. Roboflow provides object detection type basically and you do not need to change.
Now, you can upload data to roboflow. Select the path to your images and annotations.
Then, the labels are indicated on the images in position.
You can assign the proportion of training, testing and validation set. For this step, we continue as default set since we do not need to separate data set. (You can set all data to single set such as training, testing, or validation)
You can preprocess the data such as 'Auto-Orient' and 'Resize' before converting. These process are recommanded but not essential and you can skip.
Click 'Generate' button on the bottom of the page and finish conversion.
Select YOLOv5 Pytorch data format and download.
2. Training
2.1. Recall converted image and annotation files
If you have run the code, you can see the images and label folders for train, test, and valid created in the file window on the left.
2.2. Recall YOLOv5 at github clone
2.3. Install Packages for YOLOv5
2.4. Check that the yaml file
Check that the data set information is written correctly in the yaml file.
2.5. Divide Dataset
Currently, the train and validation datasets are randomly divided. But we want to shuffle the data every time we train. So put all the data together and shuffle them randomly.
Create a list for all image files
Divide the train set and validation set by a ratio of 0.25.
Save the training image file and the image path of the validation image file as .txt file
2.6 Imageset Training
You can set the various parameters needed for training.
2.7 Training result
You can see the result of training at the tensorboard
and You MUST save the weights(best.pt) at the left file-folder at your own computer
(yolov5 -> runs -> weights -> best.pt)
3. Run Demo Video by using VSCODE
3.1. Setting the YOLOv5 Training Environment
Create Anaconda Environment
Install Required Packages
Download Yolov5 Zip_file
https://github.com/ultralytics/yolov5
run VScode
3.2. Customizing
Before modifying and executing the code, the previously downloaded weights file(best.pt) and test images must be placed in the yolov5 folder.
modifying detect.py
Detecting how many people per class by frame
Plot How many people per class by frame
modifying plots.py
Color classification by class
incorrect_wear_mask : class[0] - 'FFB330' (Orange)
with_mask : class[1] - '0B6CE3' (Blue)
without_mask : class[2] - 'FF0000' (Red)
RUN!
Last updated