Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,79 +40,57 @@ np.random.seed(42)
|
|
40 |
colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
|
41 |
|
42 |
def process_video(video_path):
|
43 |
-
|
44 |
-
if video_path is None:
|
45 |
-
return None
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
if isinstance(video_path, tuple) and len(video_path) >= 1:
|
50 |
-
video_path = video_path[0]
|
51 |
-
# Or a dict with a 'name' key
|
52 |
-
elif isinstance(video_path, dict) and 'name' in video_path:
|
53 |
-
video_path = video_path['name']
|
54 |
-
# Make sure it's a string
|
55 |
-
video_path = str(video_path)
|
56 |
-
|
57 |
-
cap = cv2.VideoCapture(video_path)
|
58 |
-
|
59 |
-
if not cap.isOpened():
|
60 |
-
print(f"Error: Could not open video file at {video_path}")
|
61 |
-
return None
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
|
103 |
-
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
print(f"Video processed successfully, output at: {output_path}")
|
109 |
-
return output_path
|
110 |
|
111 |
-
|
112 |
-
print(f"Error processing video: {str(e)}")
|
113 |
-
import traceback
|
114 |
-
traceback.print_exc()
|
115 |
-
return None
|
116 |
|
117 |
def process_image(image):
|
118 |
img = np.array(image)
|
@@ -182,11 +160,9 @@ with gr.Blocks(css=css, title="Video & Image Object Detection by YOLOv5") as dem
|
|
182 |
with gr.Tabs():
|
183 |
with gr.TabItem("Video Detection", elem_classes="tab-item"):
|
184 |
with gr.Row():
|
185 |
-
# Keep using gr.Video but with source="upload" parameter
|
186 |
video_input = gr.Video(
|
187 |
label="Upload Video",
|
188 |
-
interactive=True,
|
189 |
-
source="upload", # Explicitly set upload as source
|
190 |
elem_id="video-input"
|
191 |
)
|
192 |
|
|
|
40 |
colors = np.random.randint(0, 255, size=(len(model.names), 3), dtype=np.uint8)
|
41 |
|
42 |
def process_video(video_path):
|
43 |
+
cap = cv2.VideoCapture(video_path)
|
|
|
|
|
44 |
|
45 |
+
if not cap.isOpened():
|
46 |
+
return "Error: Could not open video file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
frame_width = int(cap.get(3))
|
49 |
+
frame_height = int(cap.get(4))
|
50 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
51 |
+
|
52 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
53 |
+
output_path = "output_video.mp4"
|
54 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
55 |
|
56 |
+
total_frames = 0
|
57 |
+
total_time = 0
|
58 |
|
59 |
+
while cap.isOpened():
|
60 |
+
ret, frame = cap.read()
|
61 |
+
if not ret:
|
62 |
+
break
|
63 |
+
|
64 |
+
start_time = time.time()
|
65 |
+
|
66 |
+
# Convert frame for YOLOv5
|
67 |
+
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
68 |
+
results = model(img, size=640)
|
69 |
+
|
70 |
+
inference_time = time.time() - start_time
|
71 |
+
total_time += inference_time
|
72 |
+
total_frames += 1
|
73 |
+
|
74 |
+
detections = results.pred[0].cpu().numpy()
|
75 |
|
76 |
+
for *xyxy, conf, cls in detections:
|
77 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
78 |
+
class_id = int(cls)
|
79 |
+
color = colors[class_id].tolist()
|
80 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
81 |
+
label = f"{model.names[class_id]} {conf:.2f}"
|
82 |
+
cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
|
83 |
|
84 |
+
# Calculate FPS
|
85 |
+
avg_fps = total_frames / total_time if total_time > 0 else 0
|
86 |
+
cv2.putText(frame, f"FPS: {avg_fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
87 |
|
88 |
+
out.write(frame)
|
89 |
|
90 |
+
cap.release()
|
91 |
+
out.release()
|
|
|
|
|
|
|
92 |
|
93 |
+
return output_path
|
|
|
|
|
|
|
|
|
94 |
|
95 |
def process_image(image):
|
96 |
img = np.array(image)
|
|
|
160 |
with gr.Tabs():
|
161 |
with gr.TabItem("Video Detection", elem_classes="tab-item"):
|
162 |
with gr.Row():
|
|
|
163 |
video_input = gr.Video(
|
164 |
label="Upload Video",
|
165 |
+
interactive=True,
|
|
|
166 |
elem_id="video-input"
|
167 |
)
|
168 |
|