ai-lover commited on
Commit
33f966c
·
verified ·
1 Parent(s): 1036e31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -9,9 +9,16 @@ import pygame
9
  import threading
10
  from datetime import datetime, timedelta
11
 
12
- # Initialize pygame mixer
13
- pygame.mixer.quit() # Ensure the mixer is fully stopped
14
- pygame.mixer.init()
 
 
 
 
 
 
 
15
 
16
  # Load YOLOv8 model
17
  yolo = YOLO("yolov8n.pt")
@@ -42,15 +49,6 @@ st.markdown(
42
  unsafe_allow_html=True,
43
  )
44
 
45
- # Create a container for the buttons
46
- st.markdown('<div class="button-container">', unsafe_allow_html=True)
47
-
48
- # # Render buttons inside the container
49
- # start_detection = st.button("Start Detection", key="start")
50
- # stop_detection = st.button("Stop Detection", key="stop")
51
-
52
- st.markdown('</div>', unsafe_allow_html=True)
53
-
54
  # Display welcome image
55
  welcome_image_path = "bismillah.png" # Ensure this image exists in the script's directory
56
  if os.path.exists(welcome_image_path):
@@ -75,7 +73,7 @@ with col1:
75
  start_detection = st.button("Start Detection")
76
  with col2:
77
  stop_detection = st.button("Stop Detection")
78
- audio_activation = st.checkbox("Enable Audio Alerts", value=False)
79
 
80
  # Categories for audio alerts (hazardous objects or living things)
81
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
@@ -87,6 +85,10 @@ alert_cooldown = timedelta(seconds=10) # 10-second cooldown for alerts
87
 
88
  def play_audio_alert(label, position):
89
  """Generate and play an audio alert."""
 
 
 
 
90
  phrases = [
91
  f"Be careful, there's a {label} on your {position}.",
92
  f"Watch out! {label} detected on your {position}.",
@@ -188,7 +190,8 @@ if start_detection:
188
  if 'video_capture' in locals() and video_capture.isOpened():
189
  video_capture.release()
190
  cv2.destroyAllWindows()
191
- pygame.mixer.quit()
 
192
 
193
  elif stop_detection:
194
  st.warning("Object detection stopped.")
 
9
  import threading
10
  from datetime import datetime, timedelta
11
 
12
+ # Check if running in a cloud environment (e.g., Hugging Face Spaces)
13
+ running_in_cloud = "HF_HOME" in os.environ
14
+
15
+ if running_in_cloud:
16
+ os.environ["SDL_AUDIODRIVER"] = "dummy" # Use the dummy audio driver
17
+ print("Running in a cloud environment: Audio alerts disabled.")
18
+ else:
19
+ # Initialize pygame mixer
20
+ pygame.mixer.quit() # Ensure the mixer is fully stopped
21
+ pygame.mixer.init()
22
 
23
  # Load YOLOv8 model
24
  yolo = YOLO("yolov8n.pt")
 
49
  unsafe_allow_html=True,
50
  )
51
 
 
 
 
 
 
 
 
 
 
52
  # Display welcome image
53
  welcome_image_path = "bismillah.png" # Ensure this image exists in the script's directory
54
  if os.path.exists(welcome_image_path):
 
73
  start_detection = st.button("Start Detection")
74
  with col2:
75
  stop_detection = st.button("Stop Detection")
76
+ audio_activation = st.checkbox("Enable Audio Alerts", value=False) if not running_in_cloud else False
77
 
78
  # Categories for audio alerts (hazardous objects or living things)
79
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
 
85
 
86
  def play_audio_alert(label, position):
87
  """Generate and play an audio alert."""
88
+ if running_in_cloud:
89
+ print(f"Audio alert triggered for {label} on {position}. (Audio playback disabled in cloud)")
90
+ return
91
+
92
  phrases = [
93
  f"Be careful, there's a {label} on your {position}.",
94
  f"Watch out! {label} detected on your {position}.",
 
190
  if 'video_capture' in locals() and video_capture.isOpened():
191
  video_capture.release()
192
  cv2.destroyAllWindows()
193
+ if not running_in_cloud:
194
+ pygame.mixer.quit()
195
 
196
  elif stop_detection:
197
  st.warning("Object detection stopped.")