Update app.py
Browse files
app.py
CHANGED
@@ -80,6 +80,7 @@ def infer_video_depth(
|
|
80 |
grayscale: bool = True,
|
81 |
convert_from_color: bool = True,
|
82 |
blur: float = 0.3,
|
|
|
83 |
output_dir: str = './outputs',
|
84 |
input_size: int = 518,
|
85 |
):
|
@@ -160,6 +161,110 @@ def infer_video_depth(
|
|
160 |
]
|
161 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
162 |
os.replace(temp_audio_path, stitched_video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
gc.collect()
|
165 |
torch.cuda.empty_cache()
|
@@ -193,6 +298,8 @@ def construct_demo():
|
|
193 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
194 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
195 |
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur (can reduce edge artifacts on display)", value=0.3)
|
|
|
|
|
196 |
generate_btn = gr.Button("Generate")
|
197 |
with gr.Column(scale=2):
|
198 |
pass
|
@@ -201,8 +308,8 @@ def construct_demo():
|
|
201 |
|
202 |
generate_btn.click(
|
203 |
fn=infer_video_depth,
|
204 |
-
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider],
|
205 |
-
outputs=[depth_vis_video, stitched_video],
|
206 |
)
|
207 |
|
208 |
return demo
|
|
|
80 |
grayscale: bool = True,
|
81 |
convert_from_color: bool = True,
|
82 |
blur: float = 0.3,
|
83 |
+
loop_factor: int = 1, # Neuer Parameter
|
84 |
output_dir: str = './outputs',
|
85 |
input_size: int = 518,
|
86 |
):
|
|
|
161 |
]
|
162 |
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
163 |
os.replace(temp_audio_path, stitched_video_path)
|
164 |
+
|
165 |
+
# Nachdem die Videos erstellt wurden, wenden wir den Loop-Faktor an
|
166 |
+
if loop_factor > 1:
|
167 |
+
depth_looped_path = os.path.join(output_dir, os.path.splitext(os.path.basename(depth_vis_path))[0] + f'_loop{loop_factor}.mp4')
|
168 |
+
|
169 |
+
# Erstelle eine temporäre Textdatei mit der Liste der zu wiederholenden Dateien
|
170 |
+
concat_file_path = os.path.join(output_dir, 'concat_list.txt')
|
171 |
+
with open(concat_file_path, 'w') as f:
|
172 |
+
for _ in range(loop_factor):
|
173 |
+
f.write(f"file '{depth_vis_path}'\n")
|
174 |
+
|
175 |
+
# Verwende ffmpeg, um das Video zu wiederholen ohne Neucodierung
|
176 |
+
cmd = [
|
177 |
+
"ffmpeg",
|
178 |
+
"-y",
|
179 |
+
"-f", "concat",
|
180 |
+
"-safe", "0",
|
181 |
+
"-i", concat_file_path,
|
182 |
+
"-c", "copy",
|
183 |
+
depth_looped_path
|
184 |
+
]
|
185 |
+
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
186 |
+
|
187 |
+
# Ersetze den ursprünglichen Pfad durch den neuen geloopten Pfad
|
188 |
+
depth_vis_path = depth_looped_path
|
189 |
+
|
190 |
+
if stitch and stitched_video_path:
|
191 |
+
# Speichern wir den Originalnamen
|
192 |
+
original_path = stitched_video_path
|
193 |
+
|
194 |
+
# Temporärer Pfad für das geloopte Video
|
195 |
+
temp_looped_path = os.path.join(output_dir, 'temp_looped_rgbd.mp4')
|
196 |
+
|
197 |
+
# Erstelle eine temporäre Textdatei für die stitched Videos
|
198 |
+
concat_stitched_file_path = os.path.join(output_dir, 'concat_stitched_list.txt')
|
199 |
+
with open(concat_stitched_file_path, 'w') as f:
|
200 |
+
for _ in range(loop_factor):
|
201 |
+
f.write(f"file '{original_path}'\n")
|
202 |
+
|
203 |
+
# Verwende ffmpeg, um das stitched Video zu loopen
|
204 |
+
cmd = [
|
205 |
+
"ffmpeg",
|
206 |
+
"-y",
|
207 |
+
"-f", "concat",
|
208 |
+
"-safe", "0",
|
209 |
+
"-i", concat_stitched_file_path,
|
210 |
+
"-c", "copy",
|
211 |
+
temp_looped_path
|
212 |
+
]
|
213 |
+
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
214 |
+
|
215 |
+
# Extrahiere den Audio-Track aus dem ursprünglichen Input-Video
|
216 |
+
audio_path = os.path.join(output_dir, 'extracted_audio.aac')
|
217 |
+
extract_audio_cmd = [
|
218 |
+
"ffmpeg",
|
219 |
+
"-y",
|
220 |
+
"-i", input_video,
|
221 |
+
"-vn", "-acodec", "copy",
|
222 |
+
audio_path
|
223 |
+
]
|
224 |
+
subprocess.run(extract_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
225 |
+
|
226 |
+
# Erstelle eine Textdatei für das Audio-Looping
|
227 |
+
concat_audio_file_path = os.path.join(output_dir, 'concat_audio_list.txt')
|
228 |
+
with open(concat_audio_file_path, 'w') as f:
|
229 |
+
for _ in range(loop_factor):
|
230 |
+
f.write(f"file '{audio_path}'\n")
|
231 |
+
|
232 |
+
# Erstelle den geloopten Audio-Track
|
233 |
+
looped_audio_path = os.path.join(output_dir, 'looped_audio.aac')
|
234 |
+
audio_loop_cmd = [
|
235 |
+
"ffmpeg",
|
236 |
+
"-y",
|
237 |
+
"-f", "concat",
|
238 |
+
"-safe", "0",
|
239 |
+
"-i", concat_audio_file_path,
|
240 |
+
"-c", "copy",
|
241 |
+
looped_audio_path
|
242 |
+
]
|
243 |
+
subprocess.run(audio_loop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
244 |
+
|
245 |
+
# Kombiniere das geloopte Video mit dem geloopten Audio
|
246 |
+
# Aber schreibe zurück in den originalen Pfad
|
247 |
+
final_cmd = [
|
248 |
+
"ffmpeg",
|
249 |
+
"-y",
|
250 |
+
"-i", temp_looped_path,
|
251 |
+
"-i", looped_audio_path,
|
252 |
+
"-c:v", "copy",
|
253 |
+
"-c:a", "aac",
|
254 |
+
"-map", "0:v:0",
|
255 |
+
"-map", "1:a:0",
|
256 |
+
"-shortest",
|
257 |
+
original_path # Verwenden des originalen Pfads als Ziel
|
258 |
+
]
|
259 |
+
subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
260 |
+
|
261 |
+
# Wir verwenden weiterhin den originalen Pfad
|
262 |
+
# (stitched_video_path bleibt unverändert)
|
263 |
+
|
264 |
+
# Bereinige temporäre Dateien
|
265 |
+
for file_path in [concat_file_path, concat_stitched_file_path, concat_audio_file_path, audio_path, looped_audio_path, temp_looped_path]:
|
266 |
+
if os.path.exists(file_path):
|
267 |
+
os.remove(file_path)
|
268 |
|
269 |
gc.collect()
|
270 |
torch.cuda.empty_cache()
|
|
|
298 |
grayscale_option = gr.Checkbox(label="Output Depth as Grayscale", value=True)
|
299 |
convert_from_color_option = gr.Checkbox(label="Convert Grayscale from Color", value=True)
|
300 |
blur_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Depth Blur (can reduce edge artifacts on display)", value=0.3)
|
301 |
+
# Füge den Loop-Faktor Slider hinzu
|
302 |
+
loop_factor = gr.Slider(label="Loop Factor (repeats the output video)", minimum=1, maximum=20, value=1, step=1)
|
303 |
generate_btn = gr.Button("Generate")
|
304 |
with gr.Column(scale=2):
|
305 |
pass
|
|
|
308 |
|
309 |
generate_btn.click(
|
310 |
fn=infer_video_depth,
|
311 |
+
inputs=[input_video, max_len, target_fps, max_res, stitch_option, grayscale_option, convert_from_color_option, blur_slider, loop_factor], # loop_factor hinzugefügt
|
312 |
+
outputs=[depth_vis_video, stitched_video],
|
313 |
)
|
314 |
|
315 |
return demo
|