Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,40 +2,67 @@ import torch
|
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
# Device configuration
|
7 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
8 |
|
9 |
-
# Load YOLOv5s model
|
10 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
|
11 |
|
12 |
-
#
|
|
|
13 |
if device.type == 'cuda':
|
14 |
-
model.half()
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Return the first rendered image as numpy array
|
28 |
-
return np.array(rendered_images[0]) if rendered_images else image
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
+
import time
|
6 |
|
7 |
# Device configuration
|
8 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
|
10 |
+
# Load optimized YOLOv5s model
|
11 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
|
12 |
|
13 |
+
# Performance optimizations
|
14 |
+
model.conf = 0.5 # Confidence threshold (adjust for speed/accuracy balance)
|
15 |
if device.type == 'cuda':
|
16 |
+
model.half() # FP16 precision
|
17 |
|
18 |
+
def process_frame(image):
|
19 |
+
"""Process single frame with error handling"""
|
20 |
+
if image is None:
|
21 |
+
return None
|
22 |
|
23 |
+
try:
|
24 |
+
# Convert numpy array to PIL Image
|
25 |
+
image_pil = Image.fromarray(image)
|
26 |
+
|
27 |
+
# Perform inference
|
28 |
+
with torch.no_grad():
|
29 |
+
results = model(image_pil)
|
30 |
+
|
31 |
+
# Render results
|
32 |
+
rendered_images = results.render()
|
33 |
+
return np.array(rendered_images[0]) if rendered_images else image
|
34 |
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Processing error: {e}")
|
37 |
+
return image
|
|
|
|
|
38 |
|
39 |
+
with gr.Blocks(title="Real-Time Object Detection") as app:
|
40 |
+
gr.Markdown("# 🚀 Real-Time Object Detection with Dual Input")
|
41 |
+
gr.Markdown("Supports live webcam streaming and image uploads")
|
42 |
+
|
43 |
+
with gr.Tabs():
|
44 |
+
with gr.TabItem("📷 Live Camera"):
|
45 |
+
with gr.Row():
|
46 |
+
webcam_input = gr.Webcam(label="Live Feed", streaming=True)
|
47 |
+
live_output = gr.Image(label="Processed Feed", streaming=True)
|
48 |
+
webcam_input.change(process_frame, webcam_input, live_output)
|
49 |
+
|
50 |
+
with gr.TabItem("🖼️ Image Upload"):
|
51 |
+
with gr.Row():
|
52 |
+
upload_input = gr.Image(type="numpy", label="Upload Image")
|
53 |
+
upload_output = gr.Image(label="Detection Result")
|
54 |
+
upload_input.change(process_frame, upload_input, upload_output)
|
55 |
+
|
56 |
+
gr.Markdown("### ⚙️ Performance Settings")
|
57 |
+
with gr.Accordion("Advanced Settings", open=False):
|
58 |
+
gr.Slider(minimum=0.1, maximum=0.9, value=0.5,
|
59 |
+
label="Confidence Threshold", interactive=True)
|
60 |
+
gr.Checkbox(label="Enable FP16 Acceleration", value=True)
|
61 |
|
62 |
+
# Configure queue and launch
|
63 |
+
app.queue(concurrency_count=4, max_size=20).launch(
|
64 |
+
server_name="0.0.0.0",
|
65 |
+
server_port=7860,
|
66 |
+
share=False,
|
67 |
+
enable_queue=True
|
68 |
+
)
|