点击下方卡片,关注“计算机视觉之家”
视觉/图像重磅干货,第一时间送达!

相关部门一般都会建议司机朋友及时休息调整后再驾驶,避免酿成惨剧。

作为视觉开发人员,我们可否帮助驾驶人员设计一套智能检测嗜睡的系统,及时提醒驾驶员注意休息?如下图所示,本文将详细介绍如何使用Python和MediaPipe来实现一个嗜睡检测系统。

思路:疲劳驾驶的司机大部分都有打瞌睡的情形,所以我们根据驾驶员眼睛闭合的频率和时间来判断驾驶员是否疲劳驾驶(或嗜睡)。


由于我们专注于驾驶员睡意检测,在468个点中,我们只需要属于眼睛区域的标志点。眼睛区域有 32 个标志点(每个 16 个点)。为了计算 EAR,我们只需要 12 个点(每只眼睛 6 个点)。
以上图为参考,选取的12个地标点如下:
对于左眼: [362, 385, 387, 263, 373, 380]
对于右眼:[33, 160, 158, 133, 153, 144]
选择的地标点按顺序排列:P 1、 P 2、 P 3、 P 4、 P 5、 P 6
import cv2import numpy as npimport matplotlib.pyplot as pltimport mediapipe as mpmp_facemesh = mp.solutions.face_meshmp_drawing = mp.solutions.drawing_utilsdenormalize_coordinates = mp_drawing._normalized_to_pixel_coordinates%matplotlib inline
获取双眼的地标(索引)点。
# Landmark points corresponding to left eyeall_left_eye_idxs = list(mp_facemesh.FACEMESH_LEFT_EYE)# flatten and remove duplicatesall_left_eye_idxs = set(np.ravel(all_left_eye_idxs))# Landmark points corresponding to right eyeall_right_eye_idxs = list(mp_facemesh.FACEMESH_RIGHT_EYE)all_right_eye_idxs = set(np.ravel(all_right_eye_idxs))# Combined for plotting - Landmark points for both eyeall_idxs = all_left_eye_idxs.union(all_right_eye_idxs)# The chosen 12 points: P1, P2, P3, P4, P5, P6chosen_left_eye_idxs = [362, 385, 387, 263, 373, 380]chosen_right_eye_idxs = [33, 160, 158, 133, 153, 144]all_chosen_idxs = chosen_left_eye_idxs + chosen_right_eye_idx




上图:检测到地标P i的睁眼和闭眼。
底部:为视频序列的几帧绘制的眼睛纵横比 EAR。存在一个闪烁。
首先,我们必须计算每只眼睛的 Eye Aspect Ratio:

|| 表示L2范数,用于计算两个向量之间的距离。
为了计算最终的 EAR 值,作者建议取两个 EAR 值的平均值。

