疲勞駕駛檢測系統:代碼實戰(基於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

 

實驗運行結果展示

   

左圖爲清醒-睜眼狀態,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,縱橫比就是“眼睛寬度 / 眼睛高度”,這個也很好理解。

下面設置預警的閾值爲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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章