Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,8 @@ import pandas as pd
|
|
4 |
import tensorflow as tf
|
5 |
from tensorflow import keras
|
6 |
import time
|
|
|
7 |
import streamlit as st
|
8 |
-
import pygame
|
9 |
-
|
10 |
-
# Initialize Pygame mixer
|
11 |
-
pygame.mixer.init()
|
12 |
-
|
13 |
-
# Function to play the alert sound
|
14 |
-
def play_alert_sound():
|
15 |
-
pygame.mixer.music.load(pygame.mixer.Sound(pygame.mixer.get_init())) # Load a simple beep sound
|
16 |
-
pygame.mixer.music.play()
|
17 |
|
18 |
# Using Har-cascade classifier from OpenCV
|
19 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
@@ -23,41 +15,36 @@ model = keras.models.load_model('my_model (1).h5')
|
|
23 |
|
24 |
# Title for GUI
|
25 |
st.title('Drowsiness Detection')
|
|
|
26 |
|
27 |
# Navigation Bar
|
28 |
nav_choice = st.sidebar.radio('Navigation', ('Home', 'Sleep Detection', 'Help Us Improve'), index=0)
|
29 |
|
30 |
# Home page
|
31 |
if nav_choice == 'Home':
|
32 |
-
st.header('
|
33 |
st.image('ISHN0619_C3_pic.jpg')
|
34 |
-
st.markdown(
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
'', unsafe_allow_html=True)
|
39 |
st.image('sleep.jfif', width=300)
|
40 |
-
st.markdown(
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
'target="_blank">here</a></font></b>'
|
52 |
-
, unsafe_allow_html=True)
|
53 |
-
|
54 |
# Sleep Detection page
|
55 |
elif nav_choice == 'Sleep Detection':
|
56 |
st.header('Image Prediction')
|
57 |
-
|
58 |
-
st.
|
59 |
-
st.warning(
|
60 |
-
'Keeping the eyes in the same state is important but you can obviously blink your eyes, if they are open!!!')
|
61 |
b = st.progress(0)
|
62 |
for i in range(100):
|
63 |
time.sleep(0.0001)
|
@@ -67,7 +54,7 @@ elif nav_choice == 'Sleep Detection':
|
|
67 |
|
68 |
if start == 'Start':
|
69 |
decision = 0
|
70 |
-
st.markdown('<font face="Comic sans MS"><b>Detected Facial Region of Interest(ROI)     
|
71 |
' Eye Features from the ROI</b></font>', unsafe_allow_html=True)
|
72 |
|
73 |
# Best of 3 mechanism for drowsiness detection
|
@@ -76,6 +63,7 @@ elif nav_choice == 'Sleep Detection':
|
|
76 |
ret, frame = cap.read()
|
77 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
78 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
|
|
79 |
# Proposal of face region by the har cascade classifier
|
80 |
for (x, y, w, h) in faces:
|
81 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
|
@@ -84,7 +72,7 @@ elif nav_choice == 'Sleep Detection':
|
|
84 |
frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
85 |
|
86 |
try:
|
87 |
-
#
|
88 |
centx, centy = roi_color.shape[:2]
|
89 |
centx //= 2
|
90 |
centy //= 2
|
@@ -124,10 +112,10 @@ elif nav_choice == 'Sleep Detection':
|
|
124 |
finally:
|
125 |
cap.release()
|
126 |
|
127 |
-
# If
|
128 |
if decision == 0:
|
129 |
st.error('Eye(s) are closed')
|
130 |
-
|
131 |
|
132 |
else:
|
133 |
st.success('Eyes are Opened')
|
@@ -137,14 +125,12 @@ elif nav_choice == 'Sleep Detection':
|
|
137 |
else:
|
138 |
st.header('Help Us Improve')
|
139 |
st.success('We would appreciate your Help!!!')
|
140 |
-
st.markdown(
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
, unsafe_allow_html=True)
|
147 |
-
st.warning('NOTE: Your identity will be kept anonymous, and only your eye-patch will be extracted!!!')
|
148 |
# Image upload
|
149 |
img_upload = st.file_uploader('Upload Image Here', ['png', 'jpg', 'jpeg'])
|
150 |
if img_upload is not None:
|
|
|
4 |
import tensorflow as tf
|
5 |
from tensorflow import keras
|
6 |
import time
|
7 |
+
from playsound import playsound
|
8 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Using Har-cascade classifier from OpenCV
|
11 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
|
15 |
|
16 |
# Title for GUI
|
17 |
st.title('Drowsiness Detection')
|
18 |
+
img = []
|
19 |
|
20 |
# Navigation Bar
|
21 |
nav_choice = st.sidebar.radio('Navigation', ('Home', 'Sleep Detection', 'Help Us Improve'), index=0)
|
22 |
|
23 |
# Home page
|
24 |
if nav_choice == 'Home':
|
25 |
+
st.header('Preventing Sleep Deprivation Road Accidents')
|
26 |
st.image('ISHN0619_C3_pic.jpg')
|
27 |
+
st.markdown("""
|
28 |
+
In accordance with a survey by the Times Of India, approximately 40% of road accidents are caused by sleep deprivation and fatigued drivers.
|
29 |
+
This app aims to address this issue by alerting drowsy drivers using deep learning models and computer vision.
|
30 |
+
""")
|
|
|
31 |
st.image('sleep.jfif', width=300)
|
32 |
+
st.markdown("""
|
33 |
+
### How to Use?
|
34 |
+
1. Go to the Sleep Detection page from the Navigation Sidebar.
|
35 |
+
2. Ensure you have sufficient lighting in your room.
|
36 |
+
3. Position yourself so that you are clearly visible in the webcam and stay close to it.
|
37 |
+
4. Keep your eyes in the same state (open or closed) for about 5 seconds while the webcam captures three pictures.
|
38 |
+
5. If your eyes are closed, the model will trigger a custom sound alert.
|
39 |
+
6. Otherwise, the model will continue monitoring your eyes at regular intervals.
|
40 |
+
**Note:** The dataset used for training the model is available [here](https://www.kaggle.com/kutaykutlu/drowsiness-detection).
|
41 |
+
""")
|
42 |
+
|
|
|
|
|
|
|
43 |
# Sleep Detection page
|
44 |
elif nav_choice == 'Sleep Detection':
|
45 |
st.header('Image Prediction')
|
46 |
+
st.success('Please look at your webcam and follow the instructions provided on the Home page.')
|
47 |
+
st.warning('Keeping your eyes in the same state is crucial. You can blink if your eyes are open!')
|
|
|
|
|
48 |
b = st.progress(0)
|
49 |
for i in range(100):
|
50 |
time.sleep(0.0001)
|
|
|
54 |
|
55 |
if start == 'Start':
|
56 |
decision = 0
|
57 |
+
st.markdown('<font face="Comic sans MS"><b>Detected Facial Region of Interest (ROI)      Extracted'
|
58 |
' Eye Features from the ROI</b></font>', unsafe_allow_html=True)
|
59 |
|
60 |
# Best of 3 mechanism for drowsiness detection
|
|
|
63 |
ret, frame = cap.read()
|
64 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
65 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
66 |
+
|
67 |
# Proposal of face region by the har cascade classifier
|
68 |
for (x, y, w, h) in faces:
|
69 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
|
|
|
72 |
frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
73 |
|
74 |
try:
|
75 |
+
# Centroid method for extraction of eye-patch
|
76 |
centx, centy = roi_color.shape[:2]
|
77 |
centx //= 2
|
78 |
centy //= 2
|
|
|
112 |
finally:
|
113 |
cap.release()
|
114 |
|
115 |
+
# If eyes are closed, play custom sound alert
|
116 |
if decision == 0:
|
117 |
st.error('Eye(s) are closed')
|
118 |
+
playsound("232857-84052cf6-66a1-4c60-ad86-9ebc19eaab52.mp3")
|
119 |
|
120 |
else:
|
121 |
st.success('Eyes are Opened')
|
|
|
125 |
else:
|
126 |
st.header('Help Us Improve')
|
127 |
st.success('We would appreciate your Help!!!')
|
128 |
+
st.markdown("""
|
129 |
+
To improve this app, we need your feedback and contributions.
|
130 |
+
As part of our efforts, we would like to gather more data.
|
131 |
+
This will help us enhance the accuracy and usability of the app.
|
132 |
+
Your identity will remain anonymous, and only your eye-patch will be extracted for analysis.
|
133 |
+
""")
|
|
|
|
|
134 |
# Image upload
|
135 |
img_upload = st.file_uploader('Upload Image Here', ['png', 'jpg', 'jpeg'])
|
136 |
if img_upload is not None:
|