practisebook commited on
Commit
a225f19
·
verified ·
1 Parent(s): 55d719d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -26
app.py CHANGED
@@ -1,36 +1,107 @@
1
- import os
2
- import gradio as gr
3
- from ultralytics import YOLO
4
  import cv2
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Load YOLOv8 model
7
- model = YOLO("yolov8n.pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def detect_objects(video):
10
- cap = cv2.VideoCapture(video)
11
- frames = []
12
- while cap.isOpened():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  ret, frame = cap.read()
14
  if not ret:
15
- break
16
- results = model(frame)
17
- annotated_frame = results[0].plot()
18
- _, buffer = cv2.imencode('.jpg', annotated_frame)
19
- frames.append(buffer.tobytes())
20
- cap.release()
21
- return frames
22
 
23
- # Create Gradio interface
24
- with gr.Blocks() as demo:
25
- gr.Markdown("# Real-Time Object Detection for Blind Assistance")
26
- gr.Markdown("This app detects objects in real-time using your webcam.")
27
 
28
- # Remove the `source` argument
29
- video_input = gr.Video(label="Webcam Stream", type="filepath")
30
- output_gallery = gr.Video(label="Detection Output")
31
 
32
- detect_button = gr.Button("Start Detection")
33
- detect_button.click(detect_objects, inputs=[video_input], outputs=[output_gallery])
 
 
 
 
 
 
34
 
35
- # Launch the app
36
- demo.launch()
 
 
 
 
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}.",
29
+ f"Alert! A {label} is on your {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):
52
+ results = yolo(image)
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])]
59
+
60
+ if enable_audio and label in alert_categories:
61
+ frame_center_x = image.shape[1] // 2
62
+ obj_center_x = (x1 + x2) // 2
63
+ position = "left" if obj_center_x < frame_center_x else "right"
64
+
65
+ detected_objects[label] = position
66
+
67
+ current_time = datetime.now()
68
+ if (
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):
83
+ cap = cv2.VideoCapture(0)
84
+ if not cap.isOpened():
85
+ return "Error: Unable to access the camera."
86
+
87
+ while True:
88
  ret, frame = cap.read()
89
  if not ret:
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
 
107
+ gradio_app().launch()