Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,228 +6,118 @@ import time
|
|
6 |
import os
|
7 |
from pathlib import Path
|
8 |
|
9 |
-
# Create cache directory for models
|
10 |
os.makedirs("models", exist_ok=True)
|
11 |
|
12 |
-
# Check device availability - Hugging Face Spaces often provides GPU
|
13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
print(f"Using device: {device}")
|
15 |
|
16 |
-
# Load YOLOv5x model
|
17 |
model_path = Path("models/yolov5x.pt")
|
18 |
if model_path.exists():
|
19 |
print(f"Loading model from cache: {model_path}")
|
20 |
-
model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True,
|
21 |
-
source="local", path=str(model_path)).to(device)
|
22 |
else:
|
23 |
print("Downloading YOLOv5x model and caching...")
|
24 |
model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True).to(device)
|
25 |
-
# Cache the model for faster startup next time
|
26 |
torch.save(model.state_dict(), model_path)
|
27 |
|
28 |
-
#
|
29 |
-
model.conf = 0.3 # Confidence threshold
|
30 |
-
model.iou = 0.3 #
|
31 |
-
model.classes = None # Detect all
|
32 |
|
33 |
-
# Optimize for GPU if available
|
34 |
if device.type == "cuda":
|
35 |
-
# Use mixed precision for performance boost
|
36 |
model.half()
|
37 |
else:
|
38 |
-
# On CPU, optimize operations
|
39 |
torch.set_num_threads(os.cpu_count())
|
40 |
|
41 |
-
# Set model to evaluation mode for inference
|
42 |
model.eval()
|
43 |
|
44 |
-
|
45 |
-
np.random.
|
46 |
-
# Generate more attractive, vibrant colors
|
47 |
-
colors = []
|
48 |
-
for i in range(len(model.names)):
|
49 |
-
# Use HSV color space for more vibrant colors
|
50 |
-
hue = i / len(model.names)
|
51 |
-
# Full saturation and value for vivid colors
|
52 |
-
saturation = 0.9
|
53 |
-
value = 1.0
|
54 |
-
# Convert HSV to RGB
|
55 |
-
h = hue * 360
|
56 |
-
s = saturation
|
57 |
-
v = value
|
58 |
-
c = v * s
|
59 |
-
x = c * (1 - abs((h / 60) % 2 - 1))
|
60 |
-
m = v - c
|
61 |
-
|
62 |
-
if h < 60:
|
63 |
-
r, g, b = c, x, 0
|
64 |
-
elif h < 120:
|
65 |
-
r, g, b = x, c, 0
|
66 |
-
elif h < 180:
|
67 |
-
r, g, b = 0, c, x
|
68 |
-
elif h < 240:
|
69 |
-
r, g, b = 0, x, c
|
70 |
-
elif h < 300:
|
71 |
-
r, g, b = x, 0, c
|
72 |
-
else:
|
73 |
-
r, g, b = c, 0, x
|
74 |
-
|
75 |
-
r, g, b = (r + m) * 255, (g + m) * 255, (b + m) * 255
|
76 |
-
colors.append([int(b), int(g), int(r)]) # OpenCV uses BGR
|
77 |
|
78 |
-
# Track performance metrics
|
79 |
total_inference_time = 0
|
80 |
inference_count = 0
|
81 |
|
82 |
def detect_objects(image):
|
83 |
-
|
84 |
global total_inference_time, inference_count
|
85 |
|
86 |
if image is None:
|
87 |
return None
|
88 |
|
89 |
start_time = time.time()
|
90 |
-
|
91 |
-
# Create a copy for drawing results
|
92 |
output_image = image.copy()
|
93 |
-
|
94 |
-
# Fixed input size for optimal processing
|
95 |
input_size = 640
|
96 |
|
97 |
-
# Perform inference with no gradient calculation
|
98 |
with torch.no_grad():
|
99 |
-
# Convert image to tensor for faster processing
|
100 |
results = model(image, size=input_size)
|
101 |
|
102 |
-
# Record inference time (model processing only)
|
103 |
inference_time = time.time() - start_time
|
104 |
total_inference_time += inference_time
|
105 |
inference_count += 1
|
106 |
avg_inference_time = total_inference_time / inference_count
|
107 |
|
108 |
-
# Extract detections from first (and only) image
|
109 |
detections = results.pred[0].cpu().numpy()
|
110 |
|
111 |
for *xyxy, conf, cls in detections:
|
112 |
x1, y1, x2, y2 = map(int, xyxy)
|
113 |
class_id = int(cls)
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 3)
|
118 |
|
119 |
label = f"{model.names[class_id]} {conf:.2f}"
|
120 |
-
|
121 |
-
font_scale = 0.7
|
122 |
-
font_thickness = 2
|
123 |
-
|
124 |
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
|
125 |
|
126 |
-
|
127 |
-
overlay = output_image.copy()
|
128 |
-
cv2.rectangle(overlay, (x1, y1 - h - 10), (x1 + w + 10, y1), color, -1)
|
129 |
-
output_image = cv2.addWeighted(overlay, alpha, output_image, 1 - alpha, 0)
|
130 |
-
|
131 |
-
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
132 |
-
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), font_thickness + 1)
|
133 |
-
|
134 |
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
135 |
-
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness)
|
136 |
|
137 |
-
# Calculate FPS
|
138 |
fps = 1 / inference_time
|
139 |
|
140 |
-
|
141 |
-
h, w = output_image.shape[:2]
|
142 |
-
|
143 |
overlay = output_image.copy()
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
alpha = 0.8 - (i / fps_bg_height * 0.3)
|
151 |
-
color_value = int(220 * (1 - i / fps_bg_height))
|
152 |
-
cv2.rectangle(overlay,
|
153 |
-
(10, 10 + i),
|
154 |
-
(fps_bg_width, 10 + i),
|
155 |
-
(40, color_value, 40),
|
156 |
-
-1)
|
157 |
-
|
158 |
-
cv2.addWeighted(overlay, 0.8, output_image, 0.2, 0, output_image,
|
159 |
-
dst=output_image[10:10+fps_bg_height, 10:10+fps_bg_width])
|
160 |
-
|
161 |
-
cv2.rectangle(output_image,
|
162 |
-
(10, 10),
|
163 |
-
(fps_bg_width, 10 + fps_bg_height),
|
164 |
-
(255, 255, 255),
|
165 |
-
2,
|
166 |
-
cv2.LINE_AA)
|
167 |
-
|
168 |
-
cv2.putText(output_image, "Performance", (20, 35),
|
169 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
|
170 |
-
|
171 |
-
cv2.putText(output_image, f"Current: {fps:.1f} FPS", (20, 65),
|
172 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
|
173 |
-
|
174 |
-
cv2.putText(output_image, f"Average: {1/avg_inference_time:.1f} FPS", (20, 90),
|
175 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
|
176 |
|
177 |
return output_image
|
178 |
|
179 |
-
|
180 |
-
example_images = [
|
181 |
-
"spring_street_after.jpg",
|
182 |
-
"pexels-hikaique-109919.jpg"
|
183 |
-
]
|
184 |
-
|
185 |
-
# Make sure example directory exists
|
186 |
os.makedirs("examples", exist_ok=True)
|
187 |
|
188 |
-
# Create Gradio interface - optimized for Hugging Face Spaces
|
189 |
with gr.Blocks(title="Optimized YOLOv5 Object Detection") as demo:
|
190 |
gr.Markdown("""
|
191 |
# Optimized YOLOv5 Object Detection
|
192 |
-
|
193 |
-
This system utilizes YOLOv5 to detect 80+ object types from the COCO dataset.
|
194 |
-
|
195 |
-
**Performance Features:**
|
196 |
-
- Processing speed: Optimized for 30+ FPS at 640x640 resolution
|
197 |
-
- Confidence threshold: 0.3
|
198 |
-
- IoU threshold: 0.3
|
199 |
-
|
200 |
-
Upload an image, then click Submit to see the detections!
|
201 |
""")
|
202 |
|
203 |
with gr.Row():
|
204 |
with gr.Column(scale=1):
|
205 |
input_image = gr.Image(label="Input Image", type="numpy")
|
206 |
-
|
207 |
-
clear_button = gr.Button("Clear", size="sm")
|
208 |
-
submit_button = gr.Button("Submit", variant="primary", size="lg")
|
209 |
-
|
210 |
with gr.Column(scale=1):
|
211 |
output_image = gr.Image(label="Detected Objects", type="numpy")
|
212 |
|
|
|
|
|
|
|
|
|
213 |
gr.Examples(
|
214 |
examples=example_images,
|
215 |
inputs=input_image,
|
216 |
outputs=output_image,
|
217 |
fn=detect_objects,
|
218 |
-
cache_examples=True
|
219 |
)
|
220 |
|
221 |
submit_button.click(fn=detect_objects, inputs=input_image, outputs=output_image)
|
222 |
-
clear_button.click(
|
223 |
-
fn=lambda: (None, None),
|
224 |
-
outputs=[input_image, output_image],
|
225 |
-
queue=False
|
226 |
-
).then(
|
227 |
-
fn=detect_objects,
|
228 |
-
inputs=input_image,
|
229 |
-
outputs=output_image
|
230 |
-
)
|
231 |
|
232 |
-
|
233 |
-
demo.launch()
|
|
|
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 YOLOv5x model
|
16 |
model_path = Path("models/yolov5x.pt")
|
17 |
if model_path.exists():
|
18 |
print(f"Loading model from cache: {model_path}")
|
19 |
+
model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True, source="local", path=str(model_path)).to(device)
|
|
|
20 |
else:
|
21 |
print("Downloading YOLOv5x model and caching...")
|
22 |
model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True).to(device)
|
|
|
23 |
torch.save(model.state_dict(), model_path)
|
24 |
|
25 |
+
# Model configurations
|
26 |
+
model.conf = 0.3 # Confidence threshold
|
27 |
+
model.iou = 0.3 # IoU threshold
|
28 |
+
model.classes = None # Detect all classes
|
29 |
|
|
|
30 |
if device.type == "cuda":
|
|
|
31 |
model.half()
|
32 |
else:
|
|
|
33 |
torch.set_num_threads(os.cpu_count())
|
34 |
|
|
|
35 |
model.eval()
|
36 |
|
37 |
+
np.random.seed(42)
|
38 |
+
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
40 |
total_inference_time = 0
|
41 |
inference_count = 0
|
42 |
|
43 |
def detect_objects(image):
|
|
|
44 |
global total_inference_time, inference_count
|
45 |
|
46 |
if image is None:
|
47 |
return None
|
48 |
|
49 |
start_time = time.time()
|
|
|
|
|
50 |
output_image = image.copy()
|
|
|
|
|
51 |
input_size = 640
|
52 |
|
|
|
53 |
with torch.no_grad():
|
|
|
54 |
results = model(image, size=input_size)
|
55 |
|
|
|
56 |
inference_time = time.time() - start_time
|
57 |
total_inference_time += inference_time
|
58 |
inference_count += 1
|
59 |
avg_inference_time = total_inference_time / inference_count
|
60 |
|
|
|
61 |
detections = results.pred[0].cpu().numpy()
|
62 |
|
63 |
for *xyxy, conf, cls in detections:
|
64 |
x1, y1, x2, y2 = map(int, xyxy)
|
65 |
class_id = int(cls)
|
66 |
+
color = colors[class_id].tolist()
|
67 |
|
68 |
+
# Thinner, stylish bounding boxes
|
69 |
+
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2, lineType=cv2.LINE_AA)
|
|
|
70 |
|
71 |
label = f"{model.names[class_id]} {conf:.2f}"
|
72 |
+
font_scale, font_thickness = 0.7, 1
|
|
|
|
|
|
|
73 |
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
|
74 |
|
75 |
+
cv2.rectangle(output_image, (x1, y1 - h - 10), (x1 + w + 10, y1), color, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
77 |
+
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness, lineType=cv2.LINE_AA)
|
78 |
|
|
|
79 |
fps = 1 / inference_time
|
80 |
|
81 |
+
# Stylish FPS display
|
|
|
|
|
82 |
overlay = output_image.copy()
|
83 |
+
cv2.rectangle(overlay, (10, 10), (300, 80), (0, 0, 0), -1)
|
84 |
+
output_image = cv2.addWeighted(overlay, 0.6, output_image, 0.4, 0)
|
85 |
+
cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40),
|
86 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
87 |
+
cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (20, 70),
|
88 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
return output_image
|
91 |
|
92 |
+
example_images = ["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
os.makedirs("examples", exist_ok=True)
|
94 |
|
|
|
95 |
with gr.Blocks(title="Optimized YOLOv5 Object Detection") as demo:
|
96 |
gr.Markdown("""
|
97 |
# Optimized YOLOv5 Object Detection
|
98 |
+
Detects objects using YOLOv5 with enhanced visualization and FPS tracking.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
""")
|
100 |
|
101 |
with gr.Row():
|
102 |
with gr.Column(scale=1):
|
103 |
input_image = gr.Image(label="Input Image", type="numpy")
|
104 |
+
|
|
|
|
|
|
|
105 |
with gr.Column(scale=1):
|
106 |
output_image = gr.Image(label="Detected Objects", type="numpy")
|
107 |
|
108 |
+
with gr.Row():
|
109 |
+
clear_button = gr.Button("Clear")
|
110 |
+
submit_button = gr.Button("Submit", variant="primary")
|
111 |
+
|
112 |
gr.Examples(
|
113 |
examples=example_images,
|
114 |
inputs=input_image,
|
115 |
outputs=output_image,
|
116 |
fn=detect_objects,
|
117 |
+
cache_examples=True
|
118 |
)
|
119 |
|
120 |
submit_button.click(fn=detect_objects, inputs=input_image, outputs=output_image)
|
121 |
+
clear_button.click(lambda: (None, None), None, [input_image, output_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
demo.launch()
|
|