-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain0.1.py
More file actions
311 lines (230 loc) · 9.25 KB
/
main0.1.py
File metadata and controls
311 lines (230 loc) · 9.25 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#importing all the necessary packeges
import math
import cv2
import mediapipe as mp
import numpy as np
import os
import time
import sys
from twilio.rest import Client
from gps import *
from threading import Thread
import threading
from gpiozero import Buzzer, Button
from rpi_lcd import LCD
from mpu6050 import mpu6050
#sends an SMS form twilio api
def sendMessage(alertMessage):
try:
account_sid = ''
auth_token = ''
client = Client(account_sid, auth_token)
message = client.messages.create(messaging_service_sid='' , body=alertMessage , to=emergencyContactNumber)
threading.Timer(7.0, lcd.clear).start()
#print(message.sid)
except:
print("There is no internet connection to send SMS")
#work in a seprat thread to detct impact
def detectImpact():
while True:
gx, gy, gz = sensor.get_accel_data()
time.sleep(0.5)
if (gx > 18) or (gy > 18) or (gz > 18):
print("Impact Detected Sending Emergancy Message")
alertMessage = " Hello Mr." + emergencyContactName + "\n" + "Car Impact Alert From: " + driverName + "\n\nLocation: " + "https://maps.google.com/?q=" + location
sendMessage(alertMessage)
#work in seprat thread to print text on the lcd screen
def putTextOnLCD(mesg, loop):
if not loop:
lcd.text(mesg , 2)
lcdLoc = "Unknown,Unknown"
while loop:
if "Unknown,Unknown" == location:
lcd.text(" Thresh: " + str(flag) , 1)
elif location != lcdLoc:
lcd.text("v Thresh: " + str(flag) , 1)
lcdLoc = location
else:
lcd.text("^ Thresh: " + str(flag) , 1)
lcdLoc = location
time.sleep(0.35)
#drow an alert text on the frame
def putDrowsyTextOnFrame():
cv2.putText(frame, "************************ALERT!************************", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "************************ALERT!************************", (10, 450), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
#works in seprat thread to get the gps coordnates every 4 sec
def getPositionData(gps):
while True:
nx = gpsd.next()
# For a list of all supported classes and fields refer to:
# https://gpsd.gitlab.io/gpsd/gpsd_json.html
global location
loc = "Unknown,Unknown"
if nx['class'] == 'TPV':
latitude = getattr(nx,'lat', "Unknown")
longitude = getattr(nx,'lon', "Unknown")
loc = str(latitude) + "," + str(longitude)
if loc != "Unknown,Unknown":
location = str(latitude) + "," + str(longitude)
print ("Your position: lon = " + str(longitude) + ", lat = " + str(latitude))
time.sleep(3)
# landmark detection function
def landmarksDetection(img, results, draw=True):
img_height, img_width = img.shape[:2]
# list[(x,y), (x,y)....]
mesh_coord = [(int(point.x * img_width), int(point.y * img_height)) for point in
results.multi_face_landmarks[0].landmark]
if draw:
[cv2.circle(img, p, 2, (0, 255, 0), -1) for p in mesh_coord]
# returning the list of tuples for each landmarks
return mesh_coord
# Euclaidean distance
def euclaideanDistance(point, point1):
x, y = point
x1, y1 = point1
distance = math.sqrt((x1 - x) ** 2 + (y1 - y) ** 2)
return distance
# Blinking Ratio
def blinkRatio(img, landmarks, right_indices, left_indices):
# Right eyes
# horizontal line
rh_right = landmarks[right_indices[0]]
rh_left = landmarks[right_indices[8]]
# vertical line
rv_top = landmarks[right_indices[12]]
rv_bottom = landmarks[right_indices[4]]
# draw lines on right eyes
# cv.line(img, rh_right, rh_left, utils.GREEN, 2)
# cv.line(img, rv_top, rv_bottom, utils.WHITE, 2)
# LEFT_EYE
# horizontal line
lh_right = landmarks[left_indices[0]]
lh_left = landmarks[left_indices[8]]
# vertical line
lv_top = landmarks[left_indices[12]]
lv_bottom = landmarks[left_indices[4]]
rhDistance = euclaideanDistance(rh_right, rh_left)
rvDistance = euclaideanDistance(rv_top, rv_bottom)
lvDistance = euclaideanDistance(lv_top, lv_bottom)
lhDistance = euclaideanDistance(lh_right, lh_left)
reRatio = rhDistance / rvDistance
leRatio = lhDistance / lvDistance
ratio = (reRatio + leRatio) / 2
return ratio
#making an instance of the lcd, buzzer, camera and acclometer
lcd = LCD()
button = Button(23)
buzzer = Buzzer(17)
cap = cv2.VideoCapture(0)
sensor = mpu6050(0x68)
#making an inctane of gps packege
os.system("sudo gpsd /dev/serial0 -F /var/run/gpsd.sock")
time.sleep(3)
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
#user details
driverName = ""
emergencyContactName = ""
emergencyContactNumber = ""
#face mesh intalisation
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5)
#important varables
location = "Unknown,Unknown"
eye_thresh = 4
head_thresh = -10
flag = 0
ratio = 0
x = 0
# left eyes indices
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
# right eyes indices
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]
#starting gps thread for getPositionData()
gpsThread = Thread(target = getPositionData, args = (gpsd,))
gpsThread.start()
#starting gps thread for putTextOnLCD()
lcdThread = Thread(target = putTextOnLCD, args = ("",True))
lcdThread.start()
#starting gps thread for detectImpact()
gForceThread = Thread(target = detectImpact, )
gForceThread.start()
while cap.isOpened():
ret, frame = cap.read()
# Flip the image horizontally for a later selfie-view display
# Also convert the color space from BGR to RGB
frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
# To improve performance
frame.flags.writeable = False
# Get the result
results = face_mesh.process(frame)
#To improve performance
frame.flags.writeable = True
# Convert the color space from RGB to BGR
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
img_h, img_w, img_c = frame.shape
face_3d = []
face_2d = []
if results.multi_face_landmarks:
try:
mesh_coords = landmarksDetection(frame, results, False)
ratio = blinkRatio(frame, mesh_coords, RIGHT_EYE, LEFT_EYE)
except:
continue
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
if idx == 1:
nose_2d = (lm.x * img_w, lm.y * img_h)
nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 8000)
x, y = int(lm.x * img_w), int(lm.y * img_h)
# Get the 2D Coordinates
face_2d.append([x, y])
# Get the 3D Coordinates
face_3d.append([x, y, lm.z])
# Convert it to the NumPy array
face_2d = np.array(face_2d, dtype=np.float64)
# Convert it to the NumPy array
face_3d = np.array(face_3d, dtype=np.float64)
# The camera matrix
focal_length = 1 * img_w
cam_matrix = np.array([[focal_length, 0, img_h / 2],
[0, focal_length, img_w / 2],
[0, 0, 1]])
# The Distance Matrix
dist_matrix = np.zeros((4, 1), dtype=np.float64)
# Solve PnP
ret, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
# Get rotational matrix
rmat, jac = cv2.Rodrigues(rot_vec)
# Get angles
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
# Get the y rotation degree
x = angles[0] * 360
y = angles[1] * 360
#print(x,y)
else:
x = 0
y = 0
ratio = 0
if x <= head_thresh or ratio >= eye_thresh:
flag += 1
print("Threash: " + str(flag))
if flag > 25:
putDrowsyTextOnFrame()
buzzer.on()
if flag == 75:
print("Threshold Is Exceeded Sending Emergancy Message")
alertMessage = " Hello Mr." + emergencyContactName + "\n" + "Drowsy Driving Alert From: " + driverName + "\n\nLocation: " + "https://maps.google.com/?q=" + location
putTextOnLCD("Sending SMS...", False)
sendMessage(alertMessage)
else:
flag = 0
buzzer.off()
#cv2.imshow('Head Pose Estimation', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
if button.is_pressed:
lcd.clear()
#os.system("python3 /home/pi/Desktop/run.py")
sys.exit()
cap.release()