Update app.py
Browse files
app.py
CHANGED
@@ -147,35 +147,65 @@ def interpolate_frames(video_path, target_fps=30):
|
|
147 |
print("Not enough frames for interpolation.")
|
148 |
return video_path
|
149 |
|
150 |
-
# Calculate interpolation factor
|
151 |
-
interpolation_factor =
|
152 |
-
interpolated_frames = []
|
153 |
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
# Perform frame interpolation
|
157 |
-
|
158 |
-
interpolated_frames.append(frames[i])
|
159 |
-
|
160 |
-
# Generate intermediate frames
|
161 |
-
for j in range(1, interpolation_factor):
|
162 |
-
alpha = j / interpolation_factor
|
163 |
-
# Use weighted average for simple interpolation
|
164 |
-
interpolated_frame = cv2.addWeighted(
|
165 |
-
frames[i], 1 - alpha,
|
166 |
-
frames[i + 1], alpha,
|
167 |
-
0
|
168 |
-
)
|
169 |
-
interpolated_frames.append(interpolated_frame)
|
170 |
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
# Save the interpolated video
|
175 |
output_path = video_path.replace('.mp4', '_interpolated.mp4')
|
176 |
-
|
|
|
|
|
177 |
out = cv2.VideoWriter(output_path, fourcc, target_fps, (width, height))
|
178 |
|
|
|
|
|
|
|
|
|
|
|
179 |
for frame in interpolated_frames:
|
180 |
out.write(frame)
|
181 |
out.release()
|
@@ -185,6 +215,8 @@ def interpolate_frames(video_path, target_fps=30):
|
|
185 |
|
186 |
except Exception as e:
|
187 |
print(f"Error during frame interpolation: {e}")
|
|
|
|
|
188 |
return video_path # Return original if interpolation fails
|
189 |
|
190 |
# --- Initialization ---
|
@@ -395,7 +427,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px
|
|
395 |
minimum=0.5,
|
396 |
maximum=5.0,
|
397 |
step=0.1,
|
398 |
-
value=0
|
399 |
info="Lower values produce smoother but less controlled motion"
|
400 |
)
|
401 |
|
@@ -412,10 +444,10 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px
|
|
412 |
fps_slider = gr.Slider(
|
413 |
label="Target FPS",
|
414 |
minimum=24,
|
415 |
-
maximum=
|
416 |
-
step=
|
417 |
-
value=
|
418 |
-
info="Higher FPS for smoother motion
|
419 |
)
|
420 |
|
421 |
submit_button = gr.Button("🎥 Generate Video", variant="primary", size="lg")
|
|
|
147 |
print("Not enough frames for interpolation.")
|
148 |
return video_path
|
149 |
|
150 |
+
# Calculate interpolation factor (can be fractional)
|
151 |
+
interpolation_factor = target_fps / original_fps
|
|
|
152 |
|
153 |
+
# For fractional factors, we need different approach
|
154 |
+
if interpolation_factor <= 1:
|
155 |
+
print("Interpolation factor too low. Skipping.")
|
156 |
+
return video_path
|
157 |
+
|
158 |
+
print(f"Interpolating with factor: {interpolation_factor:.2f}")
|
159 |
+
print(f"Total frames to process: {len(frames)}")
|
160 |
|
161 |
# Perform frame interpolation
|
162 |
+
interpolated_frames = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
if interpolation_factor == int(interpolation_factor):
|
165 |
+
# Integer factor - simple interpolation
|
166 |
+
factor = int(interpolation_factor)
|
167 |
+
for i in range(len(frames) - 1):
|
168 |
+
interpolated_frames.append(frames[i])
|
169 |
+
# Generate intermediate frames
|
170 |
+
for j in range(1, factor):
|
171 |
+
alpha = j / factor
|
172 |
+
interpolated_frame = cv2.addWeighted(
|
173 |
+
frames[i], 1 - alpha,
|
174 |
+
frames[i + 1], alpha,
|
175 |
+
0
|
176 |
+
)
|
177 |
+
interpolated_frames.append(interpolated_frame)
|
178 |
+
interpolated_frames.append(frames[-1])
|
179 |
+
else:
|
180 |
+
# Fractional factor - use different approach
|
181 |
+
# For 25 -> 60 fps, we need to add selective frames
|
182 |
+
for i in range(len(frames) - 1):
|
183 |
+
interpolated_frames.append(frames[i])
|
184 |
+
# Add intermediate frame for smoother motion
|
185 |
+
if i % 2 == 0: # Add extra frame every other original frame
|
186 |
+
alpha = 0.4 # Blend ratio
|
187 |
+
interpolated_frame = cv2.addWeighted(
|
188 |
+
frames[i], 1 - alpha,
|
189 |
+
frames[i + 1], alpha,
|
190 |
+
0
|
191 |
+
)
|
192 |
+
interpolated_frames.append(interpolated_frame)
|
193 |
+
interpolated_frames.append(frames[-1])
|
194 |
+
|
195 |
+
print(f"Total interpolated frames: {len(interpolated_frames)}")
|
196 |
|
197 |
# Save the interpolated video
|
198 |
output_path = video_path.replace('.mp4', '_interpolated.mp4')
|
199 |
+
|
200 |
+
# Use H.264 codec for better compatibility
|
201 |
+
fourcc = cv2.VideoWriter_fourcc(*'H264')
|
202 |
out = cv2.VideoWriter(output_path, fourcc, target_fps, (width, height))
|
203 |
|
204 |
+
if not out.isOpened():
|
205 |
+
# Fallback to mp4v if H264 not available
|
206 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
207 |
+
out = cv2.VideoWriter(output_path, fourcc, target_fps, (width, height))
|
208 |
+
|
209 |
for frame in interpolated_frames:
|
210 |
out.write(frame)
|
211 |
out.release()
|
|
|
215 |
|
216 |
except Exception as e:
|
217 |
print(f"Error during frame interpolation: {e}")
|
218 |
+
import traceback
|
219 |
+
traceback.print_exc()
|
220 |
return video_path # Return original if interpolation fails
|
221 |
|
222 |
# --- Initialization ---
|
|
|
427 |
minimum=0.5,
|
428 |
maximum=5.0,
|
429 |
step=0.1,
|
430 |
+
value=1.0,
|
431 |
info="Lower values produce smoother but less controlled motion"
|
432 |
)
|
433 |
|
|
|
444 |
fps_slider = gr.Slider(
|
445 |
label="Target FPS",
|
446 |
minimum=24,
|
447 |
+
maximum=50,
|
448 |
+
step=1,
|
449 |
+
value=30,
|
450 |
+
info="Higher FPS for smoother motion. 30 FPS recommended, 50 FPS maximum"
|
451 |
)
|
452 |
|
453 |
submit_button = gr.Button("🎥 Generate Video", variant="primary", size="lg")
|