Update app.py
Browse files
app.py
CHANGED
@@ -62,7 +62,7 @@ filepath = hf_hub_download(
|
|
62 |
repo_id=f"depth-anything/Video-Depth-Anything-{model_name}",
|
63 |
filename=f"video_depth_anything_{encoder}.pth",
|
64 |
repo_type="model",
|
65 |
-
cache_dir="/tmp/huggingface" #
|
66 |
)
|
67 |
video_depth_anything.load_state_dict(torch.load(filepath, map_location='cpu'))
|
68 |
video_depth_anything = video_depth_anything.to(DEVICE).eval()
|
@@ -80,7 +80,7 @@ def infer_video_depth(
|
|
80 |
grayscale: bool = True,
|
81 |
convert_from_color: bool = True,
|
82 |
blur: float = 0.3,
|
83 |
-
loop_factor: int = 1, #
|
84 |
output_dir: str = './outputs',
|
85 |
input_size: int = 518,
|
86 |
):
|
@@ -162,197 +162,165 @@ def infer_video_depth(
|
|
162 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
163 |
os.replace(temp_audio_path, stitched_video_path)
|
164 |
|
165 |
-
#
|
166 |
-
if loop_factor > 1:
|
167 |
-
#
|
168 |
os.makedirs(output_dir, exist_ok=True)
|
169 |
|
170 |
-
#
|
171 |
-
|
172 |
-
|
173 |
|
174 |
-
|
175 |
-
concat_file_path = os.path.join(output_dir, 'concat_list.txt')
|
176 |
-
with open(concat_file_path, 'w') as f:
|
177 |
-
for _ in range(loop_factor):
|
178 |
-
# Absoluten Pfad verwenden
|
179 |
-
abs_path = os.path.abspath(depth_vis_path)
|
180 |
-
f.write(f"file '{abs_path}'\n")
|
181 |
|
182 |
-
#
|
183 |
-
|
|
|
184 |
"ffmpeg",
|
185 |
-
"-
|
186 |
-
"-f", "concat",
|
187 |
-
"-safe", "0",
|
188 |
-
"-i", concat_file_path,
|
189 |
"-c", "copy",
|
190 |
-
|
|
|
191 |
]
|
192 |
-
subprocess.run(
|
|
|
|
|
|
|
|
|
193 |
|
194 |
-
#
|
195 |
-
|
196 |
-
# Ersetze den ursprünglichen Pfad durch den neuen geloopten Pfad
|
197 |
-
depth_vis_path = depth_looped_path
|
198 |
-
else:
|
199 |
-
print(f"WARNING: Failed to create looped depth video at {depth_looped_path}")
|
200 |
|
201 |
-
|
202 |
-
#
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
205 |
|
206 |
-
print(f"
|
|
|
207 |
|
208 |
-
#
|
209 |
-
|
210 |
-
check_audio_cmd = [
|
211 |
"ffmpeg",
|
212 |
-
"-
|
|
|
|
|
|
|
213 |
"-c", "copy",
|
214 |
-
|
215 |
-
"-"
|
216 |
]
|
217 |
-
|
218 |
-
|
219 |
-
if
|
220 |
-
|
221 |
-
print("Audio stream detected in input video")
|
222 |
|
223 |
-
#
|
224 |
-
|
|
|
|
|
|
|
|
|
225 |
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
# Absoluten Pfad verwenden
|
232 |
-
f.write(f"file '{abs_original_path}'\n")
|
233 |
-
|
234 |
-
print(f"Creating temporary file at: {temp_looped_path}")
|
235 |
-
print(f"Using absolute path for original: {abs_original_path}")
|
236 |
-
|
237 |
-
# Verwende ffmpeg, um das Video zu loopen
|
238 |
-
concat_cmd = [
|
239 |
"ffmpeg",
|
240 |
"-y",
|
241 |
-
"-
|
242 |
-
"-
|
243 |
-
|
244 |
-
"-c", "copy",
|
245 |
-
temp_looped_path
|
246 |
]
|
247 |
-
|
248 |
-
print(f"FFmpeg concat command exit code: {process.returncode}")
|
249 |
-
if process.returncode != 0:
|
250 |
-
print(f"FFmpeg error: {process.stderr.decode('utf-8')}")
|
251 |
|
252 |
-
#
|
253 |
-
if not os.path.exists(
|
254 |
-
print(f"
|
255 |
-
|
256 |
-
# Fallback
|
257 |
-
return [depth_vis_path, stitched_video_path]
|
258 |
-
|
259 |
-
# Wenn Audio vorhanden ist, müssen wir es separat behandeln
|
260 |
-
if has_audio:
|
261 |
-
# Extrahiere den Audio-Track aus dem originalen Input-Video
|
262 |
-
audio_path = os.path.join(output_dir, 'extracted_audio.aac')
|
263 |
-
extract_audio_cmd = [
|
264 |
-
"ffmpeg",
|
265 |
-
"-y",
|
266 |
-
"-i", input_video, # Original Input-Video verwenden
|
267 |
-
"-vn", "-acodec", "copy",
|
268 |
-
audio_path
|
269 |
-
]
|
270 |
-
subprocess.run(extract_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
271 |
-
|
272 |
-
# Prüfen, ob Audio extrahiert wurde
|
273 |
-
if not os.path.exists(audio_path) or os.path.getsize(audio_path) == 0:
|
274 |
-
print(f"WARNING: Failed to extract audio or no audio track in {input_video}")
|
275 |
-
has_audio = False
|
276 |
-
else:
|
277 |
-
# Erstelle eine Textdatei für das Audio-Looping
|
278 |
-
concat_audio_file_path = os.path.join(output_dir, 'concat_audio_list.txt')
|
279 |
-
with open(concat_audio_file_path, 'w') as f:
|
280 |
-
for _ in range(loop_factor):
|
281 |
-
# Absoluten Pfad verwenden
|
282 |
-
abs_audio_path = os.path.abspath(audio_path)
|
283 |
-
f.write(f"file '{abs_audio_path}'\n")
|
284 |
-
|
285 |
-
# Erstelle den geloopten Audio-Track
|
286 |
-
looped_audio_path = os.path.join(output_dir, 'looped_audio.aac')
|
287 |
-
audio_loop_cmd = [
|
288 |
-
"ffmpeg",
|
289 |
-
"-y",
|
290 |
-
"-f", "concat",
|
291 |
-
"-safe", "0",
|
292 |
-
"-i", concat_audio_file_path,
|
293 |
-
"-c", "copy",
|
294 |
-
looped_audio_path
|
295 |
-
]
|
296 |
-
subprocess.run(audio_loop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
297 |
-
|
298 |
-
# Prüfe, ob Audio geloopt wurde
|
299 |
-
if not os.path.exists(looped_audio_path) or os.path.getsize(looped_audio_path) == 0:
|
300 |
-
print(f"WARNING: Failed to create looped audio")
|
301 |
-
has_audio = False
|
302 |
-
|
303 |
-
# Finaler Schritt: Kombiniere Video und Audio wenn nötig, sonst nur Video kopieren
|
304 |
-
if has_audio:
|
305 |
-
# Kombiniere das geloopte Video mit dem geloopten Audio
|
306 |
-
final_cmd = [
|
307 |
-
"ffmpeg",
|
308 |
-
"-y",
|
309 |
-
"-i", temp_looped_path,
|
310 |
-
"-i", looped_audio_path,
|
311 |
-
"-c:v", "copy",
|
312 |
-
"-c:a", "aac",
|
313 |
-
"-map", "0:v:0",
|
314 |
-
"-map", "1:a:0",
|
315 |
-
"-shortest",
|
316 |
-
original_path # Verwenden des originalen Pfads als Ziel
|
317 |
-
]
|
318 |
-
subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
319 |
else:
|
320 |
-
#
|
321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
"ffmpeg",
|
323 |
"-y",
|
324 |
-
"-
|
|
|
|
|
325 |
"-c", "copy",
|
326 |
-
|
327 |
]
|
328 |
-
subprocess.run(
|
329 |
-
|
330 |
-
# Überprüfen, ob die Ersetzung erfolgreich war
|
331 |
-
if not os.path.exists(original_path):
|
332 |
-
print(f"ERROR: Failed to replace {original_path} with looped version")
|
333 |
-
else:
|
334 |
-
print(f"Successfully replaced {original_path} with looped version")
|
335 |
-
|
336 |
-
# Bereinige temporäre Dateien
|
337 |
-
temp_files = [concat_file_path, concat_stitched_file_path]
|
338 |
-
if has_audio:
|
339 |
-
temp_files.extend([concat_audio_file_path, audio_path, looped_audio_path])
|
340 |
-
if os.path.exists(temp_looped_path):
|
341 |
-
temp_files.append(temp_looped_path)
|
342 |
|
343 |
-
|
344 |
-
if os.path.exists(
|
345 |
-
|
346 |
-
|
347 |
-
except Exception as e:
|
348 |
-
print(f"Warning: Could not remove temporary file {file_path}: {str(e)}")
|
349 |
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
gc.collect()
|
358 |
torch.cuda.empty_cache()
|
@@ -386,8 +354,8 @@ def construct_demo():
|
|
386 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
387 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
388 |
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur (can reduce edge artifacts on display)", value=0.3)
|
389 |
-
#
|
390 |
-
loop_factor = gr.Slider(label="Loop Factor (repeats the output video)", minimum=1, maximum=20, value=1, step=1)
|
391 |
generate_btn = gr.Button("Generate")
|
392 |
with gr.Column(scale=2):
|
393 |
pass
|
@@ -396,7 +364,7 @@ def construct_demo():
|
|
396 |
|
397 |
generate_btn.click(
|
398 |
fn=infer_video_depth,
|
399 |
-
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider, loop_factor], # loop_factor
|
400 |
outputs=[depth_vis_video, stitched_video],
|
401 |
)
|
402 |
|
|
|
62 |
repo_id=f"depth-anything/Video-Depth-Anything-{model_name}",
|
63 |
filename=f"video_depth_anything_{encoder}.pth",
|
64 |
repo_type="model",
|
65 |
+
cache_dir="/tmp/huggingface" # Explicitly set the cache directory
|
66 |
)
|
67 |
video_depth_anything.load_state_dict(torch.load(filepath, map_location='cpu'))
|
68 |
video_depth_anything = video_depth_anything.to(DEVICE).eval()
|
|
|
80 |
grayscale: bool = True,
|
81 |
convert_from_color: bool = True,
|
82 |
blur: float = 0.3,
|
83 |
+
loop_factor: int = 1, # New parameter for video looping
|
84 |
output_dir: str = './outputs',
|
85 |
input_size: int = 518,
|
86 |
):
|
|
|
162 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
163 |
os.replace(temp_audio_path, stitched_video_path)
|
164 |
|
165 |
+
# Apply looping only to the RGBD video when requested
|
166 |
+
if loop_factor > 1 and stitch and stitched_video_path:
|
167 |
+
# Ensure the output directory exists
|
168 |
os.makedirs(output_dir, exist_ok=True)
|
169 |
|
170 |
+
# Save original path and absolute path
|
171 |
+
original_path = stitched_video_path
|
172 |
+
abs_original_path = os.path.abspath(original_path)
|
173 |
|
174 |
+
print(f"Looping video {original_path} with factor {loop_factor}")
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
+
# Check if the input video has an audio stream
|
177 |
+
has_audio = False
|
178 |
+
check_audio_cmd = [
|
179 |
"ffmpeg",
|
180 |
+
"-i", input_video,
|
|
|
|
|
|
|
181 |
"-c", "copy",
|
182 |
+
"-f", "null",
|
183 |
+
"-"
|
184 |
]
|
185 |
+
result = subprocess.run(check_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
186 |
+
stderr = result.stderr.decode('utf-8')
|
187 |
+
if "Audio" in stderr:
|
188 |
+
has_audio = True
|
189 |
+
print("Audio stream detected in input video")
|
190 |
|
191 |
+
# Temporary path in the output directory
|
192 |
+
temp_looped_path = os.path.join(output_dir, 'temp_rgbd_looped.mp4')
|
|
|
|
|
|
|
|
|
193 |
|
194 |
+
try:
|
195 |
+
# Create a temporary text file for the stitched videos
|
196 |
+
concat_stitched_file_path = os.path.join(output_dir, 'concat_stitched_list.txt')
|
197 |
+
with open(concat_stitched_file_path, 'w') as f:
|
198 |
+
for _ in range(loop_factor):
|
199 |
+
# Use absolute path
|
200 |
+
f.write(f"file '{abs_original_path}'\n")
|
201 |
|
202 |
+
print(f"Creating temporary file at: {temp_looped_path}")
|
203 |
+
print(f"Using absolute path for original: {abs_original_path}")
|
204 |
|
205 |
+
# Use ffmpeg to loop the video
|
206 |
+
concat_cmd = [
|
|
|
207 |
"ffmpeg",
|
208 |
+
"-y",
|
209 |
+
"-f", "concat",
|
210 |
+
"-safe", "0",
|
211 |
+
"-i", concat_stitched_file_path,
|
212 |
"-c", "copy",
|
213 |
+
temp_looped_path
|
|
|
214 |
]
|
215 |
+
process = subprocess.run(concat_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
216 |
+
print(f"FFmpeg concat command exit code: {process.returncode}")
|
217 |
+
if process.returncode != 0:
|
218 |
+
print(f"FFmpeg error: {process.stderr.decode('utf-8')}")
|
|
|
219 |
|
220 |
+
# Check if the temporary file was created
|
221 |
+
if not os.path.exists(temp_looped_path):
|
222 |
+
print(f"ERROR: Failed to create temporary file {temp_looped_path}")
|
223 |
+
print(f"Current directory contents: {os.listdir(output_dir)}")
|
224 |
+
# Fallback
|
225 |
+
return [depth_vis_path, stitched_video_path]
|
226 |
|
227 |
+
# If audio is present, we need to handle it separately
|
228 |
+
if has_audio:
|
229 |
+
# Extract the audio track from the original input video
|
230 |
+
audio_path = os.path.join(output_dir, 'extracted_audio.aac')
|
231 |
+
extract_audio_cmd = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
"ffmpeg",
|
233 |
"-y",
|
234 |
+
"-i", input_video, # Use original input video
|
235 |
+
"-vn", "-acodec", "copy",
|
236 |
+
audio_path
|
|
|
|
|
237 |
]
|
238 |
+
subprocess.run(extract_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
239 |
|
240 |
+
# Check if audio was extracted
|
241 |
+
if not os.path.exists(audio_path) or os.path.getsize(audio_path) == 0:
|
242 |
+
print(f"WARNING: Failed to extract audio or no audio track in {input_video}")
|
243 |
+
has_audio = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
else:
|
245 |
+
# Create a text file for audio looping
|
246 |
+
concat_audio_file_path = os.path.join(output_dir, 'concat_audio_list.txt')
|
247 |
+
with open(concat_audio_file_path, 'w') as f:
|
248 |
+
for _ in range(loop_factor):
|
249 |
+
# Use absolute path
|
250 |
+
abs_audio_path = os.path.abspath(audio_path)
|
251 |
+
f.write(f"file '{abs_audio_path}'\n")
|
252 |
+
|
253 |
+
# Create the looped audio track
|
254 |
+
looped_audio_path = os.path.join(output_dir, 'looped_audio.aac')
|
255 |
+
audio_loop_cmd = [
|
256 |
"ffmpeg",
|
257 |
"-y",
|
258 |
+
"-f", "concat",
|
259 |
+
"-safe", "0",
|
260 |
+
"-i", concat_audio_file_path,
|
261 |
"-c", "copy",
|
262 |
+
looped_audio_path
|
263 |
]
|
264 |
+
subprocess.run(audio_loop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
+
# Check if audio was looped
|
267 |
+
if not os.path.exists(looped_audio_path) or os.path.getsize(looped_audio_path) == 0:
|
268 |
+
print(f"WARNING: Failed to create looped audio")
|
269 |
+
has_audio = False
|
|
|
|
|
270 |
|
271 |
+
# Final step: Combine video and audio if needed, otherwise just copy video
|
272 |
+
if has_audio:
|
273 |
+
# Combine the looped video with the looped audio
|
274 |
+
final_cmd = [
|
275 |
+
"ffmpeg",
|
276 |
+
"-y",
|
277 |
+
"-i", temp_looped_path,
|
278 |
+
"-i", looped_audio_path,
|
279 |
+
"-c:v", "copy",
|
280 |
+
"-c:a", "aac",
|
281 |
+
"-map", "0:v:0",
|
282 |
+
"-map", "1:a:0",
|
283 |
+
"-shortest",
|
284 |
+
original_path # Use the original path as destination
|
285 |
+
]
|
286 |
+
subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
287 |
+
else:
|
288 |
+
# If no audio exists, just copy the video
|
289 |
+
copy_cmd = [
|
290 |
+
"ffmpeg",
|
291 |
+
"-y",
|
292 |
+
"-i", temp_looped_path,
|
293 |
+
"-c", "copy",
|
294 |
+
original_path
|
295 |
+
]
|
296 |
+
subprocess.run(copy_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
297 |
+
|
298 |
+
# Check if the replacement was successful
|
299 |
+
if not os.path.exists(original_path):
|
300 |
+
print(f"ERROR: Failed to replace {original_path} with looped version")
|
301 |
+
else:
|
302 |
+
print(f"Successfully replaced {original_path} with looped version")
|
303 |
+
|
304 |
+
# Clean up temporary files
|
305 |
+
temp_files = [concat_stitched_file_path]
|
306 |
+
if has_audio:
|
307 |
+
temp_files.extend([concat_audio_file_path, audio_path, looped_audio_path])
|
308 |
+
if os.path.exists(temp_looped_path):
|
309 |
+
temp_files.append(temp_looped_path)
|
310 |
+
|
311 |
+
for file_path in temp_files:
|
312 |
+
if os.path.exists(file_path):
|
313 |
+
try:
|
314 |
+
os.remove(file_path)
|
315 |
+
except Exception as e:
|
316 |
+
print(f"Warning: Could not remove temporary file {file_path}: {str(e)}")
|
317 |
+
|
318 |
+
except Exception as e:
|
319 |
+
print(f"Error during looping process: {str(e)}")
|
320 |
+
import traceback
|
321 |
+
traceback.print_exc()
|
322 |
+
# In case of error, keep the original files
|
323 |
+
return [depth_vis_path, stitched_video_path]
|
324 |
|
325 |
gc.collect()
|
326 |
torch.cuda.empty_cache()
|
|
|
354 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
355 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
356 |
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur (can reduce edge artifacts on display)", value=0.3)
|
357 |
+
# Add the loop factor slider
|
358 |
+
loop_factor = gr.Slider(label="Loop Factor (repeats the RGBD output video)", minimum=1, maximum=20, value=1, step=1)
|
359 |
generate_btn = gr.Button("Generate")
|
360 |
with gr.Column(scale=2):
|
361 |
pass
|
|
|
364 |
|
365 |
generate_btn.click(
|
366 |
fn=infer_video_depth,
|
367 |
+
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider, loop_factor], # Added loop_factor
|
368 |
outputs=[depth_vis_video, stitched_video],
|
369 |
)
|
370 |
|