└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # ANTI-SLEEP-ALARM-FOR-DRIVERS-AND-BREAKING-SYSTEM- 2 | import cv2 3 | import dlib 4 | from scipy.spatial import distance 5 | 6 | # Function to compute the eye aspect ratio (EAR) 7 | def calculate_EAR(eye): 8 | A = distance.euclidean(eye[1], eye[5]) 9 | B = distance.euclidean(eye[2], eye[4]) 10 | C = distance.euclidean(eye[0], eye[3]) 11 | ear = (A + B) / (2.0 * C) 12 | return ear 13 | 14 | # Thresholds 15 | EYE_AR_THRESH = 0.25 # Threshold for EAR 16 | EYE_AR_CONSEC_FRAMES = 20 # Number of consecutive frames the eye must be below the threshold to sound alarm 17 | 18 | # Load the face detector and landmark predictor 19 | detector = dlib.get_frontal_face_detector() 20 | predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') 21 | 22 | # Indexes for the left and right eye landmarks 23 | (lStart, lEnd) = (42, 48) 24 | (rStart, rEnd) = (36, 42) 25 | 26 | cap = cv2.VideoCapture(0) 27 | frame_counter = 0 28 | 29 | while True: 30 | ret, frame = cap.read() 31 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 32 | 33 | rects = detector(gray, 0) 34 | 35 | for rect in rects: 36 | shape = predictor(gray, rect) 37 | shape = shape_to_np(shape) 38 | 39 | leftEye = shape[lStart:lEnd] 40 | rightEye = shape[rStart:rEnd] 41 | 42 | leftEAR = calculate_EAR(leftEye) 43 | rightEAR = calculate_EAR(rightEye) 44 | 45 | avg_EAR = (leftEAR + rightEAR) / 2.0 46 | 47 | if avg_EAR < EYE_AR_THRESH: 48 | frame_counter += 1 49 | if frame_counter >= EYE_AR_CONSEC_FRAMES: 50 | cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 51 | # Trigger alarm here (buzzer or speaker) 52 | else: 53 | frame_counter = 0 54 | 55 | cv2.imshow("Frame", frame) 56 | 57 | if cv2.waitKey(1) & 0xFF == ord('q'): 58 | break 59 | 60 | cap.release() 61 | cv2.destroyAllWindows() 62 | #define trigPin 9 63 | #define echoPin 10 64 | #define motorPin 3 // Motor connected to pin 3 65 | 66 | void setup() { 67 | pinMode(trigPin, OUTPUT); 68 | pinMode(echoPin, INPUT); 69 | pinMode(motorPin, OUTPUT); 70 | Serial.begin(9600); 71 | } 72 | 73 | void loop() { 74 | long duration, distance; 75 | 76 | // Send the ultrasonic pulse 77 | digitalWrite(trigPin, LOW); 78 | delayMicroseconds(2); 79 | digitalWrite(trigPin, HIGH); 80 | delayMicroseconds(10); 81 | digitalWrite(trigPin, LOW); 82 | 83 | // Measure the pulse duration 84 | duration = pulseIn(echoPin, HIGH); 85 | 86 | // Calculate the distance 87 | distance = (duration / 2) / 29.1; 88 | 89 | // Display the distance 90 | Serial.print("Distance: "); 91 | Serial.println(distance); 92 | 93 | // Brake when an obstacle is within a certain distance 94 | if (distance <= 20) { 95 | analogWrite(motorPin, 0); // Stop the motor (apply brake) 96 | Serial.println("Braking..."); 97 | } else { 98 | analogWrite(motorPin, 255); // Normal operation (motor running) 99 | } 100 | 101 | delay(100); 102 | } 103 | --------------------------------------------------------------------------------