Spaces:
Configuration error
Configuration error
Ayush35
commited on
Commit
•
d7096ad
1
Parent(s):
bac8d66
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
import numpy as np
|
6 |
+
from pygame import mixer
|
7 |
+
|
8 |
+
st.title('Driver Drowziness Detection')
|
9 |
+
st.sidebar.subheader('About')
|
10 |
+
st.sidebar.write('A computer vision system made with the help of opencv that can automatically detect driver drowsiness in a real-time video stream and then play an alarm if the driver appears to be drowsy.')
|
11 |
+
|
12 |
+
dir_path= (r'Models')
|
13 |
+
model = load_model(dir_path)
|
14 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
15 |
+
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
|
16 |
+
st.header("Webcam Live Feed")
|
17 |
+
run = st.checkbox('Click to Run/Off the cam',value=True)
|
18 |
+
FRAME_WINDOW = st.image([])
|
19 |
+
cap = cv2.VideoCapture(0)
|
20 |
+
mixer.init()
|
21 |
+
sound= mixer.Sound(r'alarm.wav')
|
22 |
+
Score = 0
|
23 |
+
eye_cond = 1
|
24 |
+
|
25 |
+
st.subheader('Rules')
|
26 |
+
st.write('The more focused you are on your ride, the lower your drowziness score')
|
27 |
+
st.write('Alarm clock sounds when score reaches 25')
|
28 |
+
st.markdown('To Stop the Alarm Just **Focus on Your Drive**')
|
29 |
+
|
30 |
+
|
31 |
+
while run:
|
32 |
+
col1,col2 = st.sidebar.columns(2)
|
33 |
+
with col1:
|
34 |
+
st.subheader('Score = ' + str(Score))
|
35 |
+
with col2:
|
36 |
+
pass
|
37 |
+
_, frame = cap.read()
|
38 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
39 |
+
height,width = frame.shape[0:2]
|
40 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
41 |
+
faces= face_cascade.detectMultiScale(gray, scaleFactor= 1.2, minNeighbors=3)
|
42 |
+
eyes= eye_cascade.detectMultiScale(gray, scaleFactor= 1.1, minNeighbors=1)
|
43 |
+
frame2 = cv2.rectangle(frame, (0,height-50),(200,height),(0,0,0),thickness=cv2.FILLED)
|
44 |
+
sc = st.empty()
|
45 |
+
def on_update():
|
46 |
+
data = getNewData()
|
47 |
+
sc.text('Score :' + str(data))
|
48 |
+
|
49 |
+
for (x,y,w,h) in faces:
|
50 |
+
cv2.rectangle(frame,pt1=(x,y),pt2=(x+w,y+h), color= (255,0,0), thickness=3 )
|
51 |
+
for (ex,ey,ew,eh) in eyes:
|
52 |
+
# cv2.rectangle(frame,pt1=(ex,ey),pt2=(ex+ew,ey+eh), color= (255,0,0), thickness=5)
|
53 |
+
# preprocessing steps
|
54 |
+
eye= frame[ey:ey+eh,ex:ex+ew]
|
55 |
+
eye= cv2.resize(eye,(80,80))
|
56 |
+
eye= eye/255
|
57 |
+
eye= eye.reshape(80,80,3)
|
58 |
+
eye= np.expand_dims(eye,axis=0)
|
59 |
+
# preprocessing is done now model prediction
|
60 |
+
prediction = model.predict(eye)
|
61 |
+
|
62 |
+
# if eyes are closed
|
63 |
+
print(prediction)
|
64 |
+
if prediction[0][0]>0.25:
|
65 |
+
eye_cond=0
|
66 |
+
Score=Score+1
|
67 |
+
if(Score>25):
|
68 |
+
try:
|
69 |
+
sound.play()
|
70 |
+
except:
|
71 |
+
pass
|
72 |
+
|
73 |
+
# if eyes are open
|
74 |
+
elif prediction[0][1]>0.75:
|
75 |
+
eye_cond=1
|
76 |
+
Score = Score-1
|
77 |
+
if (Score<0):
|
78 |
+
Score=0
|
79 |
+
cv2.putText(frame,'Score'+str(Score),(10,height-20),fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale=1,color=(255,255,255),
|
80 |
+
thickness=1,lineType=cv2.LINE_AA)
|
81 |
+
FRAME_WINDOW.image(frame)
|
82 |
+
else:
|
83 |
+
st.write('Stopped')
|