Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,71 @@
|
|
|
|
1 |
import torch
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import os
|
7 |
-
from pathlib import Path
|
8 |
-
|
9 |
-
# Create cache directory for models
|
10 |
-
os.makedirs("models", exist_ok=True)
|
11 |
-
|
12 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
-
print(f"Using device: {device}")
|
14 |
-
|
15 |
-
# Load YOLOv5n model (corrected from original)
|
16 |
-
model_path = Path("models/yolov5n.pt")
|
17 |
-
if model_path.exists():
|
18 |
-
print(f"Loading model from cache: {model_path}")
|
19 |
-
model = torch.hub.load("ultralytics/yolov5", "yolov5n", pretrained=True,
|
20 |
-
source="local", path=str(model_path)).to(device)
|
21 |
-
else:
|
22 |
-
print("Downloading YOLOv5n model and caching...")
|
23 |
-
model = torch.hub.load("ultralytics/yolov5", "yolov5n", pretrained=True).to(device)
|
24 |
-
torch.save(model.state_dict(), model_path)
|
25 |
-
|
26 |
-
# Model configurations
|
27 |
-
model.conf = 0.6
|
28 |
-
model.iou = 0.45
|
29 |
-
model.classes = None
|
30 |
-
|
31 |
-
# Optimizations
|
32 |
-
if device.type == "cuda":
|
33 |
-
model.half()
|
34 |
-
torch.backends.cudnn.benchmark = True
|
35 |
-
else:
|
36 |
-
torch.set_num_threads(os.cpu_count())
|
37 |
|
38 |
-
model
|
39 |
-
|
40 |
-
|
41 |
-
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
42 |
-
|
43 |
-
total_inference_time = 0
|
44 |
-
inference_count = 0
|
45 |
|
46 |
def detect_objects(image):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return None
|
51 |
-
|
52 |
-
# Convert RGB to BGR for OpenCV operations
|
53 |
-
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
54 |
-
output_image = image_bgr.copy()
|
55 |
-
|
56 |
-
start_time = time.time()
|
57 |
-
|
58 |
-
# Convert to RGB for model inference
|
59 |
-
img_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
output_image_rgb = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
88 |
-
|
89 |
-
# Draw performance metrics
|
90 |
-
fps = 1 / inference_time
|
91 |
-
cv2.putText(output_image_rgb, f"FPS: {fps:.1f}", (10, 30),
|
92 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
93 |
-
cv2.putText(output_image_rgb, f"Avg FPS: {1/avg_inference_time:.1f}", (10, 60),
|
94 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
95 |
-
|
96 |
-
return output_image_rgb
|
97 |
|
98 |
-
#
|
99 |
-
|
100 |
-
os.makedirs("examples", exist_ok=True)
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
gr.Examples(examples=example_images, inputs=input_image)
|
121 |
-
with gr.Row():
|
122 |
-
submit_btn = gr.Button("Detect Objects", variant="primary")
|
123 |
-
clear_btn = gr.Button("Clear")
|
124 |
-
|
125 |
-
with gr.Column():
|
126 |
-
output_image = gr.Image(type="numpy", label="Processed Image")
|
127 |
-
|
128 |
-
submit_btn.click(fn=detect_objects, inputs=input_image, outputs=output_image)
|
129 |
-
clear_btn.click(lambda: (None, None), outputs=[input_image, output_image])
|
130 |
|
131 |
-
demo.launch()
|
|
|
1 |
+
import cv2
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
import gradio as gr
|
5 |
+
from ultralytics import YOLO
|
6 |
+
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Load YOLOv5 model (optimized for CUDA if available)
|
9 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
10 |
+
model = YOLO("yolov5s.pt").to(device)
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def detect_objects(image):
|
13 |
+
"""Detect objects in an uploaded image."""
|
14 |
+
results = model(image)
|
15 |
+
detections = results[0].boxes.data.cpu().numpy() # Get detections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
for box in detections:
|
18 |
+
x1, y1, x2, y2, conf, cls = map(int, box[:6])
|
19 |
+
label = f"{model.names[cls]} {conf:.2f}"
|
20 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
21 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
22 |
|
23 |
+
return image
|
24 |
+
|
25 |
+
# Real-time webcam processing
|
26 |
+
cap = cv2.VideoCapture(0) # Capture from webcam
|
27 |
+
frame = None
|
28 |
+
lock = threading.Lock()
|
29 |
+
|
30 |
+
def process_webcam():
|
31 |
+
global frame
|
32 |
+
while True:
|
33 |
+
ret, img = cap.read()
|
34 |
+
if not ret:
|
35 |
+
continue
|
36 |
|
37 |
+
results = model(img)
|
38 |
+
detections = results[0].boxes.data.cpu().numpy()
|
39 |
|
40 |
+
for box in detections:
|
41 |
+
x1, y1, x2, y2, conf, cls = map(int, box[:6])
|
42 |
+
label = f"{model.names[cls]} {conf:.2f}"
|
43 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
44 |
+
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
45 |
+
|
46 |
+
with lock:
|
47 |
+
frame = img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Start the webcam thread
|
50 |
+
threading.Thread(target=process_webcam, daemon=True).start()
|
|
|
51 |
|
52 |
+
def get_webcam_frame():
|
53 |
+
"""Returns the latest processed webcam frame."""
|
54 |
+
with lock:
|
55 |
+
return frame if frame is not None else np.zeros((480, 640, 3), dtype=np.uint8)
|
56 |
+
|
57 |
+
# Gradio UI
|
58 |
+
demo = gr.Blocks()
|
59 |
+
|
60 |
+
with demo:
|
61 |
+
gr.Markdown("# YOLOv5 Real-Time Object Detection")
|
62 |
+
with gr.Tabs():
|
63 |
+
with gr.Tab("Real-Time Webcam"):
|
64 |
+
gr.Video(get_webcam_frame, streaming=True)
|
65 |
+
with gr.Tab("Upload Image"):
|
66 |
+
image_input = gr.Image(type="numpy")
|
67 |
+
image_output = gr.Image()
|
68 |
+
image_button = gr.Button("Detect Objects")
|
69 |
+
image_button.click(detect_objects, inputs=image_input, outputs=image_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
demo.launch()
|