-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
279 lines (217 loc) · 9.81 KB
/
main.py
File metadata and controls
279 lines (217 loc) · 9.81 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import cv2
import urllib.request
import numpy as np
import dlib
from math import hypot
import Jetson.GPIO as GPIO
import time
# Servo GPIO pin setup
servo_pin1 = 12 # GPIO pin connected to the servo (BCM numbering)
servo_pin2 = 33 # GPIO pin connected to the servo (BCM numbering)
servo_pin3 = 35 # GPIO pin connected to the servo (BCM numbering)
servo_pin4 = 37 # GPIO pin connected to the servo (BCM numbering)
min_duty_cycle = 2.5 # Min duty cycle for 0 degrees
max_duty_cycle = 12.5 # Max duty cycle for 180 degrees
servo_angle_1 = 90
servo_angle_2 = 90
servo_angle_3 = 90
servo_angle_4 = 90
#light pins
light_pin1 = 11 # left
light_pin2 = 19 # right
light_pin3 = 21 # up
light_pin4 = 23 # down
# Initialize GPIO for servo control
GPIO.setmode(GPIO.BOARD)
GPIO.setup(servo_pin1, GPIO.OUT)
GPIO.setup(servo_pin2, GPIO.OUT)
GPIO.setup(servo_pin3, GPIO.OUT)
GPIO.setup(servo_pin4, GPIO.OUT)
GPIO.setup(light_pin1, GPIO.OUT)
GPIO.setup(light_pin2, GPIO.OUT)
GPIO.setup(light_pin3, GPIO.OUT)
GPIO.setup(light_pin4, GPIO.OUT)
# Initialize GPIO for LIGHT control
GPIO.setup(light_pin1, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(light_pin2, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(light_pin3, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(light_pin4, GPIO.OUT, initial=GPIO.HIGH)
def set_servo_angle1(angle):
duty_cycle = min_duty_cycle + (max_duty_cycle - min_duty_cycle) * (angle / 180.0)
GPIO.output(servo_pin1, GPIO.HIGH)
time.sleep(duty_cycle / 1000.0) # Active high for duty cycle duration
GPIO.output(servo_pin1, GPIO.LOW)
time.sleep((20 - duty_cycle) / 1000.0) # Complete 20 ms period
def set_servo_angle2(angle):
"""Set the servo to a specific angle using software PWM."""
duty_cycle = min_duty_cycle + (max_duty_cycle - min_duty_cycle) * (angle / 180.0)
GPIO.output(servo_pin2, GPIO.HIGH)
time.sleep(duty_cycle / 1000.0) # Active high for duty cycle duration
GPIO.output(servo_pin2, GPIO.LOW)
time.sleep((20 - duty_cycle) / 1000.0) # Complete 20 ms period
def set_servo_angle3(angle):
"""Set the servo to a specific angle using software PWM."""
duty_cycle = min_duty_cycle + (max_duty_cycle - min_duty_cycle) * (angle / 180.0)
GPIO.output(servo_pin3, GPIO.HIGH)
time.sleep(duty_cycle / 1000.0) # Active high for duty cycle duration
GPIO.output(servo_pin3, GPIO.LOW)
time.sleep((20 - duty_cycle) / 1000.0) # Complete 20 ms period
def set_servo_angle4(angle):
"""Set the servo to a specific angle using software PWM."""
duty_cycle = min_duty_cycle + (max_duty_cycle - min_duty_cycle) * (angle / 180.0)
GPIO.output(servo_pin4, GPIO.HIGH)
time.sleep(duty_cycle / 1000.0) # Active high for duty cycle duration
GPIO.output(servo_pin4, GPIO.LOW)
time.sleep((20 - duty_cycle) / 1000.0) # Complete 20 ms period
# Initialize variables
keyboard = np.zeros((600, 1000, 3), np.uint8)
url = 'http://192.168.72.171/cam-lo.jpg'
cv2.namedWindow("live Cam Testing", cv2.WINDOW_AUTOSIZE)
# Create VideoCapture object (though not used directly here)
cap = cv2.VideoCapture(url)
# Initialize facial feature detector
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def midpoint(p1, p2):
return int((p1.x + p2.x) / 2), int((p1.y + p2.y) / 2)
def get_gaze_ratio(eye_points, facial_landmarks):
left_eye_region = np.array([(facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y),
(facial_landmarks.part(eye_points[1]).x, facial_landmarks.part(eye_points[1]).y),
(facial_landmarks.part(eye_points[2]).x, facial_landmarks.part(eye_points[2]).y),
(facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y),
(facial_landmarks.part(eye_points[4]).x, facial_landmarks.part(eye_points[4]).y),
(facial_landmarks.part(eye_points[5]).x, facial_landmarks.part(eye_points[5]).y)],
np.int32)
height, width, _ = img.shape
mask = np.zeros((height, width), np.uint8)
cv2.polylines(mask, [left_eye_region], True, 255, 2)
cv2.fillPoly(mask, [left_eye_region], 255)
eye = cv2.bitwise_and(gray, gray, mask=mask)
min_x = np.min(left_eye_region[:, 0])
max_x = np.max(left_eye_region[:, 0])
min_y = np.min(left_eye_region[:, 1])
max_y = np.max(left_eye_region[:, 1])
gray_eye = eye[min_y: max_y, min_x: max_x]
_, threshold_eye = cv2.threshold(gray_eye, 70, 255, cv2.THRESH_BINARY)
height, width = threshold_eye.shape
left_side_threshold = threshold_eye[0: height, 0: int(width / 2)]
left_side_white = cv2.countNonZero(left_side_threshold)
right_side_threshold = threshold_eye[0: height, int(width / 2): width]
right_side_white = cv2.countNonZero(right_side_threshold)
if left_side_white == 0:
gaze_ratio = 1
elif right_side_white == 0:
gaze_ratio = 5
else:
gaze_ratio = left_side_white / right_side_white
return gaze_ratio
# Function to get eye contours
def eyes_contour_points(facial_landmarks):
left_eye = []
right_eye = []
for n in range(36, 42): # Left eye landmark points
x = facial_landmarks.part(n).x
y = facial_landmarks.part(n).y
left_eye.append([x, y])
for n in range(42, 48): # Right eye landmark points
x = facial_landmarks.part(n).x
y = facial_landmarks.part(n).y
right_eye.append([x, y])
left_eye = np.array(left_eye, np.int32)
right_eye = np.array(right_eye, np.int32)
return left_eye, right_eye
# Function to calculate blinking ratio (EAR)
def get_blinking_ratio(eye_points, facial_landmarks):
eye = []
for n in eye_points:
x = facial_landmarks.part(n).x
y = facial_landmarks.part(n).y
eye.append([x, y])
eye = np.array(eye, np.int32)
A = hypot(eye[1][0] - eye[5][0], eye[1][1] - eye[5][1])
B = hypot(eye[2][0] - eye[4][0], eye[2][1] - eye[4][1])
C = hypot(eye[0][0] - eye[3][0], eye[0][1] - eye[3][1])
ear = (A + B) / (2.0 * C)
return ear
# Main loop
try:
while True:
try:
img_resp = urllib.request.urlopen(url)
imgnp = np.array(bytearray(img_resp.read()), dtype=np.uint8)
img = cv2.imdecode(imgnp, cv2.IMREAD_COLOR)
if img is None:
print("Failed to decode image.")
continue
rows, cols, _ = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img[rows - 50: rows, 0: cols] = (255, 255, 255)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
left_eye, right_eye = eyes_contour_points(landmarks)
left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
blinking_ratio = (left_eye_ratio + right_eye_ratio) / 2
# Draw contours around the eyes
cv2.polylines(img, [left_eye], True, (0, 0, 255), 2)
cv2.polylines(img, [right_eye], True, (0, 0, 255), 2)
gaze_ratio_left_eye = get_gaze_ratio([36, 37, 38, 39, 40, 41], landmarks)
gaze_ratio_right_eye = get_gaze_ratio([42, 43, 44, 45, 46, 47], landmarks)
gaze_ratio = (gaze_ratio_right_eye + gaze_ratio_left_eye) / 2
print("Gaze ratio:", gaze_ratio)
print("Blink ratioqa:", blinking_ratio)
if (blinking_ratio <= 0.20):
print("Looking Down")
GPIO.output(light_pin4, GPIO.LOW)
GPIO.output(light_pin1, GPIO.HIGH)
GPIO.output(light_pin2, GPIO.HIGH)
GPIO.output(light_pin3, GPIO.HIGH)
GPIO.output(servo_pin4, GPIO.LOW)
GPIO.output(servo_pin1, GPIO.HIGH)
GPIO.output(servo_pin2, GPIO.HIGH)
GPIO.output(servo_pin3, GPIO.HIGH)
time.sleep(0.5)
elif(blinking_ratio > 0.30):
print("Looking Up")
GPIO.output(light_pin3, GPIO.HIGH)
GPIO.output(light_pin1, GPIO.LOW)
GPIO.output(light_pin2, GPIO.HIGH)
GPIO.output(light_pin4, GPIO.HIGH)
GPIO.output(servo_pin3, GPIO.LOW)
GPIO.output(servo_pin1, GPIO.HIGH)
GPIO.output(servo_pin2, GPIO.HIGH)
GPIO.output(servo_pin4, GPIO.HIGH)
time.sleep(0.5)
elif gaze_ratio <= 1.3:
print("Looking right.")
GPIO.output(light_pin2, GPIO.LOW)
GPIO.output(light_pin1, GPIO.HIGH)
GPIO.output(light_pin3, GPIO.HIGH)
GPIO.output(light_pin4, GPIO.HIGH)
GPIO.output(servo_pin2, GPIO.LOW)
GPIO.output(servo_pin1, GPIO.HIGH)
GPIO.output(servo_pin3, GPIO.HIGH)
GPIO.output(servo_pin4, GPIO.HIGH)
time.sleep(0.5)
print("Looking left.")
GPIO.output(light_pin1, GPIO.HIGH)
GPIO.output(light_pin2, GPIO.HIGH)
GPIO.output(light_pin3, GPIO.LOW)
GPIO.output(light_pin4, GPIO.HIGH)
GPIO.output(servo_pin1, GPIO.LOW)
GPIO.output(servo_pin2, GPIO.HIGH)
GPIO.output(servo_pin3, GPIO.HIGH)
GPIO.output(servo_pin4, GPIO.HIGH)
time.sleep(0.5)
cv2.imshow('live Cam Testing', img)
key = cv2.waitKey(5)
if key == ord('q'):
break
except Exception as e:
print(f"Error processing frame: {e}")
continue
finally:
GPIO.cleanup()
cv2.destroyAllWindows()
print("GPIO cleaned up.")