-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain code(DDDS).py
More file actions
135 lines (109 loc) · 4.15 KB
/
Main code(DDDS).py
File metadata and controls
135 lines (109 loc) · 4.15 KB
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
133
134
135
import cv2
import mediapipe as mp
import serial
import time
import numpy as np
# --- CONFIGURATION AND SETUP ---
# This is the final configuration based on your requests.
ARDUINO_PORT = 'COM4'
BAUD_RATE = 9600
EAR_THRESHOLD = 0.23
EYE_CLOSED_DURATION_SECONDS = 1.5
# --- SERIAL COMMUNICATION SETUP ---
print(f"Connecting to Arduino on {ARDUINO_PORT}...")
try:
arduino = serial.Serial(port=ARDUINO_PORT, baudrate=BAUD_RATE, timeout=1)
# This sleep is important for the connection to establish.
time.sleep(2)
# Send an initial 'Awake' signal to start the motor, as requested.
print("Connection successful. Sending initial 'Awake' signal...")
arduino.write(b'1')
print("Motor activated.")
except serial.SerialException as e:
print(f"Error: Could not connect to Arduino. {e}")
exit()
# --- MEDIPIPE FACE MESH SETUP ---
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
LEFT_EYE_POINTS = [33, 160, 158, 133, 153, 144]
RIGHT_EYE_POINTS = [362, 385, 387, 263, 373, 380]
def calculate_ear(eye_landmarks, frame_shape):
coords_points = np.array([(int(lm.x * frame_shape[1]), int(lm.y * frame_shape[0])) for lm in eye_landmarks])
p2_p6 = np.linalg.norm(coords_points[1] - coords_points[5])
p3_p5 = np.linalg.norm(coords_points[2] - coords_points[4])
p1_p4 = np.linalg.norm(coords_points[0] - coords_points[3])
ear = (p2_p6 + p3_p5) / (2.0 * p1_p4)
return ear
# --- MAIN VIDEO CAPTURE LOOP ---
cap = cv2.VideoCapture(0)
eye_closed_start_time = None
system_status = "Awake"
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb_frame)
display_status = "Awake"
if results.multi_face_landmarks:
face_landmarks = results.multi_face_landmarks[0].landmark
left_eye_lms = [face_landmarks[i] for i in LEFT_EYE_POINTS]
right_eye_lms = [face_landmarks[i] for i in RIGHT_EYE_POINTS]
left_ear = calculate_ear(left_eye_lms, frame.shape)
right_ear = calculate_ear(right_eye_lms, frame.shape)
avg_ear = (left_ear + right_ear) / 2.0
if avg_ear < EAR_THRESHOLD:
if eye_closed_start_time is None:
eye_closed_start_time = time.time()
elapsed_time = time.time() - eye_closed_start_time
if elapsed_time >= EYE_CLOSED_DURATION_SECONDS:
display_status = "Asleep"
if system_status != "Asleep":
system_status = "Asleep"
print(f"STATE CHANGE: {system_status}")
arduino.write(b'0')
else:
display_status = "Blinking"
else:
eye_closed_start_time = None
display_status = "Awake"
if system_status != "Awake":
system_status = "Awake"
print(f"STATE CHANGE: {system_status}")
arduino.write(b'1')
else:
display_status = "No Face Detected"
eye_closed_start_time = None
if system_status != "Asleep":
system_status = "Asleep"
print(f"STATE CHANGE: Asleep (No face detected)")
arduino.write(b'0')
if display_status == "Awake":
color = (0, 255, 0)
elif display_status == "Asleep":
color = (0, 0, 255)
elif display_status == "Blinking":
color = (0, 255, 255)
else: # No Face Detected
color = (0, 165, 255)
cv2.putText(frame, display_status, (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 3)
cv2.imshow("Eye State Detection", frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# --- CLEANUP ---
cap.release()
cv2.destroyAllWindows()
if arduino.is_open:
print("Exiting program. Sending SHUTDOWN signal...")
# Send the '2' command for a clean shutdown (motor and buzzer off).
arduino.write(b'2')
time.sleep(0.1)
arduino.close()
print("Arduino connection closed. Everything should be off.")