Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,9 @@ import os
|
|
| 2 |
import shutil
|
| 3 |
import subprocess
|
| 4 |
import tempfile
|
|
|
|
| 5 |
import cv2
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import gradio as gr
|
| 8 |
import supervision as sv
|
|
@@ -38,13 +40,19 @@ def process_video(
|
|
| 38 |
if isinstance(video_path, dict):
|
| 39 |
video_path = video_path.get("path", video_path)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
byte_tracker = sv.ByteTrack(
|
| 44 |
track_activation_threshold=0.25,
|
| 45 |
lost_track_buffer=30,
|
| 46 |
minimum_matching_threshold=0.8,
|
| 47 |
-
frame_rate=
|
| 48 |
minimum_consecutive_frames=3
|
| 49 |
)
|
| 50 |
byte_tracker.reset()
|
|
@@ -334,44 +342,23 @@ def process_video(
|
|
| 334 |
return annotator_frame
|
| 335 |
|
| 336 |
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 337 |
-
|
| 338 |
-
source_path=video_path,
|
| 339 |
-
target_path=output_path,
|
| 340 |
-
callback=callback,
|
| 341 |
-
)
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
"-c:v",
|
| 359 |
-
"libx264",
|
| 360 |
-
"-preset",
|
| 361 |
-
"ultrafast",
|
| 362 |
-
"-crf",
|
| 363 |
-
"23",
|
| 364 |
-
"-movflags",
|
| 365 |
-
"+faststart",
|
| 366 |
-
"-pix_fmt",
|
| 367 |
-
"yuv420p",
|
| 368 |
-
final_path,
|
| 369 |
-
],
|
| 370 |
-
stdout=subprocess.DEVNULL,
|
| 371 |
-
stderr=subprocess.DEVNULL,
|
| 372 |
-
)
|
| 373 |
-
os.remove(output_path)
|
| 374 |
-
output_path = final_path
|
| 375 |
|
| 376 |
return output_path
|
| 377 |
|
|
|
|
| 2 |
import shutil
|
| 3 |
import subprocess
|
| 4 |
import tempfile
|
| 5 |
+
|
| 6 |
import cv2
|
| 7 |
+
import imageio
|
| 8 |
import numpy as np
|
| 9 |
import gradio as gr
|
| 10 |
import supervision as sv
|
|
|
|
| 40 |
if isinstance(video_path, dict):
|
| 41 |
video_path = video_path.get("path", video_path)
|
| 42 |
|
| 43 |
+
cap = cv2.VideoCapture(video_path)
|
| 44 |
+
if not cap.isOpened():
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 48 |
+
if fps is None or fps <= 0 or np.isnan(fps):
|
| 49 |
+
fps = 30
|
| 50 |
|
| 51 |
byte_tracker = sv.ByteTrack(
|
| 52 |
track_activation_threshold=0.25,
|
| 53 |
lost_track_buffer=30,
|
| 54 |
minimum_matching_threshold=0.8,
|
| 55 |
+
frame_rate=fps,
|
| 56 |
minimum_consecutive_frames=3
|
| 57 |
)
|
| 58 |
byte_tracker.reset()
|
|
|
|
| 342 |
return annotator_frame
|
| 343 |
|
| 344 |
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 345 |
+
writer = imageio.get_writer(output_path, fps=fps)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
+
index = 0
|
| 348 |
+
while True:
|
| 349 |
+
ret, frame = cap.read()
|
| 350 |
+
if not ret:
|
| 351 |
+
break
|
| 352 |
+
|
| 353 |
+
annotated_frame = callback(frame, index)
|
| 354 |
+
index += 1
|
| 355 |
+
|
| 356 |
+
# imageio expects RGB frames
|
| 357 |
+
annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 358 |
+
writer.append_data(annotated_frame_rgb)
|
| 359 |
+
|
| 360 |
+
writer.close()
|
| 361 |
+
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
return output_path
|
| 364 |
|