Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,11 @@ from pathlib import Path
|
|
9 |
# Create cache directory for models
|
10 |
os.makedirs("models", exist_ok=True)
|
11 |
|
12 |
-
# Select device
|
13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
print(f"Using device: {device}")
|
15 |
|
16 |
-
#
|
17 |
model_path = Path("models/yolov5n.pt")
|
18 |
-
|
19 |
if model_path.exists():
|
20 |
print(f"Loading model from cache: {model_path}")
|
21 |
model = torch.hub.load("ultralytics/yolov5", "custom", path=str(model_path), source="local").to(device)
|
@@ -25,112 +23,84 @@ else:
|
|
25 |
torch.save(model.state_dict(), model_path)
|
26 |
|
27 |
# Optimize model for speed
|
28 |
-
model.conf = 0.3 #
|
29 |
-
model.iou = 0.3 #
|
30 |
model.classes = None # Detect all classes
|
31 |
-
model.eval()
|
32 |
|
33 |
if device.type == "cuda":
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
|
39 |
# Pre-generate colors for bounding boxes
|
40 |
np.random.seed(42)
|
41 |
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
x1, y1, x2, y2 = map(int, xyxy)
|
75 |
-
class_id = int(cls)
|
76 |
-
color = colors[class_id].tolist()
|
77 |
-
|
78 |
-
# Keep bounding boxes within image bounds
|
79 |
-
x1, y1, x2, y2 = max(0, x1), max(0, y1), min(w, x2), min(h, y2)
|
80 |
-
|
81 |
-
# Draw bounding box
|
82 |
-
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
83 |
-
|
84 |
-
label = f"{model.names[class_id]} {conf:.2f}"
|
85 |
-
font_scale, font_thickness = 0.9, 2
|
86 |
-
(tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
|
87 |
-
|
88 |
-
# Label background
|
89 |
-
cv2.rectangle(output_image, (x1, y1 - th - 10), (x1 + tw + 10, y1), color, -1)
|
90 |
-
cv2.putText(output_image, label, (x1 + 5, y1 - 5),
|
91 |
-
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness, lineType=cv2.LINE_AA)
|
92 |
-
|
93 |
-
fps = 1 / inference_time
|
94 |
-
|
95 |
-
# Display FPS
|
96 |
-
overlay = output_image.copy()
|
97 |
-
cv2.rectangle(overlay, (10, 10), (300, 80), (0, 0, 0), -1)
|
98 |
-
output_image = cv2.addWeighted(overlay, 0.6, output_image, 0.4, 0)
|
99 |
-
cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40),
|
100 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
101 |
-
cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (20, 70),
|
102 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
103 |
-
|
104 |
-
return output_image
|
105 |
-
|
106 |
-
# Gradio UI
|
107 |
-
example_images = ["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
|
108 |
-
os.makedirs("examples", exist_ok=True)
|
109 |
|
110 |
-
|
111 |
-
gr.Markdown("""
|
112 |
-
# YOLOv5 Object Detection - High Quality & High FPS
|
113 |
-
Detects objects with full-resolution output and ultra-fast performance.
|
114 |
-
""")
|
115 |
|
116 |
-
|
117 |
-
with gr.Column(scale=1):
|
118 |
-
input_image = gr.Image(label="Input Image", type="numpy")
|
119 |
-
submit_button = gr.Button("Submit", variant="primary")
|
120 |
-
clear_button = gr.Button("Clear")
|
121 |
-
|
122 |
-
with gr.Column(scale=1):
|
123 |
-
output_image = gr.Image(label="Detected Objects", type="numpy")
|
124 |
-
|
125 |
-
gr.Examples(
|
126 |
-
examples=example_images,
|
127 |
-
inputs=input_image,
|
128 |
-
outputs=output_image,
|
129 |
-
fn=detect_objects,
|
130 |
-
cache_examples=True
|
131 |
-
)
|
132 |
-
|
133 |
-
submit_button.click(fn=detect_objects, inputs=input_image, outputs=output_image)
|
134 |
-
clear_button.click(lambda: (None, None), None, [input_image, output_image])
|
135 |
|
136 |
demo.launch()
|
|
|
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 YOLOv5 Nano model
|
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", "custom", path=str(model_path), source="local").to(device)
|
|
|
23 |
torch.save(model.state_dict(), model_path)
|
24 |
|
25 |
# Optimize model for speed
|
26 |
+
model.conf = 0.3 # Lower confidence threshold
|
27 |
+
model.iou = 0.3 # Non-Maximum Suppression IoU threshold
|
28 |
model.classes = None # Detect all classes
|
|
|
29 |
|
30 |
if device.type == "cuda":
|
31 |
+
model.half() # Use FP16 for faster inference
|
32 |
+
else:
|
33 |
+
torch.set_num_threads(os.cpu_count())
|
34 |
|
35 |
+
model.eval()
|
36 |
|
37 |
# Pre-generate colors for bounding boxes
|
38 |
np.random.seed(42)
|
39 |
colors = np.random.uniform(0, 255, size=(len(model.names), 3))
|
40 |
|
41 |
+
def process_video(video_path):
|
42 |
+
cap = cv2.VideoCapture(video_path)
|
43 |
+
|
44 |
+
if not cap.isOpened():
|
45 |
+
return "Error: Could not open video file."
|
46 |
+
|
47 |
+
frame_width = int(cap.get(3))
|
48 |
+
frame_height = int(cap.get(4))
|
49 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
50 |
+
|
51 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
52 |
+
output_path = "output_video.mp4"
|
53 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
54 |
+
|
55 |
+
total_frames = 0
|
56 |
+
total_time = 0
|
57 |
+
|
58 |
+
while cap.isOpened():
|
59 |
+
ret, frame = cap.read()
|
60 |
+
if not ret:
|
61 |
+
break # Break if no more frames
|
62 |
+
|
63 |
+
start_time = time.time()
|
64 |
+
|
65 |
+
# Convert frame for YOLOv5
|
66 |
+
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
67 |
+
results = model(img, size=640)
|
68 |
+
|
69 |
+
inference_time = time.time() - start_time
|
70 |
+
total_time += inference_time
|
71 |
+
total_frames += 1
|
72 |
+
|
73 |
+
detections = results.pred[0].cpu().numpy()
|
74 |
+
|
75 |
+
for *xyxy, conf, cls in detections:
|
76 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
77 |
+
class_id = int(cls)
|
78 |
+
color = colors[class_id].tolist()
|
79 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
80 |
+
label = f"{model.names[class_id]} {conf:.2f}"
|
81 |
+
cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
|
82 |
+
|
83 |
+
# Calculate FPS
|
84 |
+
avg_fps = total_frames / total_time if total_time > 0 else 0
|
85 |
+
cv2.putText(frame, f"FPS: {avg_fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
86 |
+
|
87 |
+
out.write(frame)
|
88 |
+
|
89 |
+
cap.release()
|
90 |
+
out.release()
|
91 |
+
|
92 |
+
return output_path
|
93 |
+
|
94 |
+
# Gradio Interface
|
95 |
+
with gr.Blocks(title="Real-Time YOLOv5 Video Detection") as demo:
|
96 |
+
gr.Markdown("# Real-Time YOLOv5 Video Detection (30+ FPS)")
|
97 |
|
98 |
+
with gr.Row():
|
99 |
+
video_input = gr.Video(label="Upload Video")
|
100 |
+
process_button = gr.Button("Process Video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
video_output = gr.Video(label="Processed Video")
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
process_button.click(fn=process_video, inputs=video_input, outputs=video_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
demo.launch()
|