{"id":23388289,"url":"https://github.com/shamiul5201/drowsy_driver_detection","last_synced_at":"2026-04-29T23:32:00.177Z","repository":{"id":268366822,"uuid":"904121100","full_name":"shamiul5201/Drowsy_Driver_Detection","owner":"shamiul5201","description":"A real-time application that uses computer vision to detect drowsiness by monitoring eye movement and triggers an alarm to prevent fatigue-related incidents. Built with OpenCV, MediaPipe, Streamlit, and Pygame.","archived":false,"fork":false,"pushed_at":"2024-12-16T10:06:48.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T13:22:48.283Z","etag":null,"topics":["mediapipe","opencv","pygame","streamlit"],"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/shamiul5201.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":"2024-12-16T09:42:49.000Z","updated_at":"2025-03-11T06:58:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"e0ef1f1a-8246-4915-b771-c917fcc0585c","html_url":"https://github.com/shamiul5201/Drowsy_Driver_Detection","commit_stats":null,"previous_names":["shamiul5201/drowsy_driver_detection"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shamiul5201%2FDrowsy_Driver_Detection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shamiul5201%2FDrowsy_Driver_Detection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shamiul5201%2FDrowsy_Driver_Detection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shamiul5201%2FDrowsy_Driver_Detection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shamiul5201","download_url":"https://codeload.github.com/shamiul5201/Drowsy_Driver_Detection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247848366,"owners_count":21006240,"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":["mediapipe","opencv","pygame","streamlit"],"created_at":"2024-12-22T02:18:31.386Z","updated_at":"2026-04-29T23:32:00.145Z","avatar_url":"https://github.com/shamiul5201.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Drowsiness Detection System\n\nThis project implements a **drowsiness detection system** using OpenCV, MediaPipe, Streamlit, and Pygame. The application monitors eye movement and triggers an alarm if drowsiness is detected. The program is designed to run in real-time using a webcam feed.\n\n## Features\n- **Eye Aspect Ratio (EAR):** Tracks eye landmarks to compute the EAR and determine drowsiness.\n- **MediaPipe Integration:** Utilizes MediaPipe's FaceMesh solution for landmark detection.\n- **Real-Time Detection:** Processes webcam feed in real-time to monitor user behavior.\n- **Alarm System:** Plays an alarm sound when drowsiness is detected.\n- **Streamlit Interface:** Offers an interactive UI for deploying the application as a web app.\n\n---\n\n## Prerequisites\nEnsure the following dependencies are installed:\n\n- Python 3.7+\n- OpenCV\n- MediaPipe\n- Streamlit\n- Pygame\n\n### Installation\n1. Clone this repository:\n   ```bash\n   git clone \u003crepository-url\u003e\n   cd \u003crepository-name\u003e\n   ```\n\n2. Install the required libraries:\n   ```bash\n   pip install opencv-python mediapipe streamlit pygame\n   ```\n\n3. Ensure you have the alarm sound file `wake-up.mp3` in the root directory.\n\n---\n\n## How It Works\n\n1. **Landmark Detection:**\n   The program uses MediaPipe’s FaceMesh to detect face landmarks. Specific landmarks around the eyes are tracked to calculate the Eye Aspect Ratio (EAR).\n\n2. **Drowsiness Detection:**\n   - If the EAR drops below a threshold for a significant period, it indicates that the eyes are closed.\n   - This triggers the alarm to alert the user.\n\n3. **Alarm System:**\n   The alarm is implemented using Pygame’s mixer module. The alarm will keep playing until the user opens their eyes.\n\n---\n\n## Code Overview\n\n### Imports and Constants\n```python\nimport cv2\nimport time\nimport mediapipe as mp\nimport streamlit as st\nfrom pygame import mixer\nfrom mediapipe.python.solutions.drawing_utils import (\n    _normalized_to_pixel_coordinates as denormalize_coordinates,\n)\n\n# Constants for landmarks and colors\nleft_eye_idxs = [362, 385, 387, 263, 373, 380]\nright_eye_idxs = [33, 160, 158, 133, 153, 144]\nRED = (255, 0, 0)\nGREEN = (0, 255, 0)\nCOLOR = GREEN\nalarm_file_path = \"wake-up.mp3\"\n\n# Prepare alarm sound\nmixer.init()\nmixer.music.load(alarm_file_path)\nmixer.music.play(-1)\nmixer.music.pause()\n```\n\n### Caching MediaPipe Resources\n```python\n@st.cache_resource\ndef get_mediapipe_app(\n    max_num_faces=1,\n    refine_landmarks=True,\n    min_detection_confidence=0.5,\n    min_tracking_confidence=0.5\n):\n    face_mesh = mp.solutions.face_mesh.FaceMesh(\n        max_num_faces=max_num_faces,\n        refine_landmarks=refine_landmarks,\n        min_detection_confidence=min_detection_confidence,\n        min_tracking_confidence=min_tracking_confidence\n    )\n    return face_mesh\n```\n\n### Eye Aspect Ratio (EAR) Calculation\n```python\ndef distance(point_1, point_2):\n    dist = sum([(i - j) ** 2 for i, j in zip(point_1, point_2)]) ** 0.5\n    return dist\n\ndef get_ear(landmarks, refer_idxs, frame_width, frame_height):\n    try:\n        coords_points = []\n        for i in refer_idxs:\n            lm = landmarks[i]\n            coord = denormalize_coordinates(lm.x, lm.y, frame_width, frame_height)\n            coords_points.append(coord)\n\n        # Calculate distances\n        p2_p6 = distance(coords_points[1], coords_points[5])\n        p3_p5 = distance(coords_points[2], coords_points[4])\n        p1_p4 = distance(coords_points[0], coords_points[3])\n\n        # Compute EAR\n        ear = (p2_p6 + p3_p5) / (2.0 * p1_p4)\n    except:\n        ear = 0.0\n        coords_points = None\n\n    return ear, coords_points\n```\n\n### Real-Time Detection\n```python\ndef plot_text(image, text, origin, color, font=cv2.FONT_HERSHEY_SIMPLEX, fntScale=1.0, thickness=2):\n    image = cv2.putText(image, text, origin, font, fntScale, color, thickness)\n    return image\n\ntry:\n    cap = st.session_state[\"camera_cap\"]\n    if cap is not None:\n        cap.release()\n\n    st.session_state[\"camera_cap\"] = None\nexcept KeyError:\n    cap = None\n```\n\n---\n\n## Running the Application\n\n1. Run the Streamlit app:\n   ```bash\n   streamlit run app.py\n   ```\n\n2. Allow access to the webcam when prompted.\n\n3. The app will monitor your eyes in real-time and trigger an alarm if drowsiness is detected.\n\n\n---\n\n## Future Improvements\n- Add a customizable EAR threshold and alarm delay.\n- Incorporate additional facial features for better accuracy.\n- Enhance UI for user configuration and feedback.\n\n---\n\n## Contributing\nContributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshamiul5201%2Fdrowsy_driver_detection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshamiul5201%2Fdrowsy_driver_detection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshamiul5201%2Fdrowsy_driver_detection/lists"}