Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,137 @@
|
|
1 |
-
import
|
2 |
import streamlit as st
|
3 |
from ultralytics import YOLO
|
|
|
|
|
|
|
4 |
from gtts import gTTS
|
|
|
5 |
import tempfile
|
6 |
-
import
|
7 |
-
|
8 |
-
# Load YOLOv8 model
|
9 |
-
@st.cache_resource
|
10 |
-
def load_model():
|
11 |
-
return YOLO("yolov8n.pt") # Pre-trained YOLOv8 model
|
12 |
-
|
13 |
-
model = load_model()
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
"This app detects objects in real-time via an IP webcam and provides audio feedback."
|
19 |
-
)
|
20 |
-
|
21 |
-
# Input for IP Webcam URL
|
22 |
-
ip_webcam_url = st.text_input(
|
23 |
-
"Enter your IP Webcam URL (e.g., http://<ip-address>:8080/video):",
|
24 |
-
value="http://<your-ip>:8080/video",
|
25 |
-
)
|
26 |
-
|
27 |
-
# Start detection
|
28 |
-
start_detection = st.button("Start Real-Time Detection")
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
if start_detection:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
st.error("Error: Invalid URL. Ensure the URL starts with 'http://'.")
|
37 |
-
else:
|
38 |
-
# Open video stream
|
39 |
-
st.write("Connecting to the webcam stream...")
|
40 |
-
cap = cv2.VideoCapture(ip_webcam_url)
|
41 |
-
|
42 |
-
if not cap.isOpened():
|
43 |
-
st.error(
|
44 |
-
"Error: Could not connect to the webcam stream. Ensure the IP Webcam URL is correct and accessible."
|
45 |
-
)
|
46 |
else:
|
47 |
-
|
48 |
-
|
49 |
-
while cap.isOpened():
|
50 |
-
ret, frame = cap.read()
|
51 |
if not ret:
|
52 |
-
st.error("Error:
|
53 |
break
|
54 |
|
55 |
-
|
56 |
-
results = model(frame)
|
57 |
-
detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
|
58 |
-
|
59 |
-
# Generate audio feedback for detected objects
|
60 |
-
if detected_objects:
|
61 |
-
objects_text = ", ".join(set(detected_objects))
|
62 |
-
feedback_text = f"Detected: {objects_text}."
|
63 |
-
st.write(feedback_text)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
tts.save(audio_file.name)
|
69 |
-
os.system(f"mpg123 {audio_file.name}") # Play the audio file
|
70 |
|
71 |
-
#
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
stframe.image(annotated_frame, channels="BGR", use_column_width=True)
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
3 |
from ultralytics import YOLO
|
4 |
+
import cv2
|
5 |
+
import random
|
6 |
+
import time
|
7 |
from gtts import gTTS
|
8 |
+
import pygame
|
9 |
import tempfile
|
10 |
+
import threading
|
11 |
+
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Initialize pygame mixer for audio alerts
|
14 |
+
pygame.mixer.quit()
|
15 |
+
pygame.mixer.init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Load YOLOv8 model
|
18 |
+
yolo = YOLO("yolov8n.pt")
|
19 |
+
|
20 |
+
# Streamlit app layout
|
21 |
+
st.set_page_config(page_title="Assistive Vision App", layout="wide")
|
22 |
+
st.title("Real-Time Object Detection with Assistive Vision")
|
23 |
+
st.write("This app detects objects in real-time using a webcam feed and provides audio feedback for visually impaired people.")
|
24 |
+
|
25 |
+
# Placeholder for video feed
|
26 |
+
video_placeholder = st.empty()
|
27 |
+
|
28 |
+
# User controls
|
29 |
+
start_detection = st.button("Start Detection")
|
30 |
+
stop_detection = st.button("Stop Detection")
|
31 |
+
enable_audio = st.checkbox("Enable Audio Alerts", value=True)
|
32 |
+
|
33 |
+
# Audio alerts settings
|
34 |
+
alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
|
35 |
+
last_alert_time = {}
|
36 |
+
alert_cooldown = timedelta(seconds=10) # Cooldown for audio alerts
|
37 |
+
|
38 |
+
# Create a directory for temporary audio files
|
39 |
+
audio_temp_dir = "audio_temp_files"
|
40 |
+
if not os.path.exists(audio_temp_dir):
|
41 |
+
os.makedirs(audio_temp_dir)
|
42 |
+
|
43 |
+
def play_audio_alert(label, position):
|
44 |
+
"""Generate and play an audio alert."""
|
45 |
+
phrases = [
|
46 |
+
f"Be careful, there's a {label} on your {position}.",
|
47 |
+
f"Watch out! {label} detected on your {position}.",
|
48 |
+
f"Alert! A {label} is on your {position}.",
|
49 |
+
]
|
50 |
+
alert_text = random.choice(phrases)
|
51 |
+
|
52 |
+
temp_audio_path = os.path.join(audio_temp_dir, f"alert_{datetime.now().strftime('%Y%m%d_%H%M%S_%f')}.mp3")
|
53 |
+
tts = gTTS(alert_text)
|
54 |
+
tts.save(temp_audio_path)
|
55 |
+
|
56 |
+
try:
|
57 |
+
pygame.mixer.music.load(temp_audio_path)
|
58 |
+
pygame.mixer.music.play()
|
59 |
+
|
60 |
+
def cleanup_audio():
|
61 |
+
while pygame.mixer.music.get_busy():
|
62 |
+
time.sleep(0.1)
|
63 |
+
pygame.mixer.music.stop()
|
64 |
+
if os.path.exists(temp_audio_path):
|
65 |
+
os.remove(temp_audio_path)
|
66 |
+
|
67 |
+
threading.Thread(target=cleanup_audio, daemon=True).start()
|
68 |
+
|
69 |
+
except Exception as e:
|
70 |
+
print(f"Audio playback error: {e}")
|
71 |
+
|
72 |
+
def process_frame(frame, enable_audio):
|
73 |
+
"""Process a single video frame."""
|
74 |
+
results = yolo(frame)
|
75 |
+
result = results[0]
|
76 |
+
|
77 |
+
detected_objects = {}
|
78 |
+
for box in result.boxes:
|
79 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
80 |
+
label = result.names[int(box.cls[0])]
|
81 |
+
|
82 |
+
# Only alert for specific categories
|
83 |
+
if enable_audio and label not in alert_categories:
|
84 |
+
continue
|
85 |
+
|
86 |
+
# Determine object position
|
87 |
+
frame_center_x = frame.shape[1] // 2
|
88 |
+
object_center_x = (x1 + x2) // 2
|
89 |
+
position = "left" if object_center_x < frame_center_x else "right"
|
90 |
+
|
91 |
+
detected_objects[label] = position
|
92 |
+
|
93 |
+
# Draw bounding box and label on the frame
|
94 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
95 |
+
cv2.putText(frame, f"{label}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
96 |
+
|
97 |
+
return detected_objects, frame
|
98 |
+
|
99 |
+
# Real-time detection logic
|
100 |
if start_detection:
|
101 |
+
st.success("Starting real-time object detection...")
|
102 |
+
try:
|
103 |
+
video_capture = cv2.VideoCapture(0)
|
104 |
+
if not video_capture.isOpened():
|
105 |
+
st.error("Error: Could not access the webcam. Please check your webcam settings.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
else:
|
107 |
+
while not stop_detection:
|
108 |
+
ret, frame = video_capture.read()
|
|
|
|
|
109 |
if not ret:
|
110 |
+
st.error("Error: Unable to read from webcam. Please check your webcam.")
|
111 |
break
|
112 |
|
113 |
+
detected_objects, processed_frame = process_frame(frame, enable_audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
# Convert frame to RGB for Streamlit display
|
116 |
+
frame_rgb = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
|
117 |
+
video_placeholder.image(frame_rgb, channels="RGB", use_column_width=True)
|
|
|
|
|
118 |
|
119 |
+
# Play audio alerts
|
120 |
+
if enable_audio:
|
121 |
+
current_time = datetime.now()
|
122 |
+
for label, position in detected_objects.items():
|
123 |
+
if label not in last_alert_time or current_time - last_alert_time[label] > alert_cooldown:
|
124 |
+
play_audio_alert(label, position)
|
125 |
+
last_alert_time[label] = current_time
|
126 |
|
127 |
+
time.sleep(0.1)
|
|
|
128 |
|
129 |
+
except Exception as e:
|
130 |
+
st.error(f"An error occurred: {e}")
|
131 |
+
finally:
|
132 |
+
video_capture.release()
|
133 |
+
cv2.destroyAllWindows()
|
134 |
+
pygame.mixer.quit()
|
135 |
|
136 |
+
elif stop_detection:
|
137 |
+
st.warning("Real-time object detection stopped.")
|