Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -340,41 +340,40 @@ def process_video(
|
|
| 340 |
callback=callback,
|
| 341 |
)
|
| 342 |
|
| 343 |
-
#
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
os.remove(output_path)
|
| 375 |
-
|
|
|
|
| 376 |
|
| 377 |
-
return
|
| 378 |
|
| 379 |
|
| 380 |
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:
|
|
|
|
| 340 |
callback=callback,
|
| 341 |
)
|
| 342 |
|
| 343 |
+
# Ghi lại video bằng OpenCV để đảm bảo fps/duration hợp lệ (tránh NaN:NaN trên thanh thời gian)
|
| 344 |
+
cap = cv2.VideoCapture(output_path)
|
| 345 |
+
if not cap.isOpened():
|
| 346 |
+
# Nếu không mở được thì trả về file gốc (Gradio vẫn có thể play dạng blob)
|
| 347 |
+
return output_path
|
| 348 |
+
|
| 349 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 350 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 351 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 352 |
+
|
| 353 |
+
# Nếu fps không hợp lệ thì đặt mặc định
|
| 354 |
+
if fps is None or fps <= 0 or np.isnan(fps):
|
| 355 |
+
fps = 25.0
|
| 356 |
+
|
| 357 |
+
fixed_path = output_path.replace(".mp4", "_fixed.mp4")
|
| 358 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 359 |
+
writer = cv2.VideoWriter(fixed_path, fourcc, fps, (width, height))
|
| 360 |
+
|
| 361 |
+
while True:
|
| 362 |
+
ok, frame = cap.read()
|
| 363 |
+
if not ok:
|
| 364 |
+
break
|
| 365 |
+
writer.write(frame)
|
| 366 |
+
|
| 367 |
+
cap.release()
|
| 368 |
+
writer.release()
|
| 369 |
+
|
| 370 |
+
# Xoá file tạm gốc, dùng file đã fix
|
| 371 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
os.remove(output_path)
|
| 373 |
+
except OSError:
|
| 374 |
+
pass
|
| 375 |
|
| 376 |
+
return fixed_path
|
| 377 |
|
| 378 |
|
| 379 |
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:
|