trannam1084 commited on
Commit
12446a4
·
verified ·
1 Parent(s): 0c8ff9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -340,40 +340,40 @@ def process_video(
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:
@@ -403,7 +403,7 @@ with gr.Blocks(title="Nhận dạng phương tiện giao thông", theme=gr.theme
403
  "### ✅ Kết quả đã xử lý\n"
404
  "Hiển thị và thống kê số lượng theo lớp."
405
  )
406
- video_output = gr.Video(label="Video đã xử lý")
407
 
408
  # Tùy chọn cấu hình
409
  with gr.Accordion("⚙️ Tùy chọn nâng cao", open=False):
 
340
  callback=callback,
341
  )
342
 
343
+ # (Tuỳ chọn) Re-encode sang H.264 để trình duyệt/Gradio đọc tốt hơn
344
+ final_path = output_path.replace(".mp4", "_h264.mp4")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  try:
346
+ import imageio_ffmpeg
347
+ ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe()
348
+ except ImportError:
349
+ ffmpeg_exe = shutil.which("ffmpeg")
350
+
351
+ if ffmpeg_exe:
352
+ subprocess.run(
353
+ [
354
+ ffmpeg_exe,
355
+ "-y",
356
+ "-i",
357
+ output_path,
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
 
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:
 
403
  "### ✅ Kết quả đã xử lý\n"
404
  "Hiển thị và thống kê số lượng theo lớp."
405
  )
406
+ video_output = gr.Video(label="Video đã xử lý", format="mp4")
407
 
408
  # Tùy chọn cấu hình
409
  with gr.Accordion("⚙️ Tùy chọn nâng cao", open=False):