Drowsiness detection system using openCV in python
The Project "Drowsiness Detection System " is made in Python programming language using OpenCV .
Source code→
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
''' ===================program to detect drowsiness and sound an alarm to wake up!===============
Programmed by Pawan [CID An Education Hub] Website: https://cideduhub.blogspot.com Youtube: https://www.youtube.com/c/cidaneducationhub GitHub: https://www.github.com/manav1918 Got hint by Adrian Sir [pyimagesearch]''' # imports import face_recognition import cv2 import time from scipy.spatial import distance as dist import playsound from threading import Thread import numpy as np #======================================================================================= # 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 to set off the alarm MIN_AER = 0.30 EYE_AR_CONSEC_FRAMES = 10 # initialize the frame counter as well as a boolean used to # indicate if the alarm is going off COUNTER = 0 ALARM_ON = False 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 horizontal # 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 def sound_alarm(alarm_file): # play an alarm sound playsound.playsound(alarm_file) def main(): global COUNTER video_capture = cv2.VideoCapture(0) while True: ret, frame = video_capture.read(0) # get it into the correct format #small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # get the correct face landmarks face_landmarks_list = face_recognition.face_landmarks(frame) # get eyes for face_landmark in face_landmarks_list: leftEye = face_landmark['left_eye'] rightEye = face_landmark['right_eye'] #eye aspect ratio for left and right eyes leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2 #========================converting left and right eye values in numpy arrays lpts = np.array(leftEye) rpts = np.array(rightEye) #==================showing line from left of left eye and right of right eye cv2.polylines(frame, [lpts],True ,(255,255,0), 1) cv2.polylines(frame, [rpts],True ,(255,255,0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < MIN_AER: COUNTER+= 1 # if the eyes were closed for a sufficient number of times # then sound the alarm if COUNTER >= EYE_AR_CONSEC_FRAMES: # if the alarm is not on, turn it on if not ALARM_ON: ALARM_ON = True t = Thread(target=sound_alarm, args=('alarm.wav',)) t.deamon = True t.start() # draw an alarm on the frame cv2.putText(frame, "ALERT! You are feeling asleep!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # otherwise, the eye aspect ratio is not below the blink # threshold, so reset the counter and alarm else: COUNTER = 0 ALARM_ON = False # draw the computed eye aspect ratio on the frame to help # with debugging and setting the correct eye aspect ratio # thresholds and frame counters cv2.putText(frame, "EAR: {:.2f}".format(ear), (500, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the frame cv2.imshow("Sleep detection program.", frame) # if the `q` key was pressed, break from the loop if cv2.waitKey(1) & 0xFF == ord('q'): break # do a bit of cleanup video_capture.release() cv2.destroyAllWindows() if __name__ == "__main__": for char in __doc__: print(char,end='') print() main() |
- Install all Required modules mentioned above
- Copy the project Code
- Save in .py extension and run.
[HOW TO USE]
Subscribe our Youtube Channel to get Updated
Bro, pls Help me to install all the packages.
ReplyDeleteHave you installed?
DeleteIf No, let me know,
U can just do pip install for installing. Read older blogs for more understanding.
face_landmarks_list = face_recognition.face_landmarks(frame)
ReplyDeleteshowing error
TypeError: __call__(): incompatible function arguments. The following argument types are supported:
1. (self: dlib.fhog_object_detector, image: array, upsample_num_times: int=0) -> dlib.rectangles
Invoked with: , False, 1
pls help
Are you using a speaker for the output of the alarm?
ReplyDeleteYes. But if u hv laptop, speaker will be inbuilt in it.
DeleteCan we develop a mobile app like this using python
ReplyDeleteThank you...
ReplyDeleteAuto Blur background Photo Editor & Blur image Background Effect on image app gives a stylish DSLR blurry effect on images with easy to finger pressing editing work. Also, use Blur background Photo Editor & blurry image app like an expensive DSLR camera blurry photo tool.
ReplyDelete