Created
June 29, 2021 06:51
-
-
Save Yuvnish017/797163dbbc418433c2869694e8e346dc to your computer and use it in GitHub Desktop.
Driver Drowsiness Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def full_face_detection_pipeline(input_image_path): | |
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') | |
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') | |
detector = dlib.get_frontal_face_detector() | |
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') | |
fa = FaceAligner(predictor, desiredFaceWidth=256) | |
test_image = cv2.imread(input_image_path) | |
test_image = imutils.resize(test_image, width=800) | |
test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY) | |
rects = detector(test_image_gray, 2) | |
for rect in rects: | |
(x, y, w, h) = rect_to_bb(rect) | |
faceOrig = imutils.resize(test_image[y:y+h, x:x+w], width=256) | |
faceAligned = fa.align(test_image, test_image_gray, rect) | |
faceAligned_gray = cv2.cvtColor(faceAligned, cv2.COLOR_BGR2GRAY) | |
plt.imshow(faceAligned_gray) | |
plt.axis('off') | |
plt.title('Aligned Face') | |
plt.show() | |
eyes = eye_cascade.detectMultiScale(faceAligned_gray, 1.1, 4) | |
predictions = [] | |
for (ex, ey, ew, eh) in eyes: | |
eye = faceAligned[ey:ey+eh, ex:ex+ew] | |
# cv2.rectangle(test_image, (x+ex, y+ey), (x+ex+ew, y+ey+eh), (0, 0, 255), 8) | |
eye = cv2.resize(eye, (32, 32)) | |
eye = np.array(eye) | |
eye = np.expand_dims(eye, axis=0) | |
ypred = model.predict(eye) | |
ypred = np.argmax(ypred[0], axis=0) | |
predictions.append(ypred) | |
if all(i==0 for i in predictions): | |
cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 0, 255), 8) | |
cv2.putText(test_image, 'Sleeping', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3) | |
else: | |
cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 255, 0), 8) | |
cv2.putText(test_image, 'Not Sleeping', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 3) | |
output_path = 'test_image_prediction.jpg' | |
cv2.imwrite(output_path, test_image) | |
return output_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment