-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAFE_DRIVE.py
132 lines (110 loc) · 4.84 KB
/
SAFE_DRIVE.py
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
#### SAFE DRIVE DRIVER SAFETY SOFTWARE #####
# import the necessary packages
from scipy.spatial import distance as dist
from imutils.video import FileVideoStream
from imutils.video import VideoStream
from imutils import face_utils
import numpy as np
import argparse
import imutils
import time
import dlib
import cv2
import pyttsx3
from keras.preprocessing.image import img_to_array
import imutils
import cv2
from keras.models import load_model
import numpy as np
shape_path = r"C:\Users\ACER\Desktop\02_03_2020\Drowsiness-Detection-Using-Facial-Images-master\Drowsiness-Detection-Using-Facial-Images-master\shape_predictor_68_face_landmarks.dat"
vid_path = r"C:\Users\ACER\Desktop\video.mp4"
face_cascade = cv2.CascadeClassifier(r'C:\Users\ACER\Desktop\distraction_detection-master\distraction_detection-master\haarcascade_files\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(r'C:\Users\ACER\Desktop\distraction_detection-master\distraction_detection-master\haarcascade_files\haarcascade_eye.xml')
distract_model = load_model(r'C:\Users\ACER\Desktop\distraction_detection-master\distraction_detection-master\src\cnn\distraction_model.hdf5', compile=False)
frame_w = 1200
border_w = 2
min_size_w = 240
min_size_h = 240
min_size_w_eye = 60
min_size_h_eye = 60
scale_factor = 1.1
min_neighbours = 5
# Video writer
# IMPORTANT:
# - frame1 width and height must match output frame1 shape
# - avi works on ubuntu. mp4 doesn't :/
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
video_out = cv2.VideoWriter('video_out.avi', fourcc, 10.0,(1200, 900))
# init camera window
cv2.namedWindow('Watcha Looking At?')
camera = cv2.VideoCapture(0)
# Check if camera opened successfully
if (camera.isOpened() == False):
print("Unable to read camera feed")
while True:
# get frame
ret, frame = camera.read()
# if we have a frame, do stuff
if ret:
# make frame bigger
frame = imutils.resize(frame, width=frame_w)
# use grayscale for faster processing
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect face(s)
faces = face_cascade.detectMultiScale(gray,scaleFactor=scale_factor,minNeighbors=min_neighbours,minSize=(min_size_w,min_size_h),flags=cv2.CASCADE_SCALE_IMAGE)
# for each face, detect eyes and distraction
if len(faces) > 0:
# loop through faces
for (x,y,w,h) in faces:
# draw face rectangle
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
# get gray face for eye detection
roi_gray = gray[y:y+h, x:x+w]
# get colour face for distraction detection (model has 3 input channels - probably redundant)
roi_color = frame[y:y+h, x:x+w]
# detect gray eyes
eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=scale_factor,minNeighbors=min_neighbours,minSize=(min_size_w_eye,min_size_w_eye))
# init probability list for each eye prediction
probs = list()
# loop through detected eyes
for (ex,ey,ew,eh) in eyes:
# draw eye rectangles
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),border_w)
# get colour eye for distraction detection
roi = roi_color[ey+border_w:ey+eh-border_w, ex+border_w:ex+ew-border_w]
# match CNN input shape
roi = cv2.resize(roi, (64, 64))
# normalize (as done in model training)
roi = roi.astype("float") / 255.0
# change to array
roi = img_to_array(roi)
# correct shape
roi = np.expand_dims(roi, axis=0)
# distraction classification/detection
prediction = distract_model.predict(roi)
# save eye result
probs.append(prediction[0])
# get average score for all eyes
probs_mean = np.mean(probs)
# get label
if probs_mean <= 0.3:
label = 'distracted'
else:
label = 'focused'
# insert label on frame
cv2.putText(frame,label,(x,y-5), cv2.FONT_HERSHEY_SIMPLEX,
1, (0,0,255), 3, cv2.LINE_AA)
# Write the frame to video
video_out.write(frame)
# display frame in window
cv2.imshow('Distraction Detection', frame)
# quit with q
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# no frame, don't do stuff
else:
break
# close
camera.release()
video_out.release()
cv2.destroyAllWindows()