practisebook commited on
Commit
b40700a
·
verified ·
1 Parent(s): 40c4742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -56
app.py CHANGED
@@ -5,40 +5,71 @@ 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."""
@@ -54,23 +85,14 @@ def play_audio_alert(label, position):
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
 
@@ -79,48 +101,55 @@ def process_frame(frame, enable_audio):
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
 
@@ -129,9 +158,9 @@ if start_detection:
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.")
 
5
  import random
6
  import time
7
  from gtts import gTTS
8
+ from playsound import playsound
 
 
9
  from datetime import datetime, timedelta
10
 
 
 
 
 
11
  # Load YOLOv8 model
12
  yolo = YOLO("yolov8n.pt")
13
 
14
  # Streamlit app layout
15
  st.set_page_config(page_title="Assistive Vision App", layout="wide")
16
+ st.markdown(
17
+ """
18
+ <style>
19
+ body {
20
+ background-color: #f7f9fc;
21
+ font-family: "Arial", sans-serif;
22
+ }
23
+ .stButton>button {
24
+ background-color: #1a73e8;
25
+ color: white;
26
+ justify-content: center;
27
+ align-items: center;
28
+ border-radius: 10px;
29
+ padding: 10px;
30
+ margin: 5px;
31
+ }
32
+ .stCheckbox {
33
+ margin-top: 20px;
34
+ }
35
+ </style>
36
+ """,
37
+ unsafe_allow_html=True,
38
+ )
39
+
40
+ # Display welcome image
41
+ welcome_image_path = "bismillah.png"
42
+ if os.path.exists(welcome_image_path):
43
+ st.image(welcome_image_path, use_container_width=True, caption="Bismillah hir Rehman Ar Raheem")
44
+ else:
45
+ st.warning("Welcome image not found! Please add 'bismillah.png' in the script directory.")
46
+
47
+ st.title("Object Detection & Assistive Vision App for Visually Impaired People")
48
+ st.write("This application provides real-time object recognition and optional audio alerts.")
49
+
50
+ # Directory to store temp audio files
51
+ audio_temp_dir = "audio_temp_files"
52
+ if not os.path.exists(audio_temp_dir):
53
+ os.makedirs(audio_temp_dir)
54
 
55
+ # Placeholder for video frames
56
+ stframe = st.empty()
57
 
58
  # User controls
59
+ col1, col2 = st.columns(2)
60
+ with col1:
61
+ start_detection = st.button("Start Detection")
62
+ with col2:
63
+ stop_detection = st.button("Stop Detection")
64
+ audio_activation = st.checkbox("Enable Audio Alerts", value=False)
65
+
66
+ # Categories for audio alerts
67
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
68
+
69
+ # Dictionary to store the last alert timestamp for each object
70
  last_alert_time = {}
71
+ alert_cooldown = timedelta(seconds=10) # 10-second cooldown for alerts
72
 
 
 
 
 
73
 
74
  def play_audio_alert(label, position):
75
  """Generate and play an audio alert."""
 
85
  tts.save(temp_audio_path)
86
 
87
  try:
88
+ playsound(temp_audio_path)
89
+ os.remove(temp_audio_path) # Clean up after playing
 
 
 
 
 
 
 
 
 
 
90
  except Exception as e:
91
  print(f"Audio playback error: {e}")
92
 
93
+
94
+ def process_frame(frame, audio_mode):
95
+ """Process a single video frame for object detection."""
96
  results = yolo(frame)
97
  result = results[0]
98
 
 
101
  x1, y1, x2, y2 = map(int, box.xyxy[0])
102
  label = result.names[int(box.cls[0])]
103
 
104
+ if audio_mode and label not in alert_categories:
 
105
  continue
106
 
 
107
  frame_center_x = frame.shape[1] // 2
108
+ obj_center_x = (x1 + x2) // 2
109
+ position = "left" if obj_center_x < frame_center_x else "right"
110
 
111
  detected_objects[label] = position
112
 
 
113
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
114
+ cv2.putText(
115
+ frame,
116
+ f"{label}",
117
+ (x1, y1 - 10),
118
+ cv2.FONT_HERSHEY_SIMPLEX,
119
+ 0.5,
120
+ (0, 255, 0),
121
+ 2,
122
+ )
123
 
124
  return detected_objects, frame
125
 
126
+
127
+ # Main logic
128
  if start_detection:
129
+ st.success("Object detection started.")
130
  try:
131
  video_capture = cv2.VideoCapture(0)
132
  if not video_capture.isOpened():
133
+ st.error("Could not access the webcam. Please check your camera settings.")
134
  else:
135
  while not stop_detection:
136
  ret, frame = video_capture.read()
137
  if not ret:
138
+ st.error("Failed to capture video. Please check your camera.")
139
  break
140
 
141
+ detected_objects, processed_frame = process_frame(frame, audio_activation)
142
 
 
143
  frame_rgb = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
144
+ stframe.image(frame_rgb, channels="RGB", use_column_width=True)
145
 
146
+ if audio_activation:
 
147
  current_time = datetime.now()
148
  for label, position in detected_objects.items():
149
+ if (
150
+ label not in last_alert_time
151
+ or current_time - last_alert_time[label] > alert_cooldown
152
+ ):
153
  play_audio_alert(label, position)
154
  last_alert_time[label] = current_time
155
 
 
158
  except Exception as e:
159
  st.error(f"An error occurred: {e}")
160
  finally:
161
+ if 'video_capture' in locals() and video_capture.isOpened():
162
+ video_capture.release()
163
+ cv2.destroyAllWindows()
164
 
165
  elif stop_detection:
166
+ st.warning("Object detection stopped.")