kr4phy commited on
Commit
3e26e16
·
1 Parent(s): 48e20c0

비디오 처리 기능 개선 및 코덱 호환성 향상

Browse files
Files changed (2) hide show
  1. app.py +34 -16
  2. lane_detection.py +23 -3
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import tempfile
 
3
  from lane_detection import process_video as process_video_file
4
 
5
 
@@ -11,22 +12,36 @@ def process_video(video_path, method, use_enhanced, use_segmented, progress=gr.P
11
  if video_path is None:
12
  return None
13
 
14
- # Create temporary output file
15
- temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
16
- output_path = temp_output.name
17
- temp_output.close()
18
-
19
- # Progress callback function
20
- def update_progress(value, desc):
21
- progress(value, desc=desc)
22
-
23
- # Process the video with selected method and progress callback
24
- success = process_video_file(video_path, output_path, method, use_enhanced, use_segmented, progress_callback=update_progress)
25
-
26
- if success:
27
- return output_path
28
- else:
 
 
 
 
 
 
29
  return None
 
 
 
 
 
 
 
 
30
 
31
 
32
  # Create Gradio interface
@@ -70,7 +85,10 @@ with gr.Blocks(title="Lane Detection Demo") as demo:
70
  process_btn = gr.Button("Process Video", variant="primary")
71
 
72
  with gr.Column():
73
- video_output = gr.Video(label="Result (Original | Lane Detection)")
 
 
 
74
 
75
  # Update checkbox visibility based on method
76
  def update_checkboxes(method):
 
1
  import gradio as gr
2
  import tempfile
3
+ import os
4
  from lane_detection import process_video as process_video_file
5
 
6
 
 
12
  if video_path is None:
13
  return None
14
 
15
+ # Create temporary output file with explicit .mp4 extension
16
+ temp_dir = tempfile.gettempdir()
17
+ temp_filename = f"lane_detection_{os.urandom(8).hex()}.mp4"
18
+ output_path = os.path.join(temp_dir, temp_filename)
19
+
20
+ try:
21
+ # Progress callback function
22
+ def update_progress(value, desc):
23
+ progress(value, desc=desc)
24
+
25
+ # Process the video with selected method and progress callback
26
+ success = process_video_file(video_path, output_path, method, use_enhanced, use_segmented, progress_callback=update_progress)
27
+
28
+ if success and os.path.exists(output_path):
29
+ return output_path
30
+ else:
31
+ return None
32
+ except Exception as e:
33
+ print(f"Error processing video: {e}")
34
+ import traceback
35
+ traceback.print_exc()
36
  return None
37
+ finally:
38
+ # Clean up input if it was a temporary file
39
+ if video_path and video_path.startswith(temp_dir):
40
+ try:
41
+ if os.path.exists(video_path):
42
+ os.remove(video_path)
43
+ except:
44
+ pass
45
 
46
 
47
  # Create Gradio interface
 
85
  process_btn = gr.Button("Process Video", variant="primary")
86
 
87
  with gr.Column():
88
+ video_output = gr.Video(
89
+ label="Result (Original | Lane Detection)",
90
+ format="mp4" # Explicitly set format for better browser compatibility
91
+ )
92
 
93
  # Update checkbox visibility based on method
94
  def update_checkboxes(method):
lane_detection.py CHANGED
@@ -944,9 +944,29 @@ def process_video(input_path, output_path, method="advanced", use_enhanced=True,
944
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
945
 
946
  # Video writer for output (side-by-side, so width is doubled)
947
- # Use mp4v codec for better compatibility
948
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
949
- out = cv2.VideoWriter(output_path, fourcc, fps, (width * 2, height))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950
 
951
  frame_count = 0
952
  print(f"Processing {total_frames} frames using {method} method...")
 
944
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
945
 
946
  # Video writer for output (side-by-side, so width is doubled)
947
+ # Try different codecs for better browser compatibility
948
+ codecs_to_try = [
949
+ ('H264', cv2.VideoWriter_fourcc(*'H264')), # H.264 - most compatible
950
+ ('h264', cv2.VideoWriter_fourcc(*'h264')), # Alternative H.264
951
+ ('mp4v', cv2.VideoWriter_fourcc(*'mp4v')), # MPEG-4 Part 2
952
+ ('XVID', cv2.VideoWriter_fourcc(*'XVID')), # XVID
953
+ ]
954
+
955
+ out = None
956
+ selected_codec = None
957
+
958
+ for codec_name, fourcc in codecs_to_try:
959
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width * 2, height))
960
+ if out.isOpened():
961
+ selected_codec = codec_name
962
+ print(f"✓ Using codec: {codec_name}")
963
+ break
964
+ else:
965
+ print(f"⚠ Codec {codec_name} not available, trying next...")
966
+
967
+ if out is None or not out.isOpened():
968
+ print("✗ Error: No suitable video codec found!")
969
+ return False
970
 
971
  frame_count = 0
972
  print(f"Processing {total_frames} frames using {method} method...")