itsanurag commited on
Commit
4d8e3b8
·
verified ·
1 Parent(s): a86f78f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -45
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('Prevents sleep deprivation road accidents, by alerting drowsy drivers.')
33
  st.image('ISHN0619_C3_pic.jpg')
34
- st.markdown('<b>In accordance with the survey taken by the Times Of India, about 40 % of road </b>'
35
- '<b>accidents are caused</b> '
36
- '<b>due to sleep deprivation & fatigued drivers. In order to address this issue, this app will </b>'
37
- '<b>alert such drivers with the help of deep learning models and computer vision.</b>'
38
- '', unsafe_allow_html=True)
39
  st.image('sleep.jfif', width=300)
40
- st.markdown('<h1>How to use?<br></h1>'
41
- '<b>1. Go to Sleep Detection page from the Navigation Side-Bar.</b><br>'
42
- '<b>2. Make sure that, you have sufficient amount of light, in your room.</b><br>'
43
- '<b>3. Align yourself such that, you are clearly visible in the web-cam and '
44
- 'stay closer to the web-cam. </b><br>'
45
- '<b>4. Web-cam will take 3 pictures of you, so keep your eyes in the same state'
46
- ' (open or closed) for about 5 seconds.</b><br>'
47
- '<b>5. If your eyes are closed, the model will make a beep sound to alert you.</b><br>'
48
- '<b>6. Otherwise, the model will continue taking your pictures at regular intervals of time.</b><br>'
49
- '<font color="red"><br><b>For the purpose of the training process of the model, '
50
- 'dataset used is available <a href="https://www.kaggle.com/kutaykutlu/drowsiness-detection", '
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
- cap = 0
58
- st.success('Please look at your web-cam, while following all the instructions given on the Home page.')
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)&emsp;&emsp;&emsp;&emsp;&emsp;Extractd'
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
- # Cenentroid method for extraction of eye-patch
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 found drowsy, then make a beep sound to alert the driver
128
  if decision == 0:
129
  st.error('Eye(s) are closed')
130
- play_alert_sound()
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
- '<font face="Comic sans MS">To make this app better, we would appreciate your small amount of time.</font>'
142
- '<font face="Comic sans MS">Let me take you through, some of the basic statistical analysis of this </font>'
143
- '<font face="Comic sans MS">model. <br><b>Accuracy with naked eyes = 99.5%<br>Accuracy with spectacles = 96.8%</b><br></font> '
144
- '<font face="Comic sans MS">As we can see here, accuracy with spectacles is not at all spectacular, and hence to make this app </font>'
145
- '<font face="Comic sans MS">better, and to use it in real-time situations, we require as much data as we can gather.</font> '
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) &emsp;&emsp;&emsp;&emsp;&emsp;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: