{"id":20335721,"url":"https://github.com/0904-mansi/driver-drowsiness-detection-system","last_synced_at":"2026-03-16T20:04:29.467Z","repository":{"id":97521129,"uuid":"441691957","full_name":"0904-mansi/Driver-Drowsiness-Detection-System","owner":"0904-mansi","description":"In this project, we have created a driver drowsiness detection system that will detect whether the driver's eyes are closed for too long and detect whether the driver is sleepy or inactive.","archived":false,"fork":false,"pushed_at":"2022-01-30T09:51:05.000Z","size":40,"stargazers_count":10,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-11T22:07:23.913Z","etag":null,"topics":["cmake","dlib","face-recognition-python","machine-learning","numpy","opencv","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0904-mansi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-25T14:08:10.000Z","updated_at":"2024-01-27T08:57:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"0950db79-21f4-4c7f-8749-a19902f7f08e","html_url":"https://github.com/0904-mansi/Driver-Drowsiness-Detection-System","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0904-mansi%2FDriver-Drowsiness-Detection-System","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0904-mansi%2FDriver-Drowsiness-Detection-System/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0904-mansi%2FDriver-Drowsiness-Detection-System/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0904-mansi%2FDriver-Drowsiness-Detection-System/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0904-mansi","download_url":"https://codeload.github.com/0904-mansi/Driver-Drowsiness-Detection-System/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248487715,"owners_count":21112191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","dlib","face-recognition-python","machine-learning","numpy","opencv","python"],"created_at":"2024-11-14T20:44:22.572Z","updated_at":"2026-03-16T20:04:29.410Z","avatar_url":"https://github.com/0904-mansi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python-Assignment\n\n## Building Driver Drowsiness Detection System\n\n**Driver Drowsiness Detection with OpenCV \u0026 Dlib**\n\nIn this project, we are going to build a driver drowsiness detection system that will detect if the eyes of the driver are close for too long and infer if the driver is sleepy or inactive.\n\nThis can be an important safety implementation as studies suggest that accidents due to drivers getting drowsy or sleepy account for around 20% of all accidents and on certain long journey roads it’s up to 50%. It is a serious issue and most people that have driven for long hours at night can relate to the fact that fatigue and slight brief state of unconsciousness can happen to anyone and everyone.\n\nThere has been an increase in safety systems in cars \u0026 other vehicles and many are now mandatory in vehicles, but all of them cannot help if a driver falls asleep behind the wheel even for a brief moment. Hence that is what we are gonna build today – Driver Drowsiness Detection System\n\n## The libraries need for driver drowsiness detection system are\n\n1. Opencv\n2. Dlib\n3. Numpy\n\nThese are the only packages you will need for this machine learning project.\n\nOpenCV and NumPy installation is using pip install and dlib installation using pip only works if you have cmake and vs build tools 2015 or later (if on python version\u003e=3.7)\nThe easiest way is to create a python 3.6 env in anaconda and install a dlib wheel supported for python 3.6. \n\n## Import the libraries\nNumpy is used for handling the data from dlib and mathematical functions. Opencv will help us in gathering the frames from the webcam and writing over them and also displaying the resultant frames.\n\nDlib to extract features from the face and predict the landmark using its pre-trained face landmark detector.\n\nDlib is an open source toolkit written in c++ that has a variety of machine learning models implemented and optimized. Preference is given to dlib over other libraries and training your own model because it is fairly accurate, fast, well documented, and available for academic, research, and even commercial use.\n\nDlib’s accuracy and speed are comparable with the most state-of-the-art neural networks, and because the scope of this project is not to train one, we’ll be using dlib python wrapper Pretrained facial landmark model is available with the code, you can download it from there.\n\nThe hypot function from the math library calculates the hypotenuse of a right-angle triangle or the distance between two points (euclidean norm).\n```python\nimport numpy as np\nimport dlib\nimport cv2\nfrom math import hypot\n```\nHere we prepare our capture call to OpenCV’s video capture method that will capture the frames from the webcam in an infinite loop till we break it and stop the capture.\n```python\ncap = cv2.VideoCapture(0)\n```\n## Dlib’s face and facial landmark predictors\nKeep the downloaded landmark detection .dat file in the same folder as this code file or provide a complete path in the dlib.shape_predictor function.\n\nThis will prepare the predictor for further prediction.\n```python\n\ndetector = dlib.get_frontal_face_detector()\npredictor = dlib.shape_predictor(\"shape_predictor_68_face_landmarks.dat\")\n```\nWe create a function to calculate the midpoint from two given points.\n\nAs we are gonna use this more than once in a call we create a separate function for this.\n```python\ndef mid(p1 ,p2):\n    return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)\n```\n\n## Create a function for calculating the blinking ratio\nCreate a function for calculating the blinking ratio or the eye aspect ratio of the eyes. There are six landmarks for representing each eye.\n\n![](https://techvidvan.com/tutorials/wp-content/uploads/sites/2/2021/06/eye-aspect-ratio.jpg)\n\nStarting from the left corner moving clockwise. We find the ratio of height and width of the eye to infer the open or close state of the eye.blink-ratio=(|p2-p6|+|p3-p5|)(2|p1-p4|). The ratio falls to approximately zero when the eye is close but remains constant when they are open.\n\n\n```python\ndef eye_aspect_ratio(eye_landmark, face_roi_landmark):\n    left_point = (face_roi_landmark.part(eye_landmark[0]).x, face_roi_landmark.part(eye_landmark[0]).y)\n    right_point = (face_roi_landmark.part(eye_landmark[3]).x, face_roi_landmark.part(eye_landmark[3]).y)\n    center_top = mid(face_roi_landmark.part(eye_landmark[1]), face_roi_landmark.part(eye_landmark[2]))\n    center_bottom = mid(face_roi_landmark.part(eye_landmark[5]), face_roi_landmark.part(eye_landmark[4]))\n    hor_line_length = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))\n    ver_line_length = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))\n    ratio = hor_line_length / ver_line_length\n    return ratio\n ```\n    \n   ## Create a function for calculating mouth aspect ratio\nSimilarly, we define the mouth ratio function for finding out if a person is yawning or not. This function gives the ratio of height to width of mouth. If height is more than width it means that the mouth is wide open.\n\nFor this as well we use a series of points from the dlib detector to find the ratio.\n\n```python\ndef mouth_aspect_ratio(lips_landmark, face_roi_landmark):\n    left_point = (face_roi_landmark.part(lips_landmark[0]).x, face_roi_landmark.part(lips_landmark[0]).y)\n    right_point = (face_roi_landmark.part(lips_landmark[2]).x, face_roi_landmark.part(lips_landmark[2]).y)\n    center_top = (face_roi_landmark.part(lips_landmark[1]).x, face_roi_landmark.part(lips_landmark[1]).y)\n    center_bottom = (face_roi_landmark.part(lips_landmark[3]).x, face_roi_landmark.part(lips_landmark[3]).y)\n    hor_line_length = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))\n    ver_line_length = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))\n    if hor_line_length == 0:\n        return ver_line_length\n    ratio = ver_line_length / hor_line_length\n    return ratio\n ```\n We create a counter variable to count the number of frames the eye has been close for or the person is yawning and later use to define drowsiness in driver drowsiness detection system project Also, we declare the font for writing on images with opencv.\n```python\ncount = 0\nfont = cv2.FONT_HERSHEY_TRIPLEX\n```\n## Begin processing of frames\nCreating an infinite loop we receive frames from the opencv capture method.\n\nWe flip the frame because mirror image and convert it to grayscale. Then pass it to the face detector.\n```python\nwhile True:\n    _, img = cap.read()\n    img = cv2.flip(img,1)\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    faces = detector(gray)\n  ```\nWe loop if there are more than one face in the frame and calculate for all faces. Passing the face to the landmark predictor we get the facial landmarks for further analysis.\n\nPassing the points of each eye to the compute_blinking_ratio function we calculate the ratio for both the eyes and then take the mean of it.\n```python\n  for face_roi in faces:\n        landmark_list = predictor(gray, face_roi)\n        left_eye_ratio = eye_aspect_ratio([36, 37, 38, 39, 40, 41], landmark_list)\n        right_eye_ratio = eye_aspect_ratio([42, 43, 44, 45, 46, 47], landmark_list)\n        eye_open_ratio = (left_eye_ratio + right_eye_ratio) / 2\n        cv2.putText(img, str(eye_open_ratio), (0, 13), font, 0.5, (100, 100, 100))\n        ###print(left_eye_ratio,right_eye_ratio,eye_open_ratio)\n        #Similarly we calculate the ratio for the mouth to get yawning status, for both outer and inner lips to be more accurate and calculate its mean.\n        inner_lip_ratio = mouth_aspect_ratio([60,62,64,66], landmark_list)\n        outter_lip_ratio = mouth_aspect_ratio([48,51,54,57], landmark_list)\n        mouth_open_ratio = (inner_lip_ratio + outter_lip_ratio) / 2;\n        cv2.putText(img, str(mouth_open_ratio), (448, 13), font, 0.5, (100, 100, 100))\n        ###print(inner_lip_ratio,outter_lip_ratio,mouth_open_ratio)\n ```\n Now that we have our data we check if the mouth is wide open and the eyes are not closed. If we find that either of these situations occurs we increment the counter variable counting the number of frames the situation is persisting.\n\nWe also find the coordinates for the face bounding box\n\nIf the eyes are close or yawning occurs for more than 10 consecutive frames we infer the driver as drowsy and print that on the image as well as creating the bounding box red, else just create a green bounding box\n``python\nif mouth_open_ratio \u003e 0.380 and eye_open_ratio \u003e 4.0 or eye_open_ratio \u003e 4.30:\n    count +=1\nelse:\n    count = 0\nx,y = face_roi.left(), face_roi.top()\nx1,y1 = face_roi.right(), face_roi.bottom()\nif count\u003e10:\n    cv2.rectangle(img, (x,y), (x1,y1), (0, 0, 255), 2)\n    cv2.putText(img, \"Sleepy\", (x, y-5), font, 0.5, (0, 0, 255))\n    \nelse:\n    cv2.rectangle(img, (x,y), (x1,y1), (0, 255, 0), 2)\n    ``\nFinally, we show the frame and wait for the esc keypress to exit the infinite loop.\n\nAfter we exit the loop we release the webcam capture and close all the windows and exit the program.  \n\n## Driver Drowsiness Detection Output\n\n![](https://techvidvan.com/tutorials/wp-content/uploads/sites/2/2021/06/driver-drowsiness-detection-output.jpg)\n\n## Summary\nwe have successfully created driver drowsiness detector, we can implement it in other projects like computer vision, self-driving cars, drive safety, etc.\n\nDriver drowsiness project can be used with a raspberry pie to create a standalone system for drivers, used as a web service, or installed in workplaces to monitor employees’ activity. The sensitivity and the number of frames can be changed according to the requirements.\n\nMade with Team [Sanskriti Harmukh](https://github.com/SanskritiHarmukh) | [Satyam Jain](https://github.com/Satyam298) | [Archit Chawda](https://github.com/archit27-uo) 😃.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0904-mansi%2Fdriver-drowsiness-detection-system","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0904-mansi%2Fdriver-drowsiness-detection-system","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0904-mansi%2Fdriver-drowsiness-detection-system/lists"}