一般来说,平均 EAR 值在 [0.0, 0.40] 范围内。在“闭眼”动作期间 EAR 值迅速下降。
现在我们熟悉了 EAR 公式,让我们定义三个必需的函数:distance(…)、get_ear(…)和calculate_avg_ear(…)。
def distance(point_1, point_2):"""Calculate l2-norm between two points"""dist = sum([(i - j) ** 2 for i, j in zip(point_1, point_2)]) ** 0.5return dist
get_ear (…)函数将.landmark属性作为参数。在每个索引位置,我们都有一个NormalizedLandmark对象。该对象保存标准化的x、y和z坐标值。
def get_ear(landmarks, refer_idxs, frame_width, frame_height):"""Calculate Eye Aspect Ratio for one eye.Args:landmarks: (list) Detected landmarks listrefer_idxs: (list) Index positions of the chosen landmarksin order P1, P2, P3, P4, P5, P6frame_width: (int) Width of captured frameframe_height: (int) Height of captured frameReturns:ear: (float) Eye aspect ratio"""try:# Compute the euclidean distance between the horizontalcoords_points = []for i in refer_idxs:lm = landmarks[i]coord = denormalize_coordinates(lm.x, lm.y,frame_width, frame_height)coords_points.append(coord)# Eye landmark (x, y)-coordinatesP2_P6 = distance(coords_points[1], coords_points[5])P3_P5 = distance(coords_points[2], coords_points[4])P1_P4 = distance(coords_points[0], coords_points[3])# Compute the eye aspect ratioear = (P2_P6 + P3_P5) / (2.0 * P1_P4)except:ear = 0.0coords_points = Nonereturn ear, coords_points
最后定义了calculate_avg_ear(…)函数:
def calculate_avg_ear(landmarks, left_eye_idxs, right_eye_idxs, image_w, image_h):"""Calculate Eye aspect ratio"""left_ear, left_lm_coordinates = get_ear(landmarks,left_eye_idxs,image_w,image_h)right_ear, right_lm_coordinates = get_ear(landmarks,right_eye_idxs,image_w,image_h)Avg_EAR = (left_ear + right_ear) / 2.0return Avg_EAR, (left_lm_coordinates, right_lm_coordinates)
让我们测试一下 EAR 公式。我们将计算先前使用的图像和另一张眼睛闭合的图像的平均 EAR 值。
image_eyes_open = cv2.imread("test-open-eyes.jpg")[:, :, ::-1]image_eyes_close = cv2.imread("test-close-eyes.jpg")[:, :, ::-1]for idx, image in enumerate([image_eyes_open, image_eyes_close]):image = np.ascontiguousarray(image)imgH, imgW, _ = image.shape# Creating a copy of the original image for plotting the EAR valuecustom_chosen_lmk_image = image.copy()# Running inference using static_image_modewith mp_facemesh.FaceMesh(refine_landmarks=True) as face_mesh:results = face_mesh.process(image).multi_face_landmarks# If detections are available.if results:for face_id, face_landmarks in enumerate(results):landmarks = face_landmarks.landmarkEAR, _ = calculate_avg_ear(landmarks,chosen_left_eye_idxs,chosen_right_eye_idxs,imgW,imgH)# Print the EAR value on the custom_chosen_lmk_image.cv2.putText(custom_chosen_lmk_image,f"EAR: {round(EAR, 2)}", (1, 24),cv2.FONT_HERSHEY_COMPLEX,0.9, (255, 255, 255), 2)plot(img_dt=image.copy(),img_eye_lmks_chosen=custom_chosen_lmk_image,face_landmarks=face_landmarks,ts_thickness=1,ts_circle_radius=3,lmk_circle_radius=3)
结果:

如您所见,睁眼时的 EAR 值为0.28,闭眼时(接近于零)为 0.08。
【3】设计一个实时检测系统。

首先,我们声明两个阈值和一个计数器。
EAR_thresh: 用于检查当前EAR值是否在范围内的阈值。
D_TIME:一个计数器变量,用于跟踪当前经过的时间量EAR < EAR_THRESH.
WAIT_TIME:确定经过的时间量是否EAR < EAR_THRESH超过了允许的限制。
当应用程序启动时,我们将当前时间(以秒为单位)记录在一个变量中t1并读取传入的帧。
接下来,我们预处理并frame通过Mediapipe 的 Face Mesh 解决方案管道。
如果有任何地标检测可用,我们将检索相关的 ( Pi )眼睛地标。否则,在此处重置t1 和重置以使算法一致)。D_TIME (D_TIME
如果检测可用,则使用检索到的眼睛标志计算双眼的平均EAR值。
如果是当前时间,则加上当前时间和to之间的差。然后将下一帧重置为。EAR < EAR_THRESHt2t1D_TIMEt1 t2
如果D_TIME >= WAIT_TIME,我们会发出警报或继续下一帧。
参考链接:
https://learnopencv.com/driver-drowsiness-detection-using-mediapipe-in-python/
