Drowsiness-Detection-with-YoloV5

This repository consists of a drowsiness detection based on YOLOv5 implementation. You can reach the base repo here

1. Prepared Custom Data Set

A custom data set was prepared for this project. Videos were taken from 21 different people in scenarios that could happen while driving. Three different categories were discussed in these videos: normal, yawning and head position. Various light conditions and the use of glasses were taken into account. A total of 63 videos were obtained and labeling was done according to the method to be used.

2. Labeling Phase

The LabelImg program can be used for labeling in projects where the object detection method is used. Supports PASCAL VOC , YOLO and CreateML formats. Since training is done with Yolov5 in this project, the data is labeled in txt format. Turkish characters should not be used in labels.

2.1 LabelImg Installation for Windows:

Get repo

git clone https://github.com/tzutalin/labelImg.git

After creating and activating the virtual or anaconda environment, the following lines of code are run on the cmd screen.

pip install PyQt5

pip install lxml

pyrcc5 -o libs/resources.py resources.qrc

When the code below is run, LabelImg will be opened. For subsequent uses, it is sufficient to perform only last step.

python labelImg.py

Notes: After installing LabelImg, the ”predefined_classes.txt” file in the data folder can be emptied or the classes to be used can be written. In this way, problems that may occur during the labeling phase are prevented.

predefined_classes

There are 1975 labeled images in total for model training. 80% of this data is split as train and 20% as test. 4 classes were used as “normal, drowsy, drowsy#2, yawning”. “drowsy” includes eyes closed but head is upright, “drowsy#2” includes head dropping forward. It is labeled in two different ways so that the model does not make the wrong decision.

3. Training Phase

While the Yolov5 algorithm is preferred because it can produce high accuracy results even with little data, it is preferred because the nano model can be developed on embedded devices and the model takes up little space. The data folder structure should be as follows:

data_format

3.1 Editing YAML files

The data.yaml file holds the number and names of labels, the file path of the train and test data. This file should be located in the yolov5/data folder.

data_yaml

The nc value in the yolov5n_drowsy.yaml file has been changed to 4 as it represents the number of classes. This file should be located in the yolov5/models folder.

3.2 Training of the Model

python train.py  --resume --imgsz 640 --batch 16 --epochs 600 --data data/data.yaml --cfg models/yolov5n_drowsy.yaml --weights weights/yolov5n.pt  --name drowsy_result  --cache --device 0

The training is complete, as the model performed well at 173 epochs.

4. Drowsiness Detection with Trained Model

python drowsy_detect.py --weights runs/train/drowsy_result/weights/best.pt --source data/drowsy_training/test/images --hide-conf

Check this file drowsy_training_with_yolov5.ipynb for training

5. Result

5.1 Approach 1

app1

frame02-1072frame02-2132

5.2 Approach 2

nhtu

frame13-1120frame13-2006

GitHub

View Github