# fatigue-detector
This is an OpenCV-based fatigue detection program.
![](https://github.com/lizhunan/asset/blob/main/fatigue-detector/pre.gif?raw=true)
<p align="lift">
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-4caf50.svg" alt="License"></a>
</a>
</p>
## Requirement
The code has used as few complex third-party libraries as possible, and tried to reduce the complexity of setting up the environment. However, there are still some required libraries that need to be imported:
- opencv-python 4.7.0.72
- dlib 19.24.2
- numpy 1.21.6
As a real-time face-swapping program, some necessary hardware is required, and the author's test environment is shown in the following table:
|CPU|GPU|Memory|OS|Camera|
|---|---|---|---|---|
|Intel(R) Core(TM) i7-1065G CPU @ 1.30GHz|Intel(R) Iris(R) Plus Graphics(Inessential)|16G|Windows 11|build-in(Essential)|
## How to Use
1. Clone the code from Github.
2. Download the [shape_predictor_68_face_landmarks.dat](http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2) which is a model file provided by dlib for face detection with 68 face landmarks.
3. Move the model file to the `./model`.
4. Run: `python run.py`
parameters:
- `--ear`: The mean aspect ratio for eyes. The ratio means that the eyes tend to be closed. The default value is 0.2.
- `--mar`: The mouth aspect ratio. The larger the ratio, the more inclined to open the mouth. The default value is 0.6.
- `--max_blinks`: The maximum number of blinks refers to the number of consecutive blinks in a certain period of time, and if the threshold is exceeded, it represents fatigue.
- `--max_yawning`: The maximum number of yawns refers to the number of yawns in a certain period of time, and if the threshold is exceeded, it represents fatigue.
## Principle
Because when people are tired, they will probably have two states:
- Blink: normal people's eyes blink about 10-15 times per minute, and each blink is about 0.2-0.4 seconds. If you are tired, the blink frequency will increase and the speed will slow down.
- Yawning: This is when the mouth grows large and stays in a certain state.
Therefore, whether a person is tired can be detected from the degree of opening and closing of the eyes, the blink rate, and the degree of opening and closing of the mouth to determine whether a person is tired.
### Method
1. Location of eyes and mouth.
2. Calculate the aspect ratio for eyes and mouth separately.
3. Estimate the fatigue state according to aspect ratio.
### EAR (eye aspect ratio)
In *[Real-Time Eye Blink Detection using Facial Landmarks](https://vision.fe.uni-lj.si/cvww2016/proceedings/papers/05.pdf)*, EAR is proposed.
In brief, when a person blinks, the distance of landmarks in eyes will be changed. So, the distance can be used to detect if an eye blinks.
![](https://github.com/lizhunan/asset/blob/main/fatigue-detector/ear.png?raw=true)
The EAR can be defined:
$$
EAR=\frac{\Vert p_2 - p_6 \Vert + \Vert p_3 - p_5 \Vert}{2\Vert p_1 - p_4 \Vert}
$$
The function is
``` python
def eye_aspect_ratio(eye):
A = np.linalg.norm(eye[1] - eye[5])
B = np.linalg.norm(eye[2] - eye[4])
C = np.linalg.norm(eye[0] - eye[3])
return (A + B) /(2.0 * C)
```
### MAR (mouth aspect ratio)
Similarly, the MAR can be calculated as well. the MAR can be calculated:
```python
def mouth_aspect_ratio(mouth):
A = np.linalg.norm(mouth[2] - mouth[10])
B = np.linalg.norm(mouth[4] - mouth[8])
C = np.linalg.norm(mouth[0] - mouth[6])
return (A + B) / (2.0 * C)
```
## License and Citations
The source code is published under MIT license, see [license file](./LICENSE) for details.
hakesashou
- 粉丝: 7152
- 资源: 1718
最新资源
- 【岗位说明】物业保安人员岗位职责.doc
- 【岗位说明】物业有限公司综合部厨工岗位职责.doc
- 【岗位说明】物业管理处环境部绿化工岗位职责.doc
- 龙门式6点伺服可调中频逆变电阻焊机sw19全套技术资料100%好用.zip
- 【岗位说明】陶瓷部门职能岗位职责.doc
- 【岗位说明】最新实用陶瓷企业生产规范岗位说明书.doc
- 【岗位说明】陶瓷部门职能岗位职责.docx
- 【岗位说明】电力公司岗位职责.docx
- 【岗位说明】电网公司岗位职责02.doc
- 【岗位说明】电力公司岗位职责.doc
- 【岗位说明】电力公司岗位职责说明02.doc
- 【岗位说明】电力施工项目经理岗位职责.doc
- 【岗位说明】供电公司部门职责.doc
- 【岗位说明】苏州电力公司人力资源部主任岗位职责描述.doc
- 【岗位说明】某电力公司客户经理岗位职责描述.doc
- 【岗位说明】石油开采岗位责任制.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