├── README.md ├── drowsiness_detect.py └── key.py /README.md: -------------------------------------------------------------------------------- 1 | # 🚗 Drowsiness Detector Using Python with SMS Alert 2 | 3 | A real-time drowsiness detection system designed to enhance driver safety. This project uses Python, OpenCV, and Dlib to detect drowsiness, providing both an audio alert and an SMS notification for prompt action. 4 | 5 | --- 6 | 7 | ## 📝 Features 8 | 9 | - **Real-Time Detection**: Monitors the driver’s face and eye movements via a webcam. 10 | - **Eye Aspect Ratio (EAR) Calculation**: Detects drowsiness by analyzing eye closure patterns. 11 | - **Audio Alerts**: Plays a warning sound when drowsiness is detected. 12 | - **SMS Notifications**: Sends an SMS alert to a predefined contact using the Twilio API. 13 | - **Facial Landmark Detection**: Utilizes Haar cascades and Dlib’s 68-point facial landmark predictor for precise detection. 14 | 15 | --- 16 | 17 | ## 📋 Requirements 18 | 19 | ### Ensure you have the following installed: 20 | 21 | - Python 3.7 or higher 22 | - Required Python Libraries: 23 | ```bash 24 | pip install numpy opencv-python pygame imutils scipy dlib twilio 25 | ### Dlib Shape Predictor Model: 26 | - Download shape_predictor_68_face_landmarks.dat from Dlib's Model Zoo. 27 | - Place the file in your project directory. 28 | 29 | --- 30 | 31 | ## ⚙️ Setup and Installation 32 | ### Clone the repository: 33 | git clone https://github.com/your-username/Drowsiness-detector-using-python-with-sms-alert.git 34 | cd Drowsiness-detector-using-python-with-sms-alert 35 | ### Install dependencies: 36 | pip install -r requirements.txt 37 | ### Configure Twilio credentials: 38 | Create a file named key.py in the project directory. 39 | ### Add the following: 40 | account_sid = "your_account_sid" 41 | auth_token = "your_auth_token" 42 | twilio_number = "your_twilio_phone_number" 43 | target_number = "target_phone_number" 44 | Ensure the audio alert file (alert.wav) is in the project directory. 45 | 46 | --- 47 | 48 | ## 🚀 Usage 49 | ### Run the script: 50 | python drowsiness_detection.py 51 | The webcam will activate, and the system will start monitoring for drowsiness. 52 | 53 | If drowsiness is detected: 54 | 55 | An alert sound will play. 56 | An SMS will be sent to the predefined number. 57 | Press q to exit the application. 58 | 59 | --- 60 | 61 | ## 🛠️ How It Works 62 | Eye Aspect Ratio (EAR): 63 | 64 | EAR is calculated using the distances between specific eye landmarks. 65 | If EAR falls below a set threshold for consecutive frames, the system detects drowsiness. 66 | ### Audio Alert: 67 | The pygame library plays a warning sound to alert the driver. 68 | ### SMS Alert: 69 | The Twilio API sends an SMS to notify others about the driver’s condition. 70 | 71 | --- 72 | 73 | ## 📂 File Structure 74 | 75 | Drowsiness-detector-using-python-with-sms-alert/ 76 | │ 77 | ├── alert.wav # Audio file for alert 78 | ├── drowsiness_detection.py # Main script 79 | ├── shape_predictor_68_face_landmarks.dat # Dlib model for facial landmarks 80 | ├── key.py # Twilio credentials 81 | ├── requirements.txt # Python dependencies 82 | └── README.md # Project documentation 83 | 84 | --- 85 | 86 | ## 🧩 Future Enhancements 87 | Add yawning detection for better accuracy. 88 | Introduce a logging system to record drowsiness events with timestamps. 89 | Optimize for mobile and edge devices. 90 | Enhance the UI with more intuitive feedback mechanisms. 91 | 92 | --- 93 | 94 | ## 🛡️ License 95 | This project is licensed under the MIT License. See the LICENSE file for details. 96 | 97 | --- 98 | 99 | ## 🤝 Contributing 100 | Contributions are welcome! Feel free to open issues or submit pull requests to improve this project. 101 | 102 | --- 103 | 104 | ## 📬 Contact 105 | ### For inquiries or feedback, reach out to me: 106 | Email: vivi420vishnu.com 107 | GitHub: VichuA2 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /drowsiness_detect.py: -------------------------------------------------------------------------------- 1 | from scipy.spatial import distance 2 | from imutils import face_utils 3 | import numpy as np 4 | import pygame #For playing sound 5 | import time 6 | import dlib 7 | import cv2 8 | import twilio 9 | from twilio.rest import Client 10 | import key 11 | Client = Client(key.account_sid,key.auth_token) 12 | pygame.mixer.init() 13 | pygame.mixer.music.load('alert.wav') 14 | EYE_ASPECT_RATIO_THRESHOLD = 0.3 15 | EYE_ASPECT_RATIO_CONSEC_FRAMES = 50 16 | COUNTER = 0 17 | face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml") 18 | def eye_aspect_ratio(eye): 19 | A = distance.euclidean(eye[1], eye[5]) 20 | B = distance.euclidean(eye[2], eye[4]) 21 | C = distance.euclidean(eye[0], eye[3]) 22 | ear = (A+B) / (2*C) 23 | return ear 24 | detector = dlib.get_frontal_face_detector() 25 | predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') 26 | (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS['left_eye'] 27 | (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye'] 28 | video_capture = cv2.VideoCapture(0) 29 | time.sleep(2) 30 | while(True): 31 | ret, frame = video_capture.read() 32 | frame = cv2.flip(frame,1) 33 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 34 | faces = detector(gray, 0) 35 | face_rectangle = face_cascade.detectMultiScale(gray, 1.3, 5) 36 | for (x,y,w,h) in face_rectangle: 37 | cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) 38 | for face in faces: 39 | shape = predictor(gray, face) 40 | shape = face_utils.shape_to_np(shape) 41 | leftEye = shape[lStart:lEnd] 42 | rightEye = shape[rStart:rEnd] 43 | leftEyeAspectRatio = eye_aspect_ratio(leftEye) 44 | rightEyeAspectRatio = eye_aspect_ratio(rightEye) 45 | eyeAspectRatio = (leftEyeAspectRatio + rightEyeAspectRatio) / 2 46 | leftEyeHull = cv2.convexHull(leftEye) 47 | rightEyeHull = cv2.convexHull(rightEye) 48 | cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) 49 | cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) 50 | if(eyeAspectRatio < EYE_ASPECT_RATIO_THRESHOLD): 51 | COUNTER += 1 52 | if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES: 53 | pygame.mixer.music.play(-1) 54 | cv2.putText(frame, "You are Drowsy", (150,200), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,0,255), 2) 55 | message = Client.messages.create(body="Your Driver feels sleepy", from_=key.twilio_number,to=key.target_number) 56 | else: 57 | pygame.mixer.music.stop() 58 | COUNTER = 0 59 | cv2.imshow('Video', frame) 60 | if(cv2.waitKey(1) & 0xFF == ord('q')): 61 | break 62 | video_capture.release() 63 | cv2.destroyAllWindows() 64 | -------------------------------------------------------------------------------- /key.py: -------------------------------------------------------------------------------- 1 | account_sid = 'add yours' 2 | auth_token = 'add yours 3 | twilio_number = 'add yours' 4 | target_number = 'add yours' --------------------------------------------------------------------------------