import os
import torch
import sys
# import spaces #fixme
import random
import gradio as gr
import random
from configs.infer_config import get_parser
from huggingface_hub import hf_hub_download
traj_examples = [
['0 40', '0 0', '0 0'],
['0 -35', '0 0', '0 -0.1'],
['0 -3 -15 -20 -17 -5 0', '0 -2 -5 -10 -8 -5 0 2 5 3 0', '0 0'],
['0 3 10 20 17 10 0', '0 -2 -8 -6 0 2 5 3 0', '0 -0.02 -0.09 -0.16 -0.09 0'],
['0 30', '0 -1 -5 -4 0 1 5 4 0', '0 -0.2'],
]
img_examples = [
['test/images/boy.png',0,1],
['test/images/car.jpeg',5,1],
['test/images/fruit.jpg',5,1],
['test/images/room.png',10,1],
['test/images/castle.png',-4,1],
]
max_seed = 2 ** 31
def download_model():
REPO_ID = 'Drexubery/ViewCrafter_25'
filename_list = ['model.ckpt']
for filename in filename_list:
local_file = os.path.join('./checkpoints/', filename)
if not os.path.exists(local_file):
hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir='./checkpoints/', force_download=True)
# download_model() #fixme
parser = get_parser() # infer_config.py
opts = parser.parse_args() # default device: 'cuda:0'
tmp = str(random.randint(10**(5-1), 10**5 - 1))
opts.save_dir = f'./{tmp}'
os.makedirs(opts.save_dir,exist_ok=True)
test_tensor = torch.Tensor([0]).cuda()
opts.device = str(test_tensor.device)
# opts.config = './configs/inference_pvd_1024_gradio.yaml' #fixme
opts.config = './configs/inference_pvd_1024_local.yaml' #fixme
# # install pytorch3d # fixme
# pyt_version_str=torch.__version__.split("+")[0].replace(".", "")
# version_str="".join([
# f"py3{sys.version_info.minor}_cu",
# torch.version.cuda.replace(".",""),
# f"_pyt{pyt_version_str}"
# ])
# print(version_str)
# os.system(f"{sys.executable} -m pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html")
# os.system("mkdir -p checkpoints/ && wget https://download.europe.naverlabs.com/ComputerVision/DUSt3R/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth -P checkpoints/")
# print(f'>>> System info: {version_str}')
from viewcrafter import ViewCrafter
CAMERA_MOTION_MODE = ["Basic Camera Trajectory", "Custom Camera Trajectory"]
def proceed(mode):
if mode == "Basic Camera Trajectory":
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True)
def viewcrafter_demo(opts):
css = """#input_img {max-width: 1024px !important} #output_vid {max-width: 1024px; max-height:576px} #random_button {max-width: 100px !important}"""
image2video = ViewCrafter(opts, gradio = True)
# image2video.run_traj_basic = spaces.GPU(image2video.run_traj_basic, duration=50) # fixme
# image2video.run_traj = spaces.GPU(image2video.run_traj, duration=50) # fixme
# image2video.run_gen = spaces.GPU(image2video.run_gen, duration=260) # fixme
with gr.Blocks(analytics_enabled=False, css=css) as viewcrafter_iface:
gr.Markdown("
")
with gr.Column():
# # step 0: tutorial
# gr.Markdown("## Step 0: Read tutorial", show_label=False)
# gr.Markdown("Please refer to the tutorial
here for best practice, which includes the cameara system defination and the renderer parameters.
")
# step 2: input an image
gr.Markdown("---\n## Step 1: Input an Image, selet an elevation angle and a center_scale factor", show_label=False, visible=True)
gr.Markdown("1. Estimate an elevation angle that represents the angle at which the image was taken; a value bigger than 0 indicates a top-down view, and it doesn't need to be precise.
2. The origin of the world coordinate system is by default defined at the point cloud corresponding to the center pixel of the input image. You can adjust the position of the origin by modifying center_scale; a value smaller than 1 brings the origin closer to you.
")
with gr.Row(equal_height=True):
with gr.Column(scale=2):
with gr.Row():
i2v_input_image = gr.Image(label="Input Image",elem_id="input_img")
with gr.Row():
i2v_elevation = gr.Slider(minimum=-45, maximum=45, step=1, elem_id="elevation", label="elevation", value=5)
i2v_center_scale = gr.Slider(minimum=0.1, maximum=2, step=0.1, elem_id="i2v_center_scale", label="center_scale", value=1)
gr.Examples(examples=img_examples,
inputs=[i2v_input_image,i2v_elevation,i2v_center_scale],
examples_per_page=6
)
# step 2 - camera trajectory generation
gr.Markdown("---\n## Step 2: Input camera trajectory", show_label=False, visible=True)
gr.Markdown(f"\n - {CAMERA_MOTION_MODE[0]}: Select from 6 basic camera trajectory \
\n - {CAMERA_MOTION_MODE[1]}: Customize complex camera trajectory yourself \
\n - Click `Proceed` to go into next step",
show_label=False, visible=True)
with gr.Row():
camera_mode = gr.Radio(choices=CAMERA_MOTION_MODE, value=CAMERA_MOTION_MODE[0], label="Camera trajectory mode", interactive=True, visible=True)
pro_btn = gr.Button("Proceed")
with gr.Column(visible=False) as ouput1:
gr.Markdown(" Select one cameras trajectory.
")
with gr.Row():
with gr.Column():
left = gr.Button(value = "Left")
right = gr.Button(value = "Right")
up = gr.Button(value = "Up")
down = gr.Button(value = "Down")
zoomin = gr.Button(value = "Zoom in")
zoomout = gr.Button(value = "Zoom out")
with gr.Column():
i2v_traj_video1 = gr.Video(label="Camera Trajectory",elem_id="traj_vid",autoplay=True,show_share_button=True)
with gr.Column(visible=False) as ouput2:
gr.Markdown(" Input a d_phi sequence, a d_theta sequence, and a d_r sequence to generate a camera trajectory. In the sequences, a positive d_phi moves the camera to the right, a negative d_theta moves the camera up, and a negative d_r moves the camera forward. Ensure that each sequence starts with 0 and contains at least two elements (a start and an end). If you upload a new image, remember to conduct this step again.
")
with gr.Row():
with gr.Column():
# camera_mode = gr.Radio(choices=CAMERA_MOTION_MODE, value=CAMERA_MOTION_MODE[0], label="Camera Motion Control Mode", interactive=True, visible=False)
i2v_d_phi2 = gr.Text(label='d_phi sequence')
i2v_d_theta2 = gr.Text(label='d_theta sequence')
i2v_d_r2 = gr.Text(label='d_r sequence')
i2v_traj_btn2 = gr.Button("Generate custom trajectory")
# camera_info = gr.Button(value="Proceed", visible=False)
with gr.Column():
i2v_traj_video2 = gr.Video(label="Camera Trajectory",elem_id="traj_vid",autoplay=True,show_share_button=True)
with gr.Column():
gr.Examples(examples=traj_examples,
inputs=[i2v_d_phi2, i2v_d_theta2, i2v_d_r2],
)
# with gr.Column():
# i2v_traj_btn = gr.Button("Generate trajectory")
# i2v_traj_video = gr.Video(label="Camera Trajectory",elem_id="traj_vid",autoplay=True,show_share_button=True)
# step 3 - Generate video
gr.Markdown("---\n## Step 3: Generate video", show_label=False, visible=True)
gr.Markdown(" You can reduce the sampling steps for faster inference; try different random seed if the result is not satisfying.
")
with gr.Row():
with gr.Column():
i2v_steps = gr.Slider(minimum=1, maximum=50, step=1, elem_id="i2v_steps", label="Sampling steps", value=50)
i2v_seed = gr.Slider(label='Random seed', minimum=0, maximum=max_seed, step=1, value=0)
i2v_end_btn = gr.Button("Generate video")
# with gr.Tab(label='Result'):
with gr.Column():
i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
pro_btn.click(inputs=[camera_mode],
outputs=[ouput1,ouput2],
fn = proceed
)
i2v_traj_btn2.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale, i2v_d_phi2, i2v_d_theta2, i2v_d_r2],
outputs=[i2v_traj_video2],
fn = image2video.run_traj
)
left.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,left],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
right.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,right],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
up.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,up],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
down.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,down],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
zoomin.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,zoomin],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
zoomout.click(inputs=[i2v_input_image, i2v_elevation, i2v_center_scale,zoomout],
outputs=[i2v_traj_video1],
fn = image2video.run_traj_basic
)
i2v_end_btn.click(inputs=[i2v_steps, i2v_seed],
outputs=[i2v_output_video],
fn = image2video.run_gen
)
return viewcrafter_iface
viewcrafter_iface = viewcrafter_demo(opts)
viewcrafter_iface.queue(max_size=10)
# viewcrafter_iface.launch() #fixme
viewcrafter_iface.launch(server_name='11.220.92.96', server_port=80, max_threads=10,debug=True)