Spaces:
Running
on
Zero
Running
on
Zero
add
Browse files
app.py
CHANGED
@@ -5,8 +5,6 @@ from enum import Enum
|
|
5 |
import db_examples
|
6 |
import cv2
|
7 |
|
8 |
-
import spaces
|
9 |
-
|
10 |
from demo_utils1 import *
|
11 |
|
12 |
from misc_utils.train_utils import unit_test_create_model
|
@@ -24,6 +22,7 @@ from torchvision.transforms import functional as F
|
|
24 |
from torch.hub import download_url_to_file
|
25 |
|
26 |
import os
|
|
|
27 |
|
28 |
# 推理设置
|
29 |
from pl_trainer.inference.inference import InferenceIP2PVideo
|
@@ -184,7 +183,6 @@ os.makedirs(new_tmp_dir, exist_ok=True)
|
|
184 |
def save_video_from_frames(image_pred, save_pth, fps=8):
|
185 |
"""
|
186 |
将 image_pred 中的帧保存为视频文件。
|
187 |
-
|
188 |
参数:
|
189 |
- image_pred: Tensor,形状为 (1, 16, 3, 512, 512)
|
190 |
- save_pth: 保存视频的路径,例如 "output_video.mp4"
|
@@ -224,9 +222,28 @@ inf_pipe = InferenceIP2PVideo(
|
|
224 |
num_ddim_steps=20
|
225 |
)
|
226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
# 伪函数占位(生成空白视频)
|
228 |
@spaces.GPU
|
229 |
-
def dummy_process(input_fg, input_bg):
|
230 |
# import pdb; pdb.set_trace()
|
231 |
|
232 |
diffusion_model.to(torch.float16)
|
@@ -240,7 +257,8 @@ def dummy_process(input_fg, input_bg):
|
|
240 |
# 初始化潜变量
|
241 |
init_latent = torch.randn_like(cond_fg_tensor)
|
242 |
|
243 |
-
EDIT_PROMPT = 'change the background'
|
|
|
244 |
VIDEO_CFG = 1.2
|
245 |
TEXT_CFG = 7.5
|
246 |
text_cond = diffusion_model.encode_text([EDIT_PROMPT]) # (1, 77, 768)
|
@@ -295,76 +313,147 @@ def dummy_process(input_fg, input_bg):
|
|
295 |
class BGSource(Enum):
|
296 |
UPLOAD = "Use Background Video"
|
297 |
UPLOAD_FLIP = "Use Flipped Background Video"
|
298 |
-
|
299 |
-
|
300 |
-
TOP = "Top Light"
|
301 |
-
BOTTOM = "Bottom Light"
|
302 |
-
GREY = "Ambient"
|
303 |
|
304 |
# Quick prompts 示例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
quick_prompts = [
|
306 |
'beautiful woman',
|
307 |
'handsome man',
|
308 |
-
'beautiful woman, cinematic lighting',
|
309 |
'handsome man, cinematic lighting',
|
310 |
'beautiful woman, natural lighting',
|
311 |
'handsome man, natural lighting',
|
312 |
-
'beautiful woman,
|
313 |
-
'handsome man,
|
|
|
314 |
]
|
|
|
|
|
315 |
quick_prompts = [[x] for x in quick_prompts]
|
316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
# Gradio UI 结构
|
318 |
block = gr.Blocks().queue()
|
319 |
with block:
|
320 |
with gr.Row():
|
321 |
-
gr.Markdown("##
|
|
|
322 |
|
323 |
with gr.Row():
|
324 |
with gr.Column():
|
325 |
with gr.Row():
|
326 |
-
input_fg = gr.Video(label="Foreground Video", height=
|
327 |
-
input_bg = gr.Video(label="Background Video", height=
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
333 |
|
334 |
-
|
335 |
bg_gallery = gr.Gallery(height=450, object_fit='contain', label='Background Quick List', value=db_examples.bg_samples, columns=5, allow_preview=False)
|
336 |
-
|
337 |
|
338 |
with gr.Group():
|
|
|
|
|
|
|
339 |
with gr.Row():
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
a_prompt = gr.Textbox(label="Added Prompt", value='best quality')
|
352 |
-
n_prompt = gr.Textbox(label="Negative Prompt", value='lowres, bad anatomy, bad hands, cropped, worst quality')
|
353 |
-
normal_button = gr.Button(value="Compute Normal (4x Slower)")
|
354 |
|
355 |
with gr.Column():
|
356 |
-
result_video = gr.Video(label='Output Video', height=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
357 |
|
358 |
# 输入列表
|
359 |
# ips = [input_fg, input_bg, prompt, video_width, video_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source]
|
360 |
-
ips = [input_fg, input_bg]
|
361 |
|
362 |
# 按钮绑定处理函数
|
363 |
# relight_button.click(fn=lambda: None, inputs=[], outputs=[result_video])
|
364 |
|
365 |
relight_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
366 |
|
367 |
-
normal_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
368 |
|
369 |
# 背景库选择
|
370 |
def bg_gallery_selected(gal, evt: gr.SelectData):
|
@@ -376,16 +465,34 @@ with block:
|
|
376 |
|
377 |
bg_gallery.select(bg_gallery_selected, inputs=bg_gallery, outputs=input_bg)
|
378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
# 示例
|
380 |
# dummy_video_for_outputs = gr.Video(visible=False, label='Result')
|
381 |
gr.Examples(
|
382 |
-
fn=lambda *args: args[-1],
|
|
|
383 |
examples=db_examples.background_conditioned_examples,
|
384 |
-
inputs=[
|
385 |
-
|
|
|
386 |
run_on_click=True, examples_per_page=1024
|
387 |
)
|
388 |
|
389 |
# 启动 Gradio 应用
|
390 |
# block.launch(server_name='0.0.0.0', server_port=10002, share=True)
|
391 |
-
block.launch()
|
|
|
5 |
import db_examples
|
6 |
import cv2
|
7 |
|
|
|
|
|
8 |
from demo_utils1 import *
|
9 |
|
10 |
from misc_utils.train_utils import unit_test_create_model
|
|
|
22 |
from torch.hub import download_url_to_file
|
23 |
|
24 |
import os
|
25 |
+
import spaces
|
26 |
|
27 |
# 推理设置
|
28 |
from pl_trainer.inference.inference import InferenceIP2PVideo
|
|
|
183 |
def save_video_from_frames(image_pred, save_pth, fps=8):
|
184 |
"""
|
185 |
将 image_pred 中的帧保存为视频文件。
|
|
|
186 |
参数:
|
187 |
- image_pred: Tensor,形状为 (1, 16, 3, 512, 512)
|
188 |
- save_pth: 保存视频的路径,例如 "output_video.mp4"
|
|
|
222 |
num_ddim_steps=20
|
223 |
)
|
224 |
|
225 |
+
|
226 |
+
def process_example(*args):
|
227 |
+
v_index = args[0]
|
228 |
+
select_e = db_examples.background_conditioned_examples[int(v_index)-1]
|
229 |
+
input_fg_path = select_e[1]
|
230 |
+
input_bg_path = select_e[2]
|
231 |
+
result_video_path = select_e[-1]
|
232 |
+
# input_fg_img = args[1] # 第 0 个参数
|
233 |
+
# input_bg_img = args[2] # 第 1 个参数
|
234 |
+
# result_video_img = args[-1] # 最后一个参数
|
235 |
+
|
236 |
+
input_fg = input_fg_path.replace("frames/0000.png", "cropped_video.mp4")
|
237 |
+
input_bg = input_bg_path.replace("frames/0000.png", "cropped_video.mp4")
|
238 |
+
result_video = result_video_path.replace(".png", ".mp4")
|
239 |
+
|
240 |
+
return input_fg, input_bg, result_video
|
241 |
+
|
242 |
+
|
243 |
+
|
244 |
# 伪函数占位(生成空白视频)
|
245 |
@spaces.GPU
|
246 |
+
def dummy_process(input_fg, input_bg, prompt):
|
247 |
# import pdb; pdb.set_trace()
|
248 |
|
249 |
diffusion_model.to(torch.float16)
|
|
|
257 |
# 初始化潜变量
|
258 |
init_latent = torch.randn_like(cond_fg_tensor)
|
259 |
|
260 |
+
# EDIT_PROMPT = 'change the background'
|
261 |
+
EDIT_PROMPT = prompt
|
262 |
VIDEO_CFG = 1.2
|
263 |
TEXT_CFG = 7.5
|
264 |
text_cond = diffusion_model.encode_text([EDIT_PROMPT]) # (1, 77, 768)
|
|
|
313 |
class BGSource(Enum):
|
314 |
UPLOAD = "Use Background Video"
|
315 |
UPLOAD_FLIP = "Use Flipped Background Video"
|
316 |
+
UPLOAD_REVERSE = "Use Reversed Background Video"
|
317 |
+
|
|
|
|
|
|
|
318 |
|
319 |
# Quick prompts 示例
|
320 |
+
# quick_prompts = [
|
321 |
+
# 'beautiful woman, fantasy setting',
|
322 |
+
# 'beautiful woman, neon dynamic lighting',
|
323 |
+
# 'man in suit, tunel lighting',
|
324 |
+
# 'animated mouse, aesthetic lighting',
|
325 |
+
# 'robot warrior, a sunset background',
|
326 |
+
# 'yellow cat, reflective wet beach',
|
327 |
+
# 'camera, dock, calm sunset',
|
328 |
+
# 'astronaut, dim lighting',
|
329 |
+
# 'astronaut, colorful balloons',
|
330 |
+
# 'astronaut, desert landscape'
|
331 |
+
# ]
|
332 |
+
|
333 |
+
# quick_prompts = [
|
334 |
+
# 'beautiful woman',
|
335 |
+
# 'handsome man',
|
336 |
+
# 'beautiful woman, cinematic lighting',
|
337 |
+
# 'handsome man, cinematic lighting',
|
338 |
+
# 'beautiful woman, natural lighting',
|
339 |
+
# 'handsome man, natural lighting',
|
340 |
+
# 'beautiful woman, neo punk lighting, cyberpunk',
|
341 |
+
# 'handsome man, neo punk lighting, cyberpunk',
|
342 |
+
# ]
|
343 |
+
|
344 |
+
|
345 |
quick_prompts = [
|
346 |
'beautiful woman',
|
347 |
'handsome man',
|
348 |
+
# 'beautiful woman, cinematic lighting',
|
349 |
'handsome man, cinematic lighting',
|
350 |
'beautiful woman, natural lighting',
|
351 |
'handsome man, natural lighting',
|
352 |
+
'beautiful woman, warm lighting',
|
353 |
+
'handsome man, soft lighting',
|
354 |
+
'change the background lighting',
|
355 |
]
|
356 |
+
|
357 |
+
|
358 |
quick_prompts = [[x] for x in quick_prompts]
|
359 |
|
360 |
+
# css = """
|
361 |
+
# #foreground-gallery {
|
362 |
+
# width: 700 !important; /* 限制最大宽度 */
|
363 |
+
# max-width: 700px !important; /* 避免它自动变宽 */
|
364 |
+
# flex: none !important; /* 让它不自动扩展 */
|
365 |
+
# }
|
366 |
+
# """
|
367 |
+
|
368 |
+
# css = """
|
369 |
+
# #prompt-box, #bg-source, #quick-list, #relight-btn {
|
370 |
+
# width: 750px !important;
|
371 |
+
# }
|
372 |
+
# """
|
373 |
+
|
374 |
# Gradio UI 结构
|
375 |
block = gr.Blocks().queue()
|
376 |
with block:
|
377 |
with gr.Row():
|
378 |
+
# gr.Markdown("## RelightVid (Relighting with Foreground and Background Video Condition)")
|
379 |
+
gr.Markdown("# 💡RelightVid \n### Relighting with Foreground and Background Video Condition")
|
380 |
|
381 |
with gr.Row():
|
382 |
with gr.Column():
|
383 |
with gr.Row():
|
384 |
+
input_fg = gr.Video(label="Foreground Video", height=380, width=420, visible=True)
|
385 |
+
input_bg = gr.Video(label="Background Video", height=380, width=420, visible=True)
|
386 |
+
|
387 |
+
segment_button = gr.Button(value="Video Segmentation")
|
388 |
+
with gr.Accordion("Segmentation Options", open=False):
|
389 |
+
# 如果用户不使用 point_prompt,而是直接提供坐标,则使用 x, y
|
390 |
+
with gr.Row():
|
391 |
+
x_coord = gr.Slider(label="X Coordinate (Point Prompt Ratio)", minimum=0.0, maximum=1.0, value=0.5, step=0.01)
|
392 |
+
y_coord = gr.Slider(label="Y Coordinate (Point Prompt Ratio)", minimum=0.0, maximum=1.0, value=0.5, step=0.01)
|
393 |
+
|
394 |
|
395 |
+
fg_gallery = gr.Gallery(height=150, object_fit='contain', label='Foreground Quick List', value=db_examples.fg_samples, columns=5, allow_preview=False)
|
396 |
bg_gallery = gr.Gallery(height=450, object_fit='contain', label='Background Quick List', value=db_examples.bg_samples, columns=5, allow_preview=False)
|
397 |
+
|
398 |
|
399 |
with gr.Group():
|
400 |
+
# with gr.Row():
|
401 |
+
# num_samples = gr.Slider(label="Videos", minimum=1, maximum=12, value=1, step=1)
|
402 |
+
# seed = gr.Number(label="Seed", value=12345, precision=0)
|
403 |
with gr.Row():
|
404 |
+
video_width = gr.Slider(label="Video Width", minimum=256, maximum=1024, value=512, step=64, visible=False)
|
405 |
+
video_height = gr.Slider(label="Video Height", minimum=256, maximum=1024, value=512, step=64, visible=False)
|
406 |
+
|
407 |
+
# with gr.Accordion("Advanced options", open=False):
|
408 |
+
# steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
|
409 |
+
# cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=7.0, step=0.01)
|
410 |
+
# highres_scale = gr.Slider(label="Highres Scale", minimum=1.0, maximum=3.0, value=1.5, step=0.01)
|
411 |
+
# highres_denoise = gr.Slider(label="Highres Denoise", minimum=0.1, maximum=0.9, value=0.5, step=0.01)
|
412 |
+
# a_prompt = gr.Textbox(label="Added Prompt", value='best quality')
|
413 |
+
# n_prompt = gr.Textbox(label="Negative Prompt", value='lowres, bad anatomy, bad hands, cropped, worst quality')
|
414 |
+
# normal_button = gr.Button(value="Compute Normal (4x Slower)")
|
|
|
|
|
|
|
415 |
|
416 |
with gr.Column():
|
417 |
+
result_video = gr.Video(label='Output Video', height=750, visible=True)
|
418 |
+
|
419 |
+
prompt = gr.Textbox(label="Prompt")
|
420 |
+
bg_source = gr.Radio(choices=[e.value for e in BGSource],
|
421 |
+
value=BGSource.UPLOAD.value,
|
422 |
+
label="Background Source",
|
423 |
+
type='value')
|
424 |
+
|
425 |
+
example_prompts = gr.Dataset(samples=quick_prompts, label='Prompt Quick List', components=[prompt])
|
426 |
+
relight_button = gr.Button(value="Relight")
|
427 |
+
|
428 |
+
# prompt = gr.Textbox(label="Prompt")
|
429 |
+
# bg_source = gr.Radio(choices=[e.value for e in BGSource],
|
430 |
+
# value=BGSource.UPLOAD.value,
|
431 |
+
# label="Background Source", type='value')
|
432 |
+
|
433 |
+
# example_prompts = gr.Dataset(samples=quick_prompts, label='Prompt Quick List', components=[prompt])
|
434 |
+
# relight_button = gr.Button(value="Relight")
|
435 |
+
# fg_gallery = gr.Gallery(witdth=400, object_fit='contain', label='Foreground Quick List', value=db_examples.bg_samples, columns=4, allow_preview=False)
|
436 |
+
# fg_gallery = gr.Gallery(
|
437 |
+
# height=380,
|
438 |
+
# object_fit='contain',
|
439 |
+
# label='Foreground Quick List',
|
440 |
+
# value=db_examples.fg_samples,
|
441 |
+
# columns=4,
|
442 |
+
# allow_preview=False,
|
443 |
+
# elem_id="foreground-gallery" # 👈 添加 elem_id
|
444 |
+
# )
|
445 |
+
|
446 |
|
447 |
# 输入列表
|
448 |
# ips = [input_fg, input_bg, prompt, video_width, video_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source]
|
449 |
+
ips = [input_fg, input_bg, prompt]
|
450 |
|
451 |
# 按钮绑定处理函数
|
452 |
# relight_button.click(fn=lambda: None, inputs=[], outputs=[result_video])
|
453 |
|
454 |
relight_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
455 |
|
456 |
+
# normal_button.click(fn=dummy_process, inputs=ips, outputs=[result_video])
|
457 |
|
458 |
# 背景库选择
|
459 |
def bg_gallery_selected(gal, evt: gr.SelectData):
|
|
|
465 |
|
466 |
bg_gallery.select(bg_gallery_selected, inputs=bg_gallery, outputs=input_bg)
|
467 |
|
468 |
+
def fg_gallery_selected(gal, evt: gr.SelectData):
|
469 |
+
# import pdb; pdb.set_trace()
|
470 |
+
# img_path = gal[evt.index][0]
|
471 |
+
img_path = db_examples.fg_samples[evt.index]
|
472 |
+
video_path = img_path.replace('frames/0000.png', 'cropped_video.mp4')
|
473 |
+
return video_path
|
474 |
+
|
475 |
+
fg_gallery.select(fg_gallery_selected, inputs=fg_gallery, outputs=input_fg)
|
476 |
+
|
477 |
+
input_fg_img = gr.Image(label="Foreground Video", visible=False)
|
478 |
+
input_bg_img = gr.Image(label="Background Video", visible=False)
|
479 |
+
result_video_img = gr.Image(label="Output Video", visible=False)
|
480 |
+
|
481 |
+
v_index = gr.Textbox(label="ID", visible=False)
|
482 |
+
example_prompts.click(lambda x: x[0], inputs=example_prompts, outputs=prompt, show_progress=False, queue=False)
|
483 |
+
|
484 |
# 示例
|
485 |
# dummy_video_for_outputs = gr.Video(visible=False, label='Result')
|
486 |
gr.Examples(
|
487 |
+
# fn=lambda *args: args[-1],
|
488 |
+
fn=process_example,
|
489 |
examples=db_examples.background_conditioned_examples,
|
490 |
+
# inputs=[v_index, input_fg_img, input_bg_img, prompt, bg_source, video_width, video_height, result_video_img],
|
491 |
+
inputs=[v_index, input_fg_img, input_bg_img, prompt, bg_source, result_video_img],
|
492 |
+
outputs=[input_fg, input_bg, result_video],
|
493 |
run_on_click=True, examples_per_page=1024
|
494 |
)
|
495 |
|
496 |
# 启动 Gradio 应用
|
497 |
# block.launch(server_name='0.0.0.0', server_port=10002, share=True)
|
498 |
+
block.launch()
|