File size: 6,310 Bytes
e57738e
 
 
 
 
 
4d8e3b8
e57738e
8afc213
6fb550c
 
 
 
 
 
e57738e
 
4d8e3b8
e57738e
 
 
8afc213
e57738e
 
4d8e3b8
e57738e
4d8e3b8
 
 
 
e57738e
4d8e3b8
 
 
 
 
 
 
 
 
 
 
e57738e
 
 
4d8e3b8
 
e57738e
 
 
 
 
 
 
 
 
4d8e3b8
e57738e
 
 
 
 
 
 
 
4d8e3b8
e57738e
 
 
 
 
 
 
 
4d8e3b8
e57738e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fb550c
e57738e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d8e3b8
e57738e
 
4d8e3b8
6fb550c
e57738e
 
 
 
 
 
 
 
4d8e3b8
 
 
 
 
 
e57738e
 
 
 
 
 
 
 
 
 
 
 
8afc213
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import cv2
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import time
from playsound import playsound
import streamlit as st

# Using Har-cascade classifier from OpenCV
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Loading the trained model for prediction purpose
model = keras.models.load_model('my_model (1).h5')

# Title for GUI
st.title('Drowsiness Detection')
img = []

# Navigation Bar
nav_choice = st.sidebar.radio('Navigation', ('Home', 'Sleep Detection', 'Help Us Improve'), index=0)

# Home page
if nav_choice == 'Home':
    st.header('Preventing Sleep Deprivation Road Accidents')
    st.image('ISHN0619_C3_pic.jpg')
    st.markdown("""
        In accordance with a survey by the Times Of India, approximately 40% of road accidents are caused by sleep deprivation and fatigued drivers. 
        This app aims to address this issue by alerting drowsy drivers using deep learning models and computer vision.
        """)
    st.image('sleep.jfif', width=300)
    st.markdown("""
        ### How to Use?
        1. Go to the Sleep Detection page from the Navigation Sidebar.
        2. Ensure you have sufficient lighting in your room.
        3. Position yourself so that you are clearly visible in the webcam and stay close to it.
        4. Keep your eyes in the same state (open or closed) for about 5 seconds while the webcam captures three pictures.
        5. If your eyes are closed, the model will trigger a custom sound alert.
        6. Otherwise, the model will continue monitoring your eyes at regular intervals.
        **Note:** The dataset used for training the model is available [here](https://www.kaggle.com/kutaykutlu/drowsiness-detection).
        """)

# Sleep Detection page
elif nav_choice == 'Sleep Detection':
    st.header('Image Prediction')
    st.success('Please look at your webcam and follow the instructions provided on the Home page.')
    st.warning('Keeping your eyes in the same state is crucial. You can blink if your eyes are open!')
    b = st.progress(0)
    for i in range(100):
        time.sleep(0.0001)
        b.progress(i + 1)

    start = st.radio('Options', ('Start', 'Stop'), key='Start_pred', index=1)

    if start == 'Start':
        decision = 0
        st.markdown('<font face="Comic sans MS"><b>Detected Facial Region of Interest (ROI) &emsp;&emsp;&emsp;&emsp;&emsp;Extracted'
                    ' Eye Features from the ROI</b></font>', unsafe_allow_html=True)
        
        # Best of 3 mechanism for drowsiness detection
        for _ in range(3):
            cap = cv2.VideoCapture(0)
            ret, frame = cap.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            
            # Proposal of face region by the har cascade classifier
            for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
                roi_gray = gray[y:y + w, x:x + w]
                roi_color = frame[y:y + h, x:x + w]
            frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            try:
                # Centroid method for extraction of eye-patch 
                centx, centy = roi_color.shape[:2]
                centx //= 2
                centy //= 2
                eye_1 = roi_color[centy - 40: centy, centx - 70: centx]
                eye_1 = cv2.resize(eye_1, (86, 86))
                eye_2 = roi_color[centy - 40: centy, centx: centx + 70]
                eye_2 = cv2.resize(eye_2, (86, 86))
                cv2.rectangle(frame1, (x + centx - 60, y + centy - 40), (x + centx - 10, y + centy), (0, 255, 0), 5)
                cv2.rectangle(frame1, (x + centx + 10, y + centy - 40), (x + centx + 60, y + centy), (0, 255, 0), 5)
                preds_eye1 = model.predict(np.expand_dims(eye_1, axis=0))
                preds_eye2 = model.predict(np.expand_dims(eye_2, axis=0))
                e1, e2 = np.argmax(preds_eye1), np.argmax(preds_eye2)
                
                # Display of face image and extracted eye-patch
                img_container = st.beta_columns(4)
                img_container[0].image(frame1, width=250)
                img_container[2].image(cv2.cvtColor(eye_1, cv2.COLOR_BGR2RGB), width=150)
                img_container[3].image(cv2.cvtColor(eye_2, cv2.COLOR_BGR2RGB), width=150)
                print(e1, e2)
                
                # Decision variable for prediction
                if e1 == 1 or e2 == 1:
                    pass
                else:
                    decision += 1

            except NameError:
                st.warning('Hold your camera closer!!!\nTrying again in 2s')
                cap.release()
                time.sleep(1)
                continue

            except:
                cap.release()
                continue

            finally:
                cap.release()

        # If eyes are closed, play custom sound alert
        if decision == 0:
            st.error('Eye(s) are closed')
            playsound("232857-84052cf6-66a1-4c60-ad86-9ebc19eaab52.mp3")

        else:
            st.success('Eyes are Opened')
        st.warning('Please select "Stop" and then "Start" to try again')

# Help Us Improve page
else:
    st.header('Help Us Improve')
    st.success('We would appreciate your Help!!!')
    st.markdown("""
        To improve this app, we need your feedback and contributions. 
        As part of our efforts, we would like to gather more data.
        This will help us enhance the accuracy and usability of the app.
        Your identity will remain anonymous, and only your eye-patch will be extracted for analysis.
        """)
    # Image upload
    img_upload = st.file_uploader('Upload Image Here', ['png', 'jpg', 'jpeg'])
    if img_upload is not None:
        prog = st.progress(0)
        to_add = cv2.imread(str(img_upload.read()), 0)
        to_add = pd.DataFrame(to_add)
        
        # Save it in the database
        to_add.to_csv('Data_from_users.csv', mode='a', header=False, index=False, sep=';')
        for i in range(100):
            time.sleep(0.001)
            prog.progress(i + 1)
        st.success('Uploaded Successfully!!! Thank you for contributing.')