ai-lover commited on
Commit
66272fb
·
verified ·
1 Parent(s): 514927a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -5,21 +5,24 @@ import cv2
5
  import random
6
  import time
7
  from gtts import gTTS
 
8
  import threading
9
  from datetime import datetime, timedelta
10
 
11
- # Detect if running in a cloud environment (e.g., Hugging Face Spaces)
12
- running_in_cloud = "HF_HOME" in os.environ
13
 
14
- if not running_in_cloud:
 
15
  try:
16
- import pygame
17
  pygame.mixer.quit() # Ensure the mixer is fully stopped
18
  pygame.mixer.init()
19
- except ImportError:
20
- print("pygame module not found, skipping audio initialization.")
 
21
  else:
22
- print("Running in cloud mode: Skipping pygame initialization.")
 
23
 
24
  # Load YOLOv8 model
25
  yolo = YOLO("yolov8n.pt")
@@ -51,7 +54,7 @@ st.markdown(
51
  )
52
 
53
  # Display welcome image
54
- welcome_image_path = "bismillah.png" # Ensure this image exists in the script's directory
55
  if os.path.exists(welcome_image_path):
56
  st.image(welcome_image_path, use_container_width=True, caption="Bismillah hir Rehman Ar Raheem")
57
  else:
@@ -74,7 +77,7 @@ with col1:
74
  start_detection = st.button("Start Detection")
75
  with col2:
76
  stop_detection = st.button("Stop Detection")
77
- audio_activation = st.checkbox("Enable Audio Alerts", value=False) if not running_in_cloud else False
78
 
79
  # Categories for audio alerts (hazardous objects or living things)
80
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
@@ -86,9 +89,8 @@ alert_cooldown = timedelta(seconds=10) # 10-second cooldown for alerts
86
 
87
  def play_audio_alert(label, position):
88
  """Generate and play an audio alert."""
89
- if running_in_cloud:
90
- print(f"Audio alert triggered for {label} on {position}. (Audio playback disabled in cloud)")
91
- return
92
 
93
  phrases = [
94
  f"Be careful, there's a {label} on your {position}.",
@@ -191,7 +193,7 @@ if start_detection:
191
  if 'video_capture' in locals() and video_capture.isOpened():
192
  video_capture.release()
193
  cv2.destroyAllWindows()
194
- if not running_in_cloud:
195
  pygame.mixer.quit()
196
 
197
  elif stop_detection:
 
5
  import random
6
  import time
7
  from gtts import gTTS
8
+ import pygame
9
  import threading
10
  from datetime import datetime, timedelta
11
 
12
+ # Check if running in a headless environment
13
+ is_headless = os.getenv("DISPLAY") is None
14
 
15
+ # Initialize pygame mixer if not in headless mode
16
+ if not is_headless:
17
  try:
 
18
  pygame.mixer.quit() # Ensure the mixer is fully stopped
19
  pygame.mixer.init()
20
+ except pygame.error as e:
21
+ print(f"Pygame mixer initialization failed: {e}")
22
+ pygame.mixer = None
23
  else:
24
+ pygame.mixer = None
25
+ print("Headless environment detected. Audio playback disabled.")
26
 
27
  # Load YOLOv8 model
28
  yolo = YOLO("yolov8n.pt")
 
54
  )
55
 
56
  # Display welcome image
57
+ welcome_image_path = "bismillah.png"
58
  if os.path.exists(welcome_image_path):
59
  st.image(welcome_image_path, use_container_width=True, caption="Bismillah hir Rehman Ar Raheem")
60
  else:
 
77
  start_detection = st.button("Start Detection")
78
  with col2:
79
  stop_detection = st.button("Stop Detection")
80
+ audio_activation = st.checkbox("Enable Audio Alerts", value=False)
81
 
82
  # Categories for audio alerts (hazardous objects or living things)
83
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
 
89
 
90
  def play_audio_alert(label, position):
91
  """Generate and play an audio alert."""
92
+ if pygame.mixer is None:
93
+ return # Skip audio playback in headless mode
 
94
 
95
  phrases = [
96
  f"Be careful, there's a {label} on your {position}.",
 
193
  if 'video_capture' in locals() and video_capture.isOpened():
194
  video_capture.release()
195
  cv2.destroyAllWindows()
196
+ if pygame.mixer:
197
  pygame.mixer.quit()
198
 
199
  elif stop_detection: