疲劳驾驶检测系统:代码实战(基于OpenCV和Dlib,Python 3)

环境安装与配置(Windows 10)

步骤一:安装以下Python库

scipy、OpenCV、numpy、imutils、pyglet

建议用Anaconda 3。上面的库都可以用pip install XXX来安装,OpenCV的安装教程参考:Install OpenCV3 on Windows。

步骤二:安装Dlib(Python 3)

在安装Dlib之前,需要确保已经安装好Visual Studio 2015(update 3)和CMake。

这里附上资源下载链接:

Visual Studio 2015:https://pan.baidu.com/s/1kF3gQv3C0OKwIPAXcB-lvQ;提取码:9e8n

VS2015 update 3:https://pan.baidu.com/s/1KdpGtIsTB4x3WaiKHhib9Q;提取码:vh5e

备注:在VS2015 update 3中安装C++编译模块。

CMake安装:pip install cmake==3.8.2

下载Dlib-19.6压缩包:http://dlib.net/files/dlib-19.6.zip

cd dlib-19.6\
mkdir build
cd build

# This is a single command. Backticks are used for line continuation
cmake -G "Visual Studio 14 2015 Win64" `
-DJPEG_INCLUDE_DIR=..\dlib\external\libjpeg `
-DJPEG_LIBRARY=..\dlib\external\libjpeg `
-DPNG_PNG_INCLUDE_DIR=..\dlib\external\libpng `
-DPNG_LIBRARY_RELEASE=..\dlib\external\libpng `
-DZLIB_INCLUDE_DIR=..\dlib\external\zlib `
-DZLIB_LIBRARY_RELEASE=..\dlib\external\zlib `
-DCMAKE_INSTALL_PREFIX=install ..

cmake --build . --config Release --target INSTALL
cd ..

或者通过pip install dlib==19.16安装:

 

代码资源下载与运行

Github开源项目:driver-fatigue-detection-system

1. git clone https://github.com/raja434/driver-fatigue-detection-system.git

2. 下载预训练好的脸部标志检测器:dlib’s pre-trained facial landmark detector。把下载好的数据放在项目文件夹中(与alarm.wav同一目录下)。

3. 运行项目:python drowsiness detection.py

 

实验运行结果展示

疲劳驾驶检测系统:代码实战(基于OpenCV和Dlib,Python 3)_第1张图片   疲劳驾驶检测系统:代码实战(基于OpenCV和Dlib,Python 3)_第2张图片

左图为清醒-睁眼状态,EAR值为0.33;右图为疲劳-眯眼状态,EAR值为0.19,促发警报“Drowsiness Alert”并发出警报声音。

 

原理解析

这个简单的疲劳监测系统是通过计算眼睛纵横比(Eye Aspect Ratio)来估计疲劳程度的,源码如下:

def eye_aspect_ratio(eye):
	# compute the euclidean distances between the two sets of
	# vertical eye landmarks (x, y)-coordinates
	A = dist.euclidean(eye[1], eye[5])
	B = dist.euclidean(eye[2], eye[4])

	# compute the euclidean distance between the horizon
	# eye landmark (x, y)-coordinates
	C = dist.euclidean(eye[0], eye[3])

	# compute the eye aspect ratio
	ear = (A + B) / (2.0 * C)

	# return the eye aspect ratio
	return ear

代码中eye[0] -> eye[5]就是下图中的P1 -> P6,纵横比就是“眼睛宽度 / 眼睛高度”,这个也很好理解。

疲劳驾驶检测系统:代码实战(基于OpenCV和Dlib,Python 3)_第3张图片

下面设置预警的阈值为0.3,即EAR < 0.3时表示眨眼(blink)了一次或者处于闭眼状态,当闭眼帧数超过48时,触发警报。

# define two constants, one for the eye aspect ratio to indicate
# blink and then a second constant for the number of consecutive
# frames the eye must be below the threshold for to set off the
# alarm
EYE_AR_THRESH = 0.3
EYE_AR_CONSEC_FRAMES = 48

下面代码中把左眼和右眼的EAR值做平均,得到最终的EAR值。

# extract the left and right eye coordinates, then use the
# coordinates to compute the eye aspect ratio for both eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)

# average the eye aspect ratio together for both eyes
ear = (leftEAR + rightEAR) / 2.0

 

参考资料

1. Github开源项目:https://github.com/raja434/driver-fatigue-detection-system

2. Learn OpenCV:Install Dlib on Windows

你可能感兴趣的:(疲劳驾驶检测系统:代码实战(基于OpenCV和Dlib,Python 3))