Spaces:
Sleeping
Sleeping
bushra1dajam
commited on
Commit
•
ef28085
1
Parent(s):
570c0f0
Upload 2 files
Browse files
alarm.mp3
ADDED
Binary file (33 kB). View file
|
|
app.py
CHANGED
@@ -1,150 +1,96 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import cv2
|
3 |
-
from ultralytics import YOLO
|
4 |
-
import cvzone
|
5 |
-
import math
|
6 |
-
import pygame
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
st.
|
28 |
-
|
29 |
-
|
30 |
-
st.
|
31 |
-
|
32 |
-
|
33 |
-
st.
|
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 |
-
alert_placeholder.markdown(
|
98 |
-
f'<div style="color: red; font-size: 24px; border: 2px solid red; padding: 10px;">**Be careful! Drowsiness detected!**</div>',
|
99 |
-
unsafe_allow_html=True,
|
100 |
-
)
|
101 |
-
drowsy_count = 0 # Reset the counter after playing the sound
|
102 |
-
|
103 |
-
# Convert image back to RGB for Streamlit
|
104 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
105 |
-
|
106 |
-
# Display the image
|
107 |
-
stframe.image(frame, channels="RGB")
|
108 |
-
|
109 |
-
# Check if stop button is pressed
|
110 |
-
if stop_button:
|
111 |
-
break
|
112 |
-
|
113 |
-
cap.release()
|
114 |
-
status_text.write("Webcam stopped.")
|
115 |
-
message_text.write("")
|
116 |
-
alert_placeholder.empty()
|
117 |
-
|
118 |
-
elif page == "Image Upload":
|
119 |
-
st.header("Drowsiness Detection on Image")
|
120 |
-
|
121 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
122 |
-
|
123 |
-
if uploaded_file is not None:
|
124 |
-
# Read the image
|
125 |
-
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
126 |
-
frame = cv2.imdecode(file_bytes, 1)
|
127 |
-
|
128 |
-
# Perform prediction
|
129 |
-
results = model(frame, stream=True)
|
130 |
-
|
131 |
-
# Process the results
|
132 |
-
for result in results:
|
133 |
-
boxes = result.boxes
|
134 |
-
for box in boxes:
|
135 |
-
confidence = box.conf[0]
|
136 |
-
confidence = math.ceil(confidence * 100)
|
137 |
-
Class = int(box.cls[0])
|
138 |
-
if confidence > 50:
|
139 |
-
x1, y1, x2, y2 = box.xyxy[0]
|
140 |
-
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
141 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 5)
|
142 |
-
cv2.putText(frame, f'{classnames[Class]} {confidence}%', (x1 + 8, y1 + 100),
|
143 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 2, cv2.LINE_AA)
|
144 |
-
|
145 |
-
# Convert image back to RGB for Streamlit
|
146 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
147 |
-
|
148 |
-
# Display the image
|
149 |
-
st.image(frame, channels="RGB")
|
150 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import cvzone
|
5 |
+
import math
|
6 |
+
import pygame
|
7 |
+
|
8 |
+
# Initialize pygame mixer
|
9 |
+
pygame.mixer.init()
|
10 |
+
|
11 |
+
# Load sound
|
12 |
+
alert_sound = pygame.mixer.Sound('siren-alert-96052.mp3') # Using raw string
|
13 |
+
|
14 |
+
# Load the model
|
15 |
+
model = YOLO('best.pt')
|
16 |
+
|
17 |
+
# Reading the classes
|
18 |
+
classnames = ['Drowsy', 'Awake']
|
19 |
+
|
20 |
+
# Streamlit UI
|
21 |
+
st.title("Real-Time Drowsiness Detection")
|
22 |
+
|
23 |
+
# Layout
|
24 |
+
col1, col2 = st.columns(2)
|
25 |
+
|
26 |
+
with col1:
|
27 |
+
start_button = st.button('Start Webcam')
|
28 |
+
|
29 |
+
with col2:
|
30 |
+
stop_button = st.button('Stop Webcam')
|
31 |
+
|
32 |
+
stframe = st.empty()
|
33 |
+
status_text = st.empty()
|
34 |
+
message_text = st.empty()
|
35 |
+
|
36 |
+
if start_button:
|
37 |
+
cap = cv2.VideoCapture(0)
|
38 |
+
drowsy_count = 0 # Counter for consecutive "Drowsy" detections
|
39 |
+
|
40 |
+
while cap.isOpened():
|
41 |
+
ret, frame = cap.read()
|
42 |
+
if not ret:
|
43 |
+
status_text.write("Failed to grab frame")
|
44 |
+
break
|
45 |
+
|
46 |
+
frame = cv2.resize(frame, (640, 480))
|
47 |
+
|
48 |
+
# Run the model on the frame
|
49 |
+
result = model(frame, stream=True)
|
50 |
+
|
51 |
+
# Flag to track if "Drowsy" is detected in this frame
|
52 |
+
drowsy_detected = False
|
53 |
+
|
54 |
+
# Getting bbox, confidence, and class name information to work with
|
55 |
+
for info in result:
|
56 |
+
boxes = info.boxes
|
57 |
+
for box in boxes:
|
58 |
+
confidence = box.conf[0]
|
59 |
+
confidence = math.ceil(confidence * 100)
|
60 |
+
Class = int(box.cls[0])
|
61 |
+
if confidence > 50:
|
62 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
63 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
64 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 5)
|
65 |
+
cvzone.putTextRect(frame, f'{classnames[Class]} {confidence}%', [x1 + 8, y1 + 100],
|
66 |
+
scale=1.5, thickness=2)
|
67 |
+
if classnames[Class] == 'Drowsy':
|
68 |
+
drowsy_detected = True
|
69 |
+
|
70 |
+
# Increment the counter if "Drowsy" is detected, otherwise reset the counter
|
71 |
+
if drowsy_detected:
|
72 |
+
drowsy_count += 1
|
73 |
+
status_text.write("Drowsiness detected!")
|
74 |
+
else:
|
75 |
+
drowsy_count = 0
|
76 |
+
status_text.write("Monitoring...")
|
77 |
+
|
78 |
+
# Play alert sound and send message if "Drowsy" is detected 3 or more times
|
79 |
+
if drowsy_count >= 3:
|
80 |
+
pygame.mixer.Sound.play(alert_sound)
|
81 |
+
message_text.write("**Be careful!** Drowsiness detected multiple times!")
|
82 |
+
drowsy_count = 0 # Reset the counter after playing the sound
|
83 |
+
|
84 |
+
# Convert image back to RGB for Streamlit
|
85 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
86 |
+
|
87 |
+
# Display the image
|
88 |
+
stframe.image(frame, channels="RGB")
|
89 |
+
|
90 |
+
# Check if stop button is pressed
|
91 |
+
if stop_button:
|
92 |
+
break
|
93 |
+
|
94 |
+
cap.release()
|
95 |
+
status_text.write("Webcam stopped.")
|
96 |
+
message_text.write("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|