使用卷积神经网络预防疲劳驾驶事故
作者|小白
来源|小白学视觉
疲劳驾驶:一个严重的问题
美国国家公路交通安全管理局估计,每年有 91,000 起车祸涉及疲劳驾驶的司机,造成约50,000 人受伤和近 800 人死亡。此外,每 24 名成年司机中就有 1 人报告在过去 30 天内在驾驶时睡着了。研究甚至发现,超过20个小时不睡觉相当于血液酒精浓度为0.08%——美国的法律规定的上限。
由于这个严重的问题,我和一组其他数据科学家开始开发一种神经网络,可以检测眼睛是否闭着,当与计算机视觉结合使用时,可以检测活人是否闭着眼睛超过一秒钟。这种技术对于任何对提高驾驶安全性感兴趣的人都很有用,包括商业和日常司机、汽车公司和汽车保险公司。
目录
构建卷积神经网络
网络摄像头应用程序
数据采集
我们使用了多个来源的完整面部数据,即麻省大学阿默斯特分校的睁眼面部数据和南京大学的闭眼面部数据。
然后,我们使用一个简单的 Python 函数从这个数据集中裁剪出眼睛,只剩下 30,000 多张裁剪后的眼睛图像。我们为每个图像裁剪添加了一个缓冲区,不仅可以获取眼睛,还可以获取眼睛周围的区域。此裁剪功能稍后将重新用于网络摄像头部分。
# installations fr
om command line
# brew install cmake # dlib requirement
# pip install dlib # face_recognition requirement
# pip install face_recognition # library for detecting eye location
# imports:
from PIL import Image, ImageDraw
import face_recognition
import os
def eye_cropper(folders):# Establish count for iterative file savingcount = 0# For loop going through each image filefor folder in os.listdir(folders):for file in os.listdir(folders + '/' + folder):# Using Facial Recognition Library on Imageimage = face_recognition.load_image_file(folders + '/' + folder + '/' + file)# create a variable for the facial feature coordinatesface_landmarks_list = face_recognition.face_landmarks(image)# create a placeholder list for the eye coordinateseyes = []try:eyes.append(face_landmarks_list[0]['left_eye'])eyes.append(face_landmarks_list[0]['right_eye'])except:continue# establish the max x and y coordinates of the eyefor eye in eyes:x_max = max([coordinate[0] for coordinate in eye])x_min = min([coordinate[0] for coordinate in eye])y_max = max([coordinate[1] for coordinate in eye])y_min = min([coordinate[1] for coordinate in eye])# establish the range of x and y coordinates x_range = x_max - x_miny_range = y_max - y_min# to make sure the full eye is captured,# calculate the coordinates of a square that has 50%# cushion added to the axis with a larger rangeif x_range > y_range:right = round(.5*x_range) + x_maxleft = x_min - round(.5*x_range)bottom = round(((right-left) - y_range))/2 + y_maxtop = y_min - round(((right-left) - y_range))/2else:bottom = round(.5*y_range) + y_maxtop = y_min - round(.5*y_range)right = round(((bottom-top) - x_range))/2 + x_maxleft = x_min - round(((bottom-top) - x_range))/2#crop original image using the cushioned coordinatesim = Image.open(folders + '/' + folder + '/' + file)im = im.crop((left, top, right, bottom))# resize image for input into our modelim = im.resize((80,80))# save file to output folderim.save('yourfolderforcroppedeyes')# increase count for iterative file savingcount += 1# print count every 200 photos to monitor progressif count % 200 == 0:print(count)# Call function to crop full-face eye images
eye_cropper('yourfullfaceimagefolder')
以下是我们用来训练模型的数据示例:
创建卷积神经网络
确定指标
因为预测正面类别(熟睡的司机)对我们来说比预测负面类别(清醒的司机)更重要,所以我们最重要的指标是召回率(敏感性)。召回率越高,模型错误地预测清醒(假阴性)的睡眠驱动程序的数量就越少。
这里唯一的问题是我们的正面类别明显多于我们的负面类别。因此,最好使用F1 分数或 Precision-Recall AUC 分数,因为它们还考虑了我们猜测驾驶员睡着但实际上清醒(精确)的次数。否则我们的模型将始终预测我们处于睡眠状态,无法使用。另一种我们在处理不平衡图像数据时没有使用的方法是使用图像增强,我没有在这里使用它,但是 Jason Brownlee 在解释如何在这里使用它方面做得很好。
准备图像数据
下一步是导入图像并用模型进行预处理。
本节所需的导入:
import cv2
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential
from keras.layers import Dense,Flatten,Conv2D,MaxPooling2D,
导入我们之前创建的图像并调整图像大小,使它们全部匹配,对于这个项目,我们将大小调整为 80x80 像素。这是一个使用 OS 库的简单导入函数:
def load_images_from_folder(folder, eyes = 0):count = 0error_count = 0images = []for filename in os.listdir(folder):try:img = cv2.imread(os.path.join(folder,filename))img = cv2.resize(img, (80,80)) ## Resizing the images## for eyes if it is 0: open, 1: closeimages.append([img, eyes])except:error_count += 1print('ErrorCount = ' + str(error_count))continuecount += 1if count % 500 == 0:print('Succesful Image Import Count = ' + str(count))return imagesfolder="../data/train/new_open_eyes"
open_eyes = load_images_from_folder(folder, 0)folder="../data/train/New_Closed_Eyes"
closed_eyes = load_images_from_folder(folder, 1)
eyes = close_eyes + open_eyes
设置变量,独立的 X 是图像,依赖的 y 是相应的标签(1 表示闭眼,0 表示睁眼):
X = []
y = []
for features, label in eyes: X.append(features)y.append(label)
将图像转换为数组,以便它可以进入模型。此外,将数据除以255进行缩放。
X = np.array(X).reshape(-1, 80, 80, 3)
y = np.array(y)
X = X/255.0
使用scikit learn的train_test_Split将数据拆分为训练集和验证集。重要提示:确保分层,因为我们有不平衡的类。
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y)
创建模型架构
卷积层:
该层创建像素子集而不是完整图像,并允许更快的模型。根据设置的过滤器数量,这可能比原始图像的密度更高或更低,但它们将使模型能够使用更少的资源了解更复杂的关系。我们使用 32 个过滤器,使用至少一个卷积层,通常需要两个或更多。对我们来说,最佳设置是将两个3x3组合在一起,然后将三个3x3组合在一起。CNN 的总体趋势是使用较小的滤波器尺寸。事实上,双3x3层与5x5层基本相同,但速度更快,通常会产生更好的分数。
压平
确保展平图像阵列,以便它可以进入密集层。
密集层
层越密集,我们的模型训练所需的时间就越长,随着这些层中神经元数量的增加,网络学习到的关系的复杂性也会增加。一般来说,通常卷积层的想法是为了避免产生过深的密集层方案。在我们的模型中我们使用了三层,神经元的relu激活率呈下降趋势(256、128、64)。我们还在每一层之后使用了 30% 的dropout。
输出层
最后,因为这是一个二进制分类问题,请确保对外层使用 sigmoid 激活。
编译模型
在 model.compile()中,我们需要将指标设置为 PR AUC(tf.keras.metrics.AUC (curve = 'PR')在 tensorflow 中)或召回率(tf.keras.metrics.recall在 tensorflow 中)。将损失设置为二进制交叉熵,因为这通常是一个二进制分类模型和一个好的优化器。
拟合模型
将批量大小设置得尽可能大。我在 Google Colab 的 32 GB TPU 上运行了 gridsearch,它轻松运行了 1000 多个批次。如有疑问,请尝试 32 个批次,如果没有使内存过载,则增加。就epochs 而言,20 个 epochs 后收益递减,所以我不会比这个特定的 CNN 高太多。
以下是 Tensorflow Keras 的完整设置:
# Instantiate the model
model = Sequential()# Adding first three convolutional layers
model.add(Conv2D(filters = 32, # number of filterskernel_size = (3,3), # height/width of filteractivation = 'relu', # activation function input_shape = (80,80,3) # shape of input (image)))
model.add(Conv2D(filters = 32, # number of filterskernel_size = (3,3), # height/width of filteractivation = 'relu' # activation function ))
model.add(Conv2D(filters = 32, # number of filterskernel_size = (3,3), # height/width of filteractivation = 'relu' # activation function ))# Adding pooling after convolutional layers
model.add(MaxPooling2D(pool_size = (2,2))) # Dimensions of the region that you are pooling# Adding second set of convolutional layers
model.add(Conv2D(filters = 32, # number of filterskernel_size = (3,3), # height/width of filteractivation = 'relu' # activation function ))
model.add(Conv2D(filters = 32, # number of filterskernel_size = (3,3), # height/width of filteractivation = 'relu' # activation function ))# Add last pooling layer.
model.add(MaxPooling2D(pool_size=(2,2)))model.add(Flatten())# Adding first dense layer with 256 nodes
model.add(Dense(256, activation='relu'))# Adding a dropout layer to avoid overfitting
model.add(Dropout(0.3))model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3)) model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))# adding output layer
model.add(Dense(1, activation = 'sigmoid'))# compiling the model
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=[tf.keras.metrics.AUC(curve = 'PR')])# fitting the model
model.fit(X_train,y_train,batch_size=800,validation_data=(X_test, y_test),epochs=24)# evaluate the model
model.evaluate(X_test, y_test, verbose=1)
曲线下的最终精确召回区域:
0.981033
创建网络摄像头应用程序
获得满意的模型后,请使用model.save('yourmodelname.h5'). 保存生产模型时,请确保运行该模型时没有验证数据,这将在导入时导致问题。
安装和导入:
这些是 Mac 优化的,尽管也可以在 Windows 上使用相同的脚本。
# installations needed for webcam application
# pip install opencv-python #
# if you want to play a sound for the alert:
# pip install -U PyObjC
# pip install playsound
# imports for webcam application
import cv2
from playsound import playsound
# import model saved above
eye_model = keras.models.load_model(‘best_model.h5’)
使用 OpenCV 访问网络摄像头
使用cv2.VideoCapture(0)启动摄像头捕获。如果想根据相对帧大小而不是绝对坐标确定文本位置,请确保使用cap.get(cv2.cap\u PROP\u frame\u width)保存网络摄像头的宽度和高度,还可以每秒查看帧数。
cap = cv2.VideoCapture(0)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(cap.get(cv2.CAP_PROP_FPS))
if not cap.isOpened():raise IOError(‘Cannot open webcam’)
使用 OpenCV 捕获帧并对其进行裁剪
如果我们打算用框架数数闭着的眼睛,一定要设置一个计数器。A while True:循环将使相机保持开启状态,直到我们完成脚本。在 while 循环中,使用ret, frame = cap.read()格式来捕获网络摄像头视频的帧。最后,调用框架上的函数。它应该从帧中返回一个裁剪过的眼睛,如果在帧中找不到眼睛,函数将返回不能除以255的None,并跳到下一帧。
counter = 0
# create a while loop that runs while webcam is in use
while True:# capture frames being outputted by webcamret, frame = cap.read()# function called on the frameimage_for_prediction = eye_cropper(frame)try:image_for_prediction = image_for_prediction/255.0except:continue
通过模型运行框架
然后我们可以通过模型运行图像并获得预测。如果预测值更接近于 0,那么我们在屏幕上显示“Open”,否则(即它更接近 1),我们显示“Closed”。请注意,如果模型检测到睁开眼睛,计数器将重置为 0,如果眼睛闭上,则计数器增加 1。我们可以使用cv2.putText()显示一些基本文本来指示眼睛是闭着的还是睁开的。
prediction = eye_model.predict(image_for_prediction)
if prediction < 0.5:counter = 0status = ‘Open’cv2.putText(frame, status, (round(w/2)-80,70),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2, cv2.LINE_4)else:counter = counter + 1status = ‘Closed’cv2.putText(frame, status, (round(w/2)-104,70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2, cv2.LINE_4)
如果一行中有6帧是闭着眼睛的(“睡眠”),我们还希望显示一个警报。这可以使用一个简单的if语句来完成:
if counter > 5:cv2.putText(frame, ‘DRIVER SLEEPING’, (round(w/2)-136,round(h) — 146), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_4)counter = 5
最后,我们需要显示帧并为 while 循环提供退出键。在cv2.waitKey(1)确定帧的显示时间。括号中的数字是帧将显示的毫秒数,除非按下“k”键(在本例中为27)或escape键:
cv2.imshow(‘Drowsiness Detection’, frame)
k = cv2.waitKey(1)
if k == 27:break
在循环之外,释放网络摄像头并关闭应用程序:
cap.release()
cv2.destroyAllWindows()
最终产品
加上一些文体,这是最终产品。
正如我们所见,该模型非常有效,尽管训练时间很长,但仍可在几毫秒内返回预测。通过一些进一步的改进并导出到外部机器,这个程序可以很容易地应用于实际情况,甚至可以挽救生命。
import cv2
import numpy as np
from playsound import playsound
from PIL import Image, ImageDraw
import face_recognition
from tensorflow import keras
eye_model = keras.models.load_model('best_model_2.h5')# webcam frame is inputted into function
def eye_cropper(frame):# create a variable for the facial feature coordinatesfacial_features_list = face_recognition.face_landmarks(frame)# create a placeholder list for the eye coordinates# and append coordinates for eyes to list unless eyes# weren't found by facial recognitiontry:eye = facial_features_list[0]['left_eye']except:try:eye = facial_features_list[0]['right_eye']except:return# establish the max x and y coordinates of the eyex_max = max([coordinate[0] for coordinate in eye])x_min = min([coordinate[0] for coordinate in eye])y_max = max([coordinate[1] for coordinate in eye])y_min = min([coordinate[1] for coordinate in eye])# establish the range of x and y coordinatesx_range = x_max - x_miny_range = y_max - y_min# in order to make sure the full eye is captured,# calculate the coordinates of a square that has a# 50% cushion added to the axis with a larger range and# then match the smaller range to the cushioned larger rangeif x_range > y_range:right = round(.5*x_range) + x_maxleft = x_min - round(.5*x_range)bottom = round((((right-left) - y_range))/2) + y_maxtop = y_min - round((((right-left) - y_range))/2)else:bottom = round(.5*y_range) + y_maxtop = y_min - round(.5*y_range)right = round((((bottom-top) - x_range))/2) + x_maxleft = x_min - round((((bottom-top) - x_range))/2)# crop the image according to the coordinates determined abovecropped = frame[top:(bottom + 1), left:(right + 1)]# resize the imagecropped = cv2.resize(cropped, (80,80))image_for_prediction = cropped.reshape(-1, 80, 80, 3)return image_for_prediction# initiate webcam
cap = cv2.VideoCapture(0)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
if not cap.isOpened():raise IOError('Cannot open webcam')# set a counter
counter = 0# create a while loop that runs while webcam is in use
while True:# capture frames being outputted by webcamret, frame = cap.read()# use only every other frame to manage speed and memory usageframe_count = 0if frame_count == 0:frame_count += 1passelse:count = 0continue# function called on the frameimage_for_prediction = eye_cropper(frame)try:image_for_prediction = image_for_prediction/255.0except:continue# get prediction from modelprediction = eye_model.predict(image_for_prediction)# Based on prediction, display either "Open Eyes" or "Closed Eyes"if prediction < 0.5:counter = 0status = 'Open'cv2.rectangle(frame, (round(w/2) - 110,20), (round(w/2) + 110, 80), (38,38,38), -1)cv2.putText(frame, status, (round(w/2)-80,70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2, cv2.LINE_4)x1, y1,w1,h1 = 0,0,175,75## Draw black backgroun rectanglecv2.rectangle(frame, (x1,x1), (x1+w1-20, y1+h1-20), (0,0,0), -1)## Add textcv2.putText(frame, 'Active', (x1 +int(w1/10), y1+int(h1/2)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255,0),2)else:counter = counter + 1status = 'Closed'cv2.rectangle(frame, (round(w/2) - 110,20), (round(w/2) + 110, 80), (38,38,38), -1)cv2.putText(frame, status, (round(w/2)-104,70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2, cv2.LINE_4)x1, y1,w1,h1 = 0,0,175,75## Draw black backgroun rectanglecv2.rectangle(frame, (x1,x1), (x1+w1-20, y1+h1-20), (0,0,0), -1)## Add textcv2.putText(frame, 'Active', (x1 +int(w1/10), y1+int(h1/2)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255,0),2)# if the counter is greater than 3, play and show alert that user is asleepif counter > 2:## Draw black background rectanglecv2.rectangle(frame, (round(w/2) - 160, round(h) - 200), (round(w/2) + 160, round(h) - 120), (0,0,255), -1)cv2.putText(frame, 'DRIVER SLEEPING', (round(w/2)-136,round(h) - 146), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2, cv2.LINE_4)cv2.imshow('Drowsiness Detection', frame)k = cv2.waitKey(1)## Soundplaysound('rooster.mov')counter = 1continuecv2.imshow('Drowsiness Detection', frame)k = cv2.waitKey(1)if k == 27:break
cap.release()
cv2.destroyAllWindows()
往
期
回
顾
资讯
AI 将有自我视觉?Facebook正在研究新系统
资讯
微软、英伟达联手推出最大语言模型
资讯
“倚天”一出,谁与争锋?阿里发布云芯片
技术
ST-GCN 实现人体姿态行为分类
分享
点收藏
点点赞
点在看
使用卷积神经网络预防疲劳驾驶事故相关推荐
- perclos嘴巴_基于人脸追踪和特征分析的疲劳驾驶预警系统设计
齐伟 张来刚 刘朝阳 摘 要:本文通过对人脸追踪和疲劳特征分析的研究设计了一个疲劳驾驶预警系统,通过特征人脸识别器来进行驾驶人身份的验证;通过坐标解算来获取驾驶人的头部姿态.电脑端将驾驶人的眨眼频率. ...
- 基于脑电功率的疲劳驾驶检测研究_kaic
基于脑电功率的疲劳驾驶检测研究 摘 要 在道路交通安全领域,疲劳驾驶是一种常见的交通安全隐患.现有数据统计,全球每年有大约21%的重大交通事故与疲劳驾驶有关,疲劳驾驶成为了诱发交通事故的主要原因之一 ...
- 自动驾驶系统进阶与项目实战(三)基于全卷积神经网络的点云三维目标检测和ROS实战
自动驾驶系统进阶与项目实战(三)基于全卷积神经网络的点云三维目标检测和ROS实战 前面入门系列的文章中我介绍了几种点云三维分割/目标检测模型,在做点云预处理上,有通过球面投射(SqueezeNet)得 ...
- 疲劳驾驶样本集_上海6辆集卡车追尾起火,集卡事故为何频发? | 港口圈
10月14日8时55分许,宝山公安分局接报警称,S20外圈江杨北路下匝道西侧发生一起多车事故,有车辆起火,造成外环西向东方向严重拥堵. 接警后,公安.消防迅速出警施救.经初查,现场共有6辆集装箱卡车发 ...
- 借助chat GPT阅读文献——基于改进的时间-图卷积网络的端到端EEG信号疲劳驾驶分类模型
借助chat GPT阅读文献--基于改进的时间-图卷积网络的端到端EEG信号疲劳驾驶分类模型 文章主要内容 文献原文 End-to-end fatigue driving EEG signal det ...
- 04.卷积神经网络 W3.目标检测(作业:自动驾驶 - 汽车检测)
文章目录 1. 问题背景 2. YOLO 模型 2.1 模型细节 2.2 分类阈值过滤 2.3 非极大值抑制 2.4 完成过滤 3. 在照片上测试已预训练的YOLO模型 3.1 定义类别.anchor ...
- 【深度学习基础】一步一步讲解卷积神经网络
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送 本文转自:一步一步讲解卷积神经网络 卷积神经网络(Convoluti ...
- matlab疲劳驾驶_第一本无人驾驶技术书
第一本无人驾驶技术书(第2版) 1.<第一本无人驾驶技术书(第2版)> 刘少山,唐洁,吴双,李力耘 等 著,2019-09-01, 电子工业出版社 适读人群 :本书适合人工智能.深度学习. ...
- 神经网络 卷积神经网络_如何愚弄神经网络?
神经网络 卷积神经网络 Imagine you're in the year 2050 and you're on your way to work in a self-driving car (pr ...
最新文章
- 全网唯一秃头数据集:20 万张人像,网罗各类秃头
- Windows Server 2008 R2 活动目录服务部署 (一)
- [HTML/CSS]盒子模型,块级元素和行内元素
- 在虚拟机环境下,电脑间拷贝配置好的伪分布式Hadoop环境,出现namenode不能启动的问题!...
- mysql粘贴数据_Navicat 如何复制粘贴数据
- python中基例_Python python从入门到实践(5) --代码复用
- 【转】RabbitMQ六种队列模式-2.工作队列模式
- opencv感兴趣通道COI的使用
- 【BZOJ2844】albus就是要第一个出场,线性基
- 计算机一级考试复习资料,全国计算机一级考试复习资料
- win7 32位php安装包下载地址,appserv官方下载|AppServ(php环境安装包)下载v8.6 64位/32位 支持win7/win8/win10_ IT猫扑网...
- 【算法大赛直播周】大赛评委分享广告技术干货,精彩不容错过!
- 几何画板画椭圆_几何画板降龙十九式视频教程每天只要十分钟
- UnboundLocalError: local vaiable 'XX' reference...
- ES6 中的 Symbol 是什么?
- struts2.xml中使用chain和redirectAction这两个类型结果(type-result)时,报检查错误(validation)...
- iOS逆向工程整理 HOOK微信抢红包
- 用stream流来遍历处理list,筛选出符合条件的list
- laravel mysql 时区_Laravel时区设置
- Jsonviewer2 for Notepad++ 64 bit