Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,106 +3,64 @@ import numpy as np
|
|
| 3 |
import gradio as gr
|
| 4 |
import cv2
|
| 5 |
import time
|
| 6 |
-
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
print(f"Loading model from cache: {model_path}")
|
| 19 |
-
model = torch.hub.load("ultralytics/yolov5", "custom", path=str(model_path), source="local").to(device)
|
| 20 |
-
else:
|
| 21 |
-
print("Downloading YOLOv5n model and caching...")
|
| 22 |
-
model = torch.hub.load("ultralytics/yolov5", "yolov5n", pretrained=True).to(device)
|
| 23 |
-
torch.save(model.state_dict(), model_path)
|
| 24 |
-
|
| 25 |
-
# Configure model
|
| 26 |
-
model.conf = 0.5
|
| 27 |
-
model.iou = 0.5
|
| 28 |
-
model.classes = None
|
| 29 |
if device.type == "cuda":
|
| 30 |
-
model.half()
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
model.eval()
|
| 34 |
-
|
| 35 |
-
# Generate colors for bounding boxes
|
| 36 |
-
np.random.seed(42)
|
| 37 |
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
| 38 |
-
|
| 39 |
def detect_objects(image):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
output_image = image.copy()
|
| 44 |
-
|
|
|
|
| 45 |
detections = results.pred[0].cpu().numpy()
|
| 46 |
-
|
| 47 |
for *xyxy, conf, cls in detections:
|
| 48 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 49 |
class_id = int(cls)
|
|
|
|
|
|
|
| 50 |
color = colors[class_id].tolist()
|
| 51 |
-
cv2.rectangle(output_image, (x1, y1), (x2, y2), color,
|
| 52 |
-
label = f"{model.names[class_id]} {conf:.2f}"
|
| 53 |
-
cv2.putText(output_image, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
|
| 54 |
-
|
| 55 |
-
return output_image
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
if not cap.isOpened():
|
| 60 |
-
return "Error: Could not open video file."
|
| 61 |
-
|
| 62 |
-
frame_width = int(cap.get(3))
|
| 63 |
-
frame_height = int(cap.get(4))
|
| 64 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 65 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 66 |
-
output_path = "output_video.mp4"
|
| 67 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 68 |
-
|
| 69 |
-
while cap.isOpened():
|
| 70 |
-
ret, frame = cap.read()
|
| 71 |
-
if not ret:
|
| 72 |
-
break
|
| 73 |
-
|
| 74 |
-
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 75 |
-
results = model(img, size=640)
|
| 76 |
-
detections = results.pred[0].cpu().numpy()
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
color = colors[class_id].tolist()
|
| 82 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
| 83 |
-
label = f"{model.names[class_id]} {conf:.2f}"
|
| 84 |
-
cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
|
| 85 |
-
|
| 86 |
-
out.write(frame)
|
| 87 |
-
|
| 88 |
-
cap.release()
|
| 89 |
-
out.release()
|
| 90 |
-
return output_path
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import cv2
|
| 5 |
import time
|
| 6 |
+
# Check device availability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
# Load smaller YOLOv5 model
|
| 9 |
+
model = torch.hub.load("ultralytics/yolov5", "yolov5x", pretrained=True).to(device)
|
| 10 |
+
# Optimization configurations
|
| 11 |
+
model.conf = 0.3 # Confidence threshold
|
| 12 |
+
model.iou = 0.3 # NMS IoU threshold
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if device.type == "cuda":
|
| 14 |
+
model.half().to(device) # Use FP16 for performance boost
|
| 15 |
+
model.eval() # Set model to evaluation mode
|
| 16 |
+
# Assign fixed colors to each class for bounding boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
|
|
|
| 18 |
def detect_objects(image):
|
| 19 |
+
start_time = time.time()
|
| 20 |
+
|
| 21 |
+
# Convert BGR to RGB (if needed, Gradio might already provide RGB)
|
| 22 |
+
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 23 |
+
|
| 24 |
+
# Perform inference
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
results = model(image, size=640) # Fixed inference size
|
| 27 |
+
|
| 28 |
+
# Process results directly on numpy array
|
| 29 |
output_image = image.copy()
|
| 30 |
+
|
| 31 |
+
# Extract detections
|
| 32 |
detections = results.pred[0].cpu().numpy()
|
| 33 |
+
|
| 34 |
for *xyxy, conf, cls in detections:
|
| 35 |
x1, y1, x2, y2 = map(int, xyxy)
|
| 36 |
class_id = int(cls)
|
| 37 |
+
|
| 38 |
+
# Draw bounding box
|
| 39 |
color = colors[class_id].tolist()
|
| 40 |
+
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Create label
|
| 43 |
+
label = f"{model.names[class_id]} {conf:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Draw label background
|
| 46 |
+
(w, h), * = cv2.getTextSize(label, cv2.FONT*HERSHEY_SIMPLEX, 0.5, 1)
|
| 47 |
+
cv2.rectangle(output_image, (x1, y1 - 20), (x1 + w, y1), color, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Draw label text
|
| 50 |
+
cv2.putText(output_image, label, (x1, y1 - 5),
|
| 51 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
|
| 52 |
+
# Calculate FPS
|
| 53 |
+
fps = 1 / (time.time() - start_time)
|
| 54 |
+
print(f"FPS: {fps:.2f}")
|
| 55 |
+
return output_image
|
| 56 |
+
# Gradio interface
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=detect_objects,
|
| 59 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 60 |
+
outputs=gr.Image(type="numpy", label="Detected Objects"),
|
| 61 |
+
title="Optimized Object Detection with YOLOv5",
|
| 62 |
+
description="Faster detection using YOLOv5s with FP16 and optimized processing",
|
| 63 |
+
allow_flagging="never",
|
| 64 |
+
examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"],
|
| 65 |
+
)
|
| 66 |
+
iface.launch()
|