SpyC0der77 commited on
Commit
1314b4f
·
verified ·
1 Parent(s): 3919f07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -27,10 +27,12 @@ except Exception as e:
27
  print("[INFO] Falling back to OpenCV Farneback optical flow.")
28
  raft_model = None
29
 
30
- def generate_motion_csv(video_file, output_csv=None):
31
  """
32
  Generates a CSV file with motion data (columns: frame, mag, ang, zoom) from an input video.
33
  Uses RAFT if available, otherwise falls back to OpenCV's Farneback optical flow.
 
 
34
  """
35
  start_time = time.time()
36
  if output_csv is None:
@@ -107,6 +109,9 @@ def generate_motion_csv(video_file, output_csv=None):
107
 
108
  if frame_idx % 10 == 0 or frame_idx == total_frames:
109
  print(f"[INFO] Processed frame {frame_idx}/{total_frames}")
 
 
 
110
  frame_idx += 1
111
 
112
  cap.release()
@@ -141,10 +146,12 @@ def read_motion_csv(csv_filename):
141
  print("[INFO] Completed reading motion CSV.")
142
  return motion_data
143
 
144
- def stabilize_video_using_csv(video_file, csv_file, zoom=1.0, vertical_only=False, output_file=None):
145
  """
146
  Stabilizes the input video using motion data from the CSV file.
147
  If vertical_only is True, only vertical motion is corrected (horizontal displacement is ignored).
 
 
148
  """
149
  start_time = time.time()
150
  print(f"[INFO] Starting stabilization using CSV: {csv_file}")
@@ -185,8 +192,7 @@ def stabilize_video_using_csv(video_file, csv_file, zoom=1.0, vertical_only=Fals
185
 
186
  dx, dy = motion_data.get(frame_idx, (0, 0))
187
  if vertical_only:
188
- # If only vertical stabilization is desired, ignore horizontal motion.
189
- dx = 0
190
  transform = np.array([[1, 0, dx],
191
  [0, 1, dy]], dtype=np.float32)
192
  stabilized_frame = cv2.warpAffine(frame, transform, (width, height))
@@ -194,6 +200,9 @@ def stabilize_video_using_csv(video_file, csv_file, zoom=1.0, vertical_only=Fals
194
  out.write(stabilized_frame)
195
  if frame_idx % 10 == 0 or frame_idx == total_frames:
196
  print(f"[INFO] Stabilized frame {frame_idx}/{total_frames}")
 
 
 
197
  frame_idx += 1
198
 
199
  cap.release()
@@ -202,7 +211,7 @@ def stabilize_video_using_csv(video_file, csv_file, zoom=1.0, vertical_only=Fals
202
  print(f"[INFO] Stabilized video saved to: {output_file} in {elapsed:.2f} seconds")
203
  return output_file
204
 
205
- def process_video_ai(video_file, zoom, vertical_only):
206
  """
207
  Gradio interface function:
208
  - Generates motion data (CSV) from the input video using an AI model (RAFT if available, else Farneback).
@@ -220,8 +229,11 @@ def process_video_ai(video_file, zoom, vertical_only):
220
  raise ValueError("[ERROR] Please upload a video file.")
221
 
222
  print("[INFO] Starting AI-powered video processing...")
223
- csv_file = generate_motion_csv(video_file)
224
- stabilized_path = stabilize_video_using_csv(video_file, csv_file, zoom=zoom, vertical_only=vertical_only)
 
 
 
225
  print("[INFO] Video processing complete.")
226
  logs = log_buffer.getvalue()
227
  return video_file, stabilized_path, logs
@@ -229,7 +241,7 @@ def process_video_ai(video_file, zoom, vertical_only):
229
  # Build the Gradio UI.
230
  with gr.Blocks() as demo:
231
  gr.Markdown("# AI-Powered Video Stabilization")
232
- gr.Markdown("Upload a video, select a zoom factor, and choose whether to apply only vertical stabilization. The system will generate motion data using an AI model (RAFT if available) and then stabilize the video accordingly.")
233
 
234
  with gr.Row():
235
  with gr.Column():
 
27
  print("[INFO] Falling back to OpenCV Farneback optical flow.")
28
  raft_model = None
29
 
30
+ def generate_motion_csv(video_file, output_csv=None, progress=gr.Progress(), progress_offset=0.0, progress_scale=0.5):
31
  """
32
  Generates a CSV file with motion data (columns: frame, mag, ang, zoom) from an input video.
33
  Uses RAFT if available, otherwise falls back to OpenCV's Farneback optical flow.
34
+
35
+ The progress bar is updated from progress_offset to progress_offset+progress_scale.
36
  """
37
  start_time = time.time()
38
  if output_csv is None:
 
109
 
110
  if frame_idx % 10 == 0 or frame_idx == total_frames:
111
  print(f"[INFO] Processed frame {frame_idx}/{total_frames}")
112
+
113
+ # Update progress for this phase.
114
+ progress(progress_offset + (frame_idx / total_frames) * progress_scale, desc="Generating Motion CSV")
115
  frame_idx += 1
116
 
117
  cap.release()
 
146
  print("[INFO] Completed reading motion CSV.")
147
  return motion_data
148
 
149
+ def stabilize_video_using_csv(video_file, csv_file, zoom=1.0, vertical_only=False, progress=gr.Progress(), progress_offset=0.5, progress_scale=0.5, output_file=None):
150
  """
151
  Stabilizes the input video using motion data from the CSV file.
152
  If vertical_only is True, only vertical motion is corrected (horizontal displacement is ignored).
153
+
154
+ The progress bar is updated from progress_offset to progress_offset+progress_scale.
155
  """
156
  start_time = time.time()
157
  print(f"[INFO] Starting stabilization using CSV: {csv_file}")
 
192
 
193
  dx, dy = motion_data.get(frame_idx, (0, 0))
194
  if vertical_only:
195
+ dx = 0 # Ignore horizontal motion for vertical-only stabilization.
 
196
  transform = np.array([[1, 0, dx],
197
  [0, 1, dy]], dtype=np.float32)
198
  stabilized_frame = cv2.warpAffine(frame, transform, (width, height))
 
200
  out.write(stabilized_frame)
201
  if frame_idx % 10 == 0 or frame_idx == total_frames:
202
  print(f"[INFO] Stabilized frame {frame_idx}/{total_frames}")
203
+
204
+ # Update progress for stabilization phase.
205
+ progress(progress_offset + (frame_idx / total_frames) * progress_scale, desc="Stabilizing Video")
206
  frame_idx += 1
207
 
208
  cap.release()
 
211
  print(f"[INFO] Stabilized video saved to: {output_file} in {elapsed:.2f} seconds")
212
  return output_file
213
 
214
+ def process_video_ai(video_file, zoom, vertical_only, progress=gr.Progress(track_tqdm=True)):
215
  """
216
  Gradio interface function:
217
  - Generates motion data (CSV) from the input video using an AI model (RAFT if available, else Farneback).
 
229
  raise ValueError("[ERROR] Please upload a video file.")
230
 
231
  print("[INFO] Starting AI-powered video processing...")
232
+ # First half: Generate motion CSV.
233
+ csv_file = generate_motion_csv(video_file, progress=progress, progress_offset=0.0, progress_scale=0.5)
234
+ # Second half: Stabilize video.
235
+ stabilized_path = stabilize_video_using_csv(video_file, csv_file, zoom=zoom, vertical_only=vertical_only,
236
+ progress=progress, progress_offset=0.5, progress_scale=0.5)
237
  print("[INFO] Video processing complete.")
238
  logs = log_buffer.getvalue()
239
  return video_file, stabilized_path, logs
 
241
  # Build the Gradio UI.
242
  with gr.Blocks() as demo:
243
  gr.Markdown("# AI-Powered Video Stabilization")
244
+ gr.Markdown("Upload a video, select a zoom factor, and choose whether to apply only vertical stabilization. The system will generate motion data using an AI model (RAFT if available) and then stabilize the video with live progress updates.")
245
 
246
  with gr.Row():
247
  with gr.Column():