Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
|
@@ -331,18 +333,51 @@ def process_video(
|
|
| 331 |
|
| 332 |
return annotator_frame
|
| 333 |
|
| 334 |
-
|
| 335 |
-
fd, output_path = tempfile.mkstemp(suffix=".mp4")
|
| 336 |
-
os.close(fd)
|
| 337 |
-
|
| 338 |
sv.process_video(
|
| 339 |
source_path=video_path,
|
| 340 |
target_path=output_path,
|
| 341 |
-
callback=callback
|
| 342 |
)
|
| 343 |
|
| 344 |
-
#
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
|
| 348 |
with gr.Blocks(title="Nhận dạng phương tiện giao thông", theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
import tempfile
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
|
|
|
| 333 |
|
| 334 |
return annotator_frame
|
| 335 |
|
| 336 |
+
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
|
|
|
|
|
|
|
|
|
| 337 |
sv.process_video(
|
| 338 |
source_path=video_path,
|
| 339 |
target_path=output_path,
|
| 340 |
+
callback=callback
|
| 341 |
)
|
| 342 |
|
| 343 |
+
# Re-encode sang H.264 để video dễ xem / tải về trên web
|
| 344 |
+
final_path = output_path.replace(".mp4", "_h264.mp4")
|
| 345 |
+
try:
|
| 346 |
+
import imageio_ffmpeg
|
| 347 |
+
|
| 348 |
+
ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe()
|
| 349 |
+
except ImportError:
|
| 350 |
+
ffmpeg_exe = shutil.which("ffmpeg")
|
| 351 |
+
|
| 352 |
+
if ffmpeg_exe:
|
| 353 |
+
subprocess.run(
|
| 354 |
+
[
|
| 355 |
+
ffmpeg_exe,
|
| 356 |
+
"-y",
|
| 357 |
+
"-i",
|
| 358 |
+
output_path,
|
| 359 |
+
"-c:v",
|
| 360 |
+
"libx264",
|
| 361 |
+
"-preset",
|
| 362 |
+
"ultrafast",
|
| 363 |
+
"-crf",
|
| 364 |
+
"23",
|
| 365 |
+
"-movflags",
|
| 366 |
+
"+faststart",
|
| 367 |
+
"-pix_fmt",
|
| 368 |
+
"yuv420p",
|
| 369 |
+
final_path,
|
| 370 |
+
],
|
| 371 |
+
stdout=subprocess.DEVNULL,
|
| 372 |
+
stderr=subprocess.DEVNULL,
|
| 373 |
+
)
|
| 374 |
+
try:
|
| 375 |
+
os.remove(output_path)
|
| 376 |
+
except OSError:
|
| 377 |
+
pass
|
| 378 |
+
output_path = final_path
|
| 379 |
+
|
| 380 |
+
return output_path
|
| 381 |
|
| 382 |
|
| 383 |
with gr.Blocks(title="Nhận dạng phương tiện giao thông", theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
|