Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,6 @@ import time
|
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
from PIL import Image
|
| 9 |
-
from threading import Thread
|
| 10 |
-
from queue import Queue
|
| 11 |
|
| 12 |
# Create cache directory for models
|
| 13 |
os.makedirs("models", exist_ok=True)
|
|
@@ -26,9 +24,9 @@ else:
|
|
| 26 |
torch.save(model.state_dict(), model_path)
|
| 27 |
|
| 28 |
# Optimize model for speed
|
| 29 |
-
model.conf = 0.25 #
|
| 30 |
-
model.iou = 0.45
|
| 31 |
-
model.classes = None
|
| 32 |
model.max_det = 100 # Limit maximum detections
|
| 33 |
|
| 34 |
if device.type == "cuda":
|
|
@@ -42,27 +40,6 @@ model.eval()
|
|
| 42 |
np.random.seed(42)
|
| 43 |
colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
|
| 44 |
|
| 45 |
-
# Async video processing
|
| 46 |
-
def process_frame(model, frame_queue, result_queue):
|
| 47 |
-
while True:
|
| 48 |
-
if frame_queue.empty():
|
| 49 |
-
time.sleep(0.001)
|
| 50 |
-
continue
|
| 51 |
-
|
| 52 |
-
frame_data = frame_queue.get()
|
| 53 |
-
if frame_data is None: # Signal to stop
|
| 54 |
-
result_queue.put(None)
|
| 55 |
-
break
|
| 56 |
-
|
| 57 |
-
frame, frame_index = frame_data
|
| 58 |
-
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 59 |
-
|
| 60 |
-
# Use a smaller inference size for speed
|
| 61 |
-
results = model(img, size=384) # Reduced from 640 to 384
|
| 62 |
-
|
| 63 |
-
detections = results.xyxy[0].cpu().numpy()
|
| 64 |
-
result_queue.put((frame, detections, frame_index))
|
| 65 |
-
|
| 66 |
def process_video(video_path):
|
| 67 |
# Check if video_path is None or empty
|
| 68 |
if video_path is None or video_path == "":
|
|
@@ -81,120 +58,68 @@ def process_video(video_path):
|
|
| 81 |
frame_height = int(cap.get(4))
|
| 82 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
fourcc = cv2.VideoWriter_fourcc(*'
|
| 86 |
output_path = "output_video.mp4"
|
| 87 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
start_time = time.time()
|
| 100 |
-
processing_started = False
|
| 101 |
-
frames_buffer = {}
|
| 102 |
-
next_frame_to_write = 0
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
break
|
| 109 |
-
|
| 110 |
-
if not processing_started:
|
| 111 |
-
processing_started = True
|
| 112 |
-
start_time = time.time()
|
| 113 |
-
|
| 114 |
-
frame_queue.put((frame, total_frames))
|
| 115 |
-
total_frames += 1
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
cv2.rectangle(buffer_frame, (x1, y1), (x2, y2), color, 2, lineType=cv2.LINE_AA)
|
| 138 |
-
label = f"{model.names[class_id]} {conf:.2f}"
|
| 139 |
-
# Black text with white outline for better visibility
|
| 140 |
-
cv2.putText(buffer_frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX,
|
| 141 |
-
0.7, (0, 0, 0), 2, cv2.LINE_AA)
|
| 142 |
-
|
| 143 |
-
# Calculate elapsed time and FPS
|
| 144 |
-
elapsed = time.time() - start_time
|
| 145 |
-
current_fps = next_frame_to_write / elapsed if elapsed > 0 else 0
|
| 146 |
-
|
| 147 |
-
# Add FPS counter with black text
|
| 148 |
-
cv2.putText(buffer_frame, f"FPS: {current_fps:.2f}", (20, 40),
|
| 149 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
|
| 150 |
-
|
| 151 |
-
out.write(buffer_frame)
|
| 152 |
-
next_frame_to_write += 1
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if result is None:
|
| 165 |
-
break
|
| 166 |
-
|
| 167 |
-
processed_frame, detections, frame_idx = result
|
| 168 |
-
frames_buffer[frame_idx] = (processed_frame, detections)
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
for *xyxy, conf, cls in buffer_detections:
|
| 176 |
-
if conf < 0.35:
|
| 177 |
-
continue
|
| 178 |
-
x1, y1, x2, y2 = map(int, xyxy)
|
| 179 |
-
class_id = int(cls)
|
| 180 |
-
color = colors[class_id].tolist()
|
| 181 |
-
cv2.rectangle(buffer_frame, (x1, y1), (x2, y2), color, 2, lineType=cv2.LINE_AA)
|
| 182 |
-
label = f"{model.names[class_id]} {conf:.2f}"
|
| 183 |
-
cv2.putText(buffer_frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX,
|
| 184 |
-
0.7, (0, 0, 0), 2, cv2.LINE_AA)
|
| 185 |
-
|
| 186 |
-
# Add FPS counter
|
| 187 |
-
elapsed = time.time() - start_time
|
| 188 |
-
current_fps = next_frame_to_write / elapsed if elapsed > 0 else 0
|
| 189 |
-
cv2.putText(buffer_frame, f"FPS: {current_fps:.2f}", (20, 40),
|
| 190 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
|
| 191 |
-
|
| 192 |
-
out.write(buffer_frame)
|
| 193 |
-
next_frame_to_write += 1
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
out.release()
|
| 198 |
|
| 199 |
return output_path
|
| 200 |
|
|
|
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
from PIL import Image
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Create cache directory for models
|
| 11 |
os.makedirs("models", exist_ok=True)
|
|
|
|
| 24 |
torch.save(model.state_dict(), model_path)
|
| 25 |
|
| 26 |
# Optimize model for speed
|
| 27 |
+
model.conf = 0.25 # Lower confidence threshold for speed
|
| 28 |
+
model.iou = 0.45 # Better IoU threshold
|
| 29 |
+
model.classes = None
|
| 30 |
model.max_det = 100 # Limit maximum detections
|
| 31 |
|
| 32 |
if device.type == "cuda":
|
|
|
|
| 40 |
np.random.seed(42)
|
| 41 |
colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def process_video(video_path):
|
| 44 |
# Check if video_path is None or empty
|
| 45 |
if video_path is None or video_path == "":
|
|
|
|
| 58 |
frame_height = int(cap.get(4))
|
| 59 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 60 |
|
| 61 |
+
# Use mp4v codec which is more widely supported
|
| 62 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 63 |
output_path = "output_video.mp4"
|
| 64 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 65 |
|
| 66 |
+
# For FPS calculation
|
| 67 |
+
frame_count = 0
|
| 68 |
+
start_time = time.time()
|
| 69 |
|
| 70 |
+
# Skip frames for faster processing if needed
|
| 71 |
+
frame_skip = 0
|
| 72 |
+
if device.type != "cuda": # Skip more frames on CPU
|
| 73 |
+
frame_skip = 1
|
| 74 |
|
| 75 |
+
frame_idx = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
while cap.isOpened():
|
| 78 |
+
ret, frame = cap.read()
|
| 79 |
+
if not ret:
|
| 80 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
frame_idx += 1
|
| 83 |
+
if frame_skip > 0 and frame_idx % (frame_skip + 1) != 0:
|
| 84 |
+
out.write(frame) # Write original frame
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
# Convert frame for YOLOv5
|
| 88 |
+
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 89 |
+
|
| 90 |
+
# Use smaller inference size for speed
|
| 91 |
+
results = model(img, size=384) # Reduced from 640 to 384
|
| 92 |
+
|
| 93 |
+
detections = results.xyxy[0].cpu().numpy()
|
| 94 |
+
|
| 95 |
+
# Draw bounding boxes
|
| 96 |
+
for *xyxy, conf, cls in detections:
|
| 97 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
| 98 |
+
class_id = int(cls)
|
| 99 |
+
color = colors[class_id].tolist()
|
| 100 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2, lineType=cv2.LINE_AA)
|
| 101 |
+
label = f"{model.names[class_id]} {conf:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Black text
|
| 104 |
+
cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX,
|
| 105 |
+
0.7, (0, 0, 0), 2, cv2.LINE_AA)
|
| 106 |
|
| 107 |
+
# Update frame count for FPS calculation
|
| 108 |
+
frame_count += 1
|
| 109 |
+
|
| 110 |
+
# Calculate and display FPS every 10 frames
|
| 111 |
+
if frame_count % 10 == 0:
|
| 112 |
+
elapsed_time = time.time() - start_time
|
| 113 |
+
fps_calc = frame_count / elapsed_time if elapsed_time > 0 else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# Add FPS counter with black text
|
| 116 |
+
cv2.putText(frame, f"FPS: {fps_calc:.2f}", (20, 40),
|
| 117 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
|
| 118 |
+
|
| 119 |
+
out.write(frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
cap.release()
|
| 122 |
+
out.release()
|
|
|
|
| 123 |
|
| 124 |
return output_path
|
| 125 |
|