practisebook commited on
Commit
509cf5b
·
verified ·
1 Parent(s): a225f19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -30
app.py CHANGED
@@ -1,28 +1,20 @@
1
  import cv2
2
  import random
3
- import time
4
  from ultralytics import YOLO
5
  from gtts import gTTS
6
- import pygame
7
- import threading
8
  from datetime import datetime, timedelta
9
  import gradio as gr
10
 
11
- # Initialize pygame mixer
12
- pygame.mixer.quit()
13
- pygame.mixer.init()
14
-
15
  # Load YOLOv8 model
16
  yolo = YOLO("yolov8n.pt")
17
 
18
  # Audio alert settings
19
- audio_temp_dir = "audio_temp_files"
20
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
21
  last_alert_time = {}
22
  alert_cooldown = timedelta(seconds=10)
23
 
24
- # Create audio alert
25
- def play_audio_alert(label, position):
26
  phrases = [
27
  f"Be careful, there's a {label} on your {position}.",
28
  f"Watch out! {label} detected on your {position}.",
@@ -30,22 +22,11 @@ def play_audio_alert(label, position):
30
  ]
31
  caution_note = random.choice(phrases)
32
 
33
- temp_file_path = f"{audio_temp_dir}/temp_{datetime.now().strftime('%Y%m%d_%H%M%S_%f')}.mp3"
 
34
  tts = gTTS(caution_note)
35
  tts.save(temp_file_path)
36
-
37
- try:
38
- pygame.mixer.music.load(temp_file_path)
39
- pygame.mixer.music.play()
40
-
41
- def cleanup_audio_file():
42
- while pygame.mixer.music.get_busy():
43
- time.sleep(0.1)
44
- pygame.mixer.music.stop()
45
-
46
- threading.Thread(target=cleanup_audio_file, daemon=True).start()
47
- except Exception as e:
48
- print(f"Error playing audio alert: {e}")
49
 
50
  # Process a single frame
51
  def process_frame(image, enable_audio):
@@ -53,6 +34,8 @@ def process_frame(image, enable_audio):
53
  result = results[0]
54
 
55
  detected_objects = {}
 
 
56
  for box in result.boxes:
57
  x1, y1, x2, y2 = map(int, box.xyxy[0])
58
  label = result.names[int(box.cls[0])]
@@ -69,14 +52,15 @@ def process_frame(image, enable_audio):
69
  label not in last_alert_time
70
  or current_time - last_alert_time[label] > alert_cooldown
71
  ):
72
- play_audio_alert(label, position)
 
73
  last_alert_time[label] = current_time
74
 
75
  # Draw bounding boxes
76
  cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
77
  cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
78
 
79
- return image
80
 
81
  # Gradio interface function
82
  def object_detection_webcam(enable_audio):
@@ -90,17 +74,21 @@ def object_detection_webcam(enable_audio):
90
  return "Error: Unable to read from camera."
91
 
92
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
93
- processed_frame = process_frame(frame, enable_audio)
94
- yield processed_frame
 
95
 
96
  cap.release()
97
 
98
- # Create Gradio interface
99
  def gradio_app():
100
  return gr.Interface(
101
  fn=object_detection_webcam,
102
  inputs=[gr.Checkbox(label="Enable Audio Alerts", value=False)],
103
- outputs="image",
 
 
 
104
  live=True,
105
  )
106
 
 
1
  import cv2
2
  import random
 
3
  from ultralytics import YOLO
4
  from gtts import gTTS
 
 
5
  from datetime import datetime, timedelta
6
  import gradio as gr
7
 
 
 
 
 
8
  # Load YOLOv8 model
9
  yolo = YOLO("yolov8n.pt")
10
 
11
  # Audio alert settings
 
12
  alert_categories = {"person", "cat", "dog", "knife", "fire", "gun"}
13
  last_alert_time = {}
14
  alert_cooldown = timedelta(seconds=10)
15
 
16
+ # Create audio alert as downloadable file
17
+ def generate_audio_alert(label, position):
18
  phrases = [
19
  f"Be careful, there's a {label} on your {position}.",
20
  f"Watch out! {label} detected on your {position}.",
 
22
  ]
23
  caution_note = random.choice(phrases)
24
 
25
+ # Save audio alert as an MP3 file
26
+ temp_file_path = f"audio_alert_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
27
  tts = gTTS(caution_note)
28
  tts.save(temp_file_path)
29
+ return temp_file_path
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Process a single frame
32
  def process_frame(image, enable_audio):
 
34
  result = results[0]
35
 
36
  detected_objects = {}
37
+ audio_files = []
38
+
39
  for box in result.boxes:
40
  x1, y1, x2, y2 = map(int, box.xyxy[0])
41
  label = result.names[int(box.cls[0])]
 
52
  label not in last_alert_time
53
  or current_time - last_alert_time[label] > alert_cooldown
54
  ):
55
+ audio_file = generate_audio_alert(label, position)
56
+ audio_files.append(audio_file)
57
  last_alert_time[label] = current_time
58
 
59
  # Draw bounding boxes
60
  cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
61
  cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
62
 
63
+ return image, audio_files
64
 
65
  # Gradio interface function
66
  def object_detection_webcam(enable_audio):
 
74
  return "Error: Unable to read from camera."
75
 
76
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
77
+ processed_frame, audio_files = process_frame(frame, enable_audio)
78
+
79
+ yield {"image": processed_frame, "audio": audio_files}
80
 
81
  cap.release()
82
 
83
+ # Gradio UI
84
  def gradio_app():
85
  return gr.Interface(
86
  fn=object_detection_webcam,
87
  inputs=[gr.Checkbox(label="Enable Audio Alerts", value=False)],
88
+ outputs=[
89
+ gr.Image(label="Processed Frame"),
90
+ gr.File(label="Audio Alerts"),
91
+ ],
92
  live=True,
93
  )
94