practisebook commited on
Commit
3ee5b52
·
verified ·
1 Parent(s): ddbccfe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ from gtts import gTTS
5
+ import numpy as np
6
+ import tempfile
7
+ import os
8
+
9
+ # Load YOLOv8 model
10
+ model = YOLO("yolov8n.pt") # Make sure the YOLOv8 model file is in the same directory
11
+
12
+ # Function to process the video frame and detect objects
13
+ def detect_objects(image):
14
+ # Perform object detection
15
+ results = model(image)
16
+ annotated_frame = results[0].plot() # Annotate the frame with bounding boxes
17
+
18
+ # Extract detected object labels
19
+ detected_objects = [model.names[int(box.cls)] for box in results[0].boxes]
20
+ if detected_objects:
21
+ objects_text = ", ".join(set(detected_objects))
22
+ # Generate audio alert for detected objects
23
+ tts = gTTS(f"Detected: {objects_text}", lang="en")
24
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
25
+ tts.save(temp_file.name)
26
+ return annotated_frame, temp_file.name
27
+ return annotated_frame, None
28
+
29
+ # Gradio Interface
30
+ def process_frame(image):
31
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
32
+ annotated_frame, audio_file = detect_objects(image)
33
+ if audio_file:
34
+ return annotated_frame, audio_file
35
+ else:
36
+ return annotated_frame, None
37
+
38
+ # Gradio interface for real-time webcam feed
39
+ webcam = gr.Interface(
40
+ fn=process_frame,
41
+ inputs=gr.Image(source="webcam", tool="editor", type="numpy"),
42
+ outputs=[
43
+ gr.Image(label="Detected Objects"),
44
+ gr.Audio(label="Audio Alert (if any)")
45
+ ],
46
+ live=True, # Enable live streaming from webcam
47
+ )
48
+
49
+ # Launch Gradio App
50
+ webcam.launch()