Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,28 @@ 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 |
-
|
21 |
-
|
|
|
|
|
22 |
|
23 |
return image
|
24 |
|
@@ -28,6 +35,7 @@ frame = None
|
|
28 |
lock = threading.Lock()
|
29 |
|
30 |
def process_webcam():
|
|
|
31 |
global frame
|
32 |
while True:
|
33 |
ret, img = cap.read()
|
@@ -40,8 +48,10 @@ def process_webcam():
|
|
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 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
with lock:
|
47 |
frame = img
|
@@ -55,16 +65,22 @@ def get_webcam_frame():
|
|
55 |
return frame if frame is not None else np.zeros((480, 640, 3), dtype=np.uint8)
|
56 |
|
57 |
# Gradio UI
|
58 |
-
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
4 |
import gradio as gr
|
5 |
from ultralytics import YOLO
|
6 |
import threading
|
7 |
+
import time
|
8 |
|
9 |
# Load YOLOv5 model (optimized for CUDA if available)
|
10 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
11 |
model = YOLO("yolov5s.pt").to(device)
|
12 |
|
13 |
+
# Generate unique colors for each class
|
14 |
+
num_classes = len(model.names)
|
15 |
+
colors = np.random.randint(0, 255, size=(num_classes, 3), dtype=np.uint8)
|
16 |
+
|
17 |
def detect_objects(image):
|
18 |
+
"""Detect objects in an uploaded image with different bounding box colors."""
|
19 |
results = model(image)
|
20 |
detections = results[0].boxes.data.cpu().numpy() # Get detections
|
21 |
|
22 |
for box in detections:
|
23 |
x1, y1, x2, y2, conf, cls = map(int, box[:6])
|
24 |
label = f"{model.names[cls]} {conf:.2f}"
|
25 |
+
color = tuple(map(int, colors[cls])) # Assign unique color based on class
|
26 |
+
|
27 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
28 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
29 |
|
30 |
return image
|
31 |
|
|
|
35 |
lock = threading.Lock()
|
36 |
|
37 |
def process_webcam():
|
38 |
+
"""Continuously capture and process frames from the webcam."""
|
39 |
global frame
|
40 |
while True:
|
41 |
ret, img = cap.read()
|
|
|
48 |
for box in detections:
|
49 |
x1, y1, x2, y2, conf, cls = map(int, box[:6])
|
50 |
label = f"{model.names[cls]} {conf:.2f}"
|
51 |
+
color = tuple(map(int, colors[cls])) # Assign unique color
|
52 |
+
|
53 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
54 |
+
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
55 |
|
56 |
with lock:
|
57 |
frame = img
|
|
|
65 |
return frame if frame is not None else np.zeros((480, 640, 3), dtype=np.uint8)
|
66 |
|
67 |
# Gradio UI
|
68 |
+
with gr.Blocks() as demo:
|
|
|
|
|
69 |
gr.Markdown("# YOLOv5 Real-Time Object Detection")
|
70 |
+
|
71 |
with gr.Tabs():
|
72 |
with gr.Tab("Real-Time Webcam"):
|
73 |
+
webcam_output = gr.Image(label="Live Webcam Feed")
|
74 |
+
def update_webcam():
|
75 |
+
while True:
|
76 |
+
webcam_output.update(get_webcam_frame())
|
77 |
+
time.sleep(1/30) # ~30 FPS
|
78 |
+
|
79 |
+
threading.Thread(target=update_webcam, daemon=True).start()
|
80 |
+
|
81 |
with gr.Tab("Upload Image"):
|
82 |
+
image_input = gr.Image(type="numpy", label="Upload Image")
|
83 |
+
image_output = gr.Image(label="Detected Objects")
|
84 |
image_button = gr.Button("Detect Objects")
|
85 |
image_button.click(detect_objects, inputs=image_input, outputs=image_output)
|
86 |
|