One of the major causes behind the casualties of people in road accidents is driver’s drowsiness. After continuous driving for a long time, drivers easily get tired resulting in driver fatigue and drowsiness.
Every year many people lose their lives due to fatal road accidents around the world and drowsy driving is one of the primary causes of road accidents and death. Fatigue and micro sleep at the driving controls are often the root cause of serious accidents.
An important application of machine vision and image processing could be driver drowsiness detection system due to its high importance.
In this blog we are going to learn how me make a drowsiness detection by using dlib and opencv library.
First, you need to download the dlib for face point detections such as shape_predictor_68_face_landmarks
# ============================================================================= # 1. pip install dlib # 2. Download this Dlib facial Recongination from # https://github.com/davisking/dlib-models/blob/master/shape_predictor_68_face_landmarks.dat.bz2 # ============================================================================= import cv2 import dlib from scipy.spatial import distance def calculate_eye_aspect_ratio(eye): A = distance.euclidean(eye, eye) B = distance.euclidean(eye, eye) C = distance.euclidean(eye, eye) ear_aspect_ratio = (A+B)/(2.0*C) return ear_aspect_ratio cap = cv2.VideoCapture(0) hog_face_detector = dlib.get_frontal_face_detector() dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") while True: _, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = hog_face_detector(gray) for face in faces: face_landmarks = dlib_facelandmark(gray, face) leftEye =  rightEye =  for n in range(36,42): x = face_landmarks.part(n).x y = face_landmarks.part(n).y leftEye.append((x,y)) next_point = n+1 if n == 41: next_point = 36 x2 = face_landmarks.part(next_point).x y2 = face_landmarks.part(next_point).y cv2.line(frame,(x,y),(x2,y2),(0,255,0),1) for n in range(42,48): x = face_landmarks.part(n).x y = face_landmarks.part(n).y rightEye.append((x,y)) next_point = n+1 if n == 47: next_point = 42 x2 = face_landmarks.part(next_point).x y2 = face_landmarks.part(next_point).y cv2.line(frame,(x,y),(x2,y2),(0,255,0),1) left_ear = calculate_eye_aspect_ratio(leftEye) right_ear = calculate_eye_aspect_ratio(rightEye) EAR = (left_ear+right_ear)/2 EAR = round(EAR,2) if EAR <= 0.20: cv2.putText(frame,"Drowsiness Detection",(20,50), cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),4) cv2.putText(frame,"Warning",(20,100), cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),4) print("Drowsiness") print(EAR) cv2.imshow("Drowsiness Detection", frame) key = cv2.waitKey(1) if key == ord('q'): break # release the file pointers cap.release() cv2.destroyAllWindows()