File size: 17,080 Bytes
460fe75
0f56e8b
 
 
 
 
 
 
 
 
 
12e9d98
0f56e8b
3fc9d60
 
 
 
 
 
 
 
0f56e8b
 
 
 
 
 
 
 
12e9d98
3fc9d60
 
 
 
 
 
 
12e9d98
0f56e8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561f0f8
0f56e8b
f10dbcb
 
 
fb3c58e
2598957
f10dbcb
 
 
 
0f56e8b
f10dbcb
 
 
561f0f8
f10dbcb
 
 
 
 
 
 
 
0f56e8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc9d60
0f56e8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc9d60
0f56e8b
 
fb3c58e
 
 
3fc9d60
 
fb3c58e
 
 
561f0f8
fb3c58e
 
 
 
 
 
 
0f56e8b
 
 
 
 
12e9d98
 
0f56e8b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import spaces #fixme
import os
import torch
import sys
from demo import TrajCrafter
import random
import gradio as gr
import random
from inference import get_parser
from datetime import datetime
import argparse
from huggingface_hub import snapshot_download


examples = [
    ['examples/example_01.mp4',2,1,'0; -30; 0.5; -2; 0',30,43],
    ['examples/example_02.mp4',2,1,'0; 30; -0.4; 2; 0',30,43],
    ['examples/example_03.mp4',2,1,'20; 40; 0.5; 2; 0',30,43],
    ['examples/example_04.mp4',2,1,'0; -50; 0.3; 0; 0',30,43],
    ['examples/example_05.mp4',2,1,'0; -35; 0.4; 0; 0',30,43],
]

traj_examples = [
    ['20; -30; 0.3; 0; 0'],
    ['0; 0; -0.3; -2; 2'],
]

max_seed = 2 ** 31

os.makedirs('./checkpoints/',exist_ok=True)
# def download_model():
#     snapshot_download(repo_id="TrajectoryCrafter/TrajectoryCrafter", local_dir="checkpoints/TrajectoryCrafter", local_dir_use_symlinks=False)
#     snapshot_download(repo_id="tencent/DepthCrafter", local_dir="checkpoints/DepthCrafter", local_dir_use_symlinks=False)
#     snapshot_download(repo_id="stabilityai/stable-video-diffusion-img2vid", local_dir="checkpoints/stable-video-diffusion-img2vid", local_dir_use_symlinks=False)
#     snapshot_download(repo_id="alibaba-pai/CogVideoX-Fun-V1.1-5b-InP", local_dir="checkpoints/CogVideoX-Fun-V1.1-5b-InP", local_dir_use_symlinks=False)
#     snapshot_download(repo_id="Salesforce/blip2-opt-2.7b", local_dir="checkpoints/blip2-opt-2.7b", local_dir_use_symlinks=False)
# download_model() #fixme

parser = get_parser() # infer_config.py
opts = parser.parse_args() # default device: 'cuda:0'
opts.weight_dtype = torch.bfloat16
tmp = datetime.now().strftime("%Y%m%d_%H%M")
opts.save_dir = f'./experiments/gradio_{tmp}'
os.makedirs(opts.save_dir,exist_ok=True)
test_tensor = torch.Tensor([0]).cuda()
opts.device = str(test_tensor.device)

CAMERA_MOTION_MODE = ["Basic Camera Trajectory", "Custom Camera Trajectory"]

def show_traj(mode):
    if mode == 'Orbit Left':
        return gr.update(value='0; -30; 0; 0; 0',visible=True),gr.update(visible=False)
    elif mode == 'Orbit Right':
        return gr.update(value='0; 30; 0; 0; 0',visible=True),gr.update(visible=False)
    elif mode == 'Orbit Up':
        return gr.update(value='30; 0; 0; 0; 0',visible=True),gr.update(visible=False)
    elif mode == 'Orbit Down':
        return gr.update(value='-20; 0; 0; 0; 0',visible=True), gr.update(visible=False)
    if mode == 'Pan Left':
        return gr.update(value='0; 0; 0; -2; 0',visible=True),gr.update(visible=False)
    elif mode == 'Pan Right':
        return gr.update(value='0; 0; 0; 2; 0',visible=True),gr.update(visible=False)
    elif mode == 'Pan Up':
        return gr.update(value='0; 0; 0; 0; 2',visible=True),gr.update(visible=False)
    elif mode == 'Pan Down':
        return gr.update(value='0; 0; 0; 0; -2',visible=True), gr.update(visible=False)    
    elif mode == 'Zoom in':
        return gr.update(value='0; 0; 0.5; 0; 0',visible=True), gr.update(visible=False)
    elif mode == 'Zoom out':
        return gr.update(value='0; 0; -0.5; 0; 0',visible=True), gr.update(visible=False)
    elif mode == 'Customize':
        return gr.update(value='0; 0; 0; 0; 0',visible=True), gr.update(visible=True)
    elif mode == 'Reset':
        return gr.update(value='0; 0; 0; 0; 0',visible=False), gr.update(visible=False)

def trajcrafter_demo(opts):
    # css = """#input_img {max-width: 1024px !important} #output_vid {max-width: 1024px; max-height:576px} #random_button {max-width: 100px !important}"""
    css = """
    #input_img {max-width: 1024px !important} 
    #output_vid {max-width: 1024px; max-height:576px} 
    #random_button {max-width: 100px !important}
    .generate-btn {
        background: linear-gradient(45deg, #2196F3, #1976D2) !important;
        border: none !important;
        color: white !important;
        font-weight: bold !important;
        box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important;
    }
    .generate-btn:hover {
        background: linear-gradient(45deg, #1976D2, #1565C0) !important;
        box-shadow: 0 4px 8px rgba(0,0,0,0.3) !important;
    }
    """
    image2video = TrajCrafter(opts,gradio=True)
    image2video.run_gradio = spaces.GPU(image2video.run_gradio, duration=300) # fixme
    with gr.Blocks(analytics_enabled=False, css=css) as trajcrafter_iface:
        # gr.Markdown("""
        #     <div align='center'>
        #         <h1>TrajectoryCrafter: Redirecting View Trajectory for Monocular Videos via Diffusion Models</h1>
        #         <a style='font-size:18px;color: #E7CCCC' href='https://github.com/TrajectoryCrafter/TrajectoryCrafter'>[Github]</a>
        #         <a style='font-size:18px;color: #000000' href='https://arxiv.org/abs/2503.05638'>[ArXiv]</a>
        #         <a style='font-size:18px;color: #000000' href='https://trajectorycrafter.github.io/'>[Project Page]</a>
        #         <a style='font-size:18px;color: #000000' href='https://www.youtube.com/watch?v=dQtHFgyrids'>[Video]</a>
        #     </div>
        # """)

        gr.Markdown(
            """
            <div align='center'> 
                <h1> TrajectoryCrafter: Redirecting Camera Trajectory for Monocular Videos via Diffusion Models </span> </h1> \
                <a style='font-size:18px;color: #000000'>If you find TrajectoryCrafter useful, please help ⭐ the </a>\
                <a style='font-size:18px;color: #FF5DB0' href='https://github.com/TrajectoryCrafter/TrajectoryCrafter'>[Github Repo]</a>\
                <a style='font-size:18px;color: #000000'>, which is important to Open-Source projects. Thanks!</a>\
                    <a style='font-size:18px;color: #000000' href='https://arxiv.org/abs/2409.02048'> [ArXiv] </a>\
                    <a style='font-size:18px;color: #000000' href='https://trajectorycrafter.github.io/'> [Project Page] </a> </div>
            """
        )
        

        with gr.Row(equal_height=True):
            with gr.Column():
                # # step 1: 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("<div align='left' style='font-size:18px;color: #000000'>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. <br>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.</div>")
                i2v_input_video = gr.Video(label="Input Video", elem_id="input_video", format="mp4")


            with gr.Column():
                i2v_output_video = gr.Video(label="Generated Video", elem_id="output_vid", autoplay=True,
                                            show_share_button=True)

        with gr.Row():
            with gr.Row():
                i2v_stride = gr.Slider(minimum=1, maximum=3, step=1, elem_id="stride", label="Stride", value=1)
                i2v_center_scale = gr.Slider(minimum=0.1, maximum=2, step=0.1, elem_id="i2v_center_scale",
                                             label="center_scale", value=1)
                i2v_steps = gr.Slider(minimum=1, maximum=50, step=1, elem_id="i2v_steps", label="Sampling steps",
                                      value=30)
                i2v_seed = gr.Slider(label='Random seed', minimum=0, maximum=max_seed, step=1, value=43)
            with gr.Row():
                pan_left = gr.Button(value="Pan Left")
                pan_right = gr.Button(value="Pan Right")
                pan_up = gr.Button(value="Pan Up")
                pan_down = gr.Button(value="Pan Down")
            with gr.Row():
                orbit_left = gr.Button(value="Orbit Left")
                orbit_right = gr.Button(value="Orbit Right")
                orbit_up = gr.Button(value="Orbit Up")
                orbit_down = gr.Button(value="Orbit Down")
            with gr.Row():
                zin = gr.Button(value="Zoom in")
                zout = gr.Button(value="Zoom out")
                custom = gr.Button(value="Customize")
                reset = gr.Button(value="Reset")
            with gr.Column():
                with gr.Row():
                    with gr.Column():
                        i2v_pose = gr.Text(value='0; 0; 0; 0; 0', label="Traget camera pose (theta, phi, r, x, y)",
                                           visible=False)
                        with gr.Column(visible=False) as i2v_egs:
                            gr.Markdown(
                                "<div align='left' style='font-size:18px;color: #000000'>Please refer to <a href='https://github.com/TrajectoryCrafter/TrajectoryCrafter/blob/main/docs/config_help.md' target='_blank'>tutorial</a> for customizing camera trajectory.</div>")
                            gr.Examples(examples=traj_examples,
                                        inputs=[i2v_pose],
                                        )
            with gr.Column():
                i2v_end_btn = gr.Button("Generate video", scale=2, size="lg", variant="primary", elem_classes="generate-btn")


                # with gr.Column():
                #     i2v_input_video = gr.Video(label="Input Video", elem_id="input_video", format="mp4")
                    # 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)
                    #     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=43)                
                # with  gr.Column():
                #     with gr.Row():
                #         left = gr.Button(value = "Left")
                #         right = gr.Button(value = "Right")
                #         up = gr.Button(value = "Up")
                #     with gr.Row(): 
                #         down = gr.Button(value = "Down")                              
                #         zin = gr.Button(value = "Zoom in")
                #         zout = gr.Button(value = "Zoom out")
                #     with gr.Row(): 
                #         custom = gr.Button(value = "Customize")
                #         reset = gr.Button(value = "Reset")


            # step 3 - Generate video
            # with gr.Column():
                # gr.Markdown("---\n## Step 3: Generate video", show_label=False, visible=True)
                # gr.Markdown("<div align='left' style='font-size:18px;color: #000000'> You can reduce the sampling steps for faster inference; try different random seed if the result is not satisfying. </div>")
                # i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
                # i2v_end_btn = gr.Button("Generate video")                    
                # i2v_traj_video = gr.Video(label="Camera Trajectory",elem_id="traj_vid",autoplay=True,show_share_button=True)

            # with  gr.Column(scale=1.5):
                # 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)
                #     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=43)
                # with gr.Row():
                #     pan_left = gr.Button(value = "Pan Left")
                #     pan_right = gr.Button(value = "Pan Right")
                #     pan_up = gr.Button(value = "Pan Up")
                #     pan_down = gr.Button(value = "Pan Down")
                # with gr.Row():
                #     orbit_left = gr.Button(value = "Orbit Left")
                #     orbit_right = gr.Button(value = "Orbit Right")
                #     orbit_up = gr.Button(value = "Orbit Up")
                #     orbit_down = gr.Button(value = "Orbit Down")
                # with gr.Row():
                #     zin = gr.Button(value = "Zoom in")
                #     zout = gr.Button(value = "Zoom out")
                #     custom = gr.Button(value = "Customize")
                #     reset = gr.Button(value = "Reset")
                # with gr.Column():
                #      with gr.Row():
                #         with gr.Column():
                #             i2v_pose = gr.Text(value = '0; 0; 0; 0; 0', label="Traget camera pose (theta, phi, r, x, y)",visible=False)
                #             with gr.Column(visible=False) as i2v_egs:
                #                 gr.Markdown("<div align='left' style='font-size:18px;color: #000000'>Please refer to the <a href='https://github.com/Drexubery/ViewCrafter/blob/main/docs/gradio_tutorial.md' target='_blank'>tutorial</a> for customizing camera trajectory.</div>")
                #                 gr.Examples(examples=traj_examples,
                #                         inputs=[i2v_pose],
                #                     )
                # with gr.Row():
                #     i2v_end_btn = gr.Button("Generate video")
        # step 3 - Generate video
        # with gr.Row():
        #     with gr.Column():  



        i2v_end_btn.click(inputs=[i2v_input_video, i2v_stride, i2v_center_scale, i2v_pose, i2v_steps, i2v_seed],
                        outputs=[i2v_output_video],
                        fn = image2video.run_gradio
        )

        pan_left.click(inputs=[pan_left],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        pan_right.click(inputs=[pan_right],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        pan_up.click(inputs=[pan_up],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        pan_down.click(inputs=[pan_down],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        orbit_left.click(inputs=[orbit_left],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        orbit_right.click(inputs=[orbit_right],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        orbit_up.click(inputs=[orbit_up],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        orbit_down.click(inputs=[orbit_down],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        zin.click(inputs=[zin],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        zout.click(inputs=[zout],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        custom.click(inputs=[custom],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )
        reset.click(inputs=[reset],
                      outputs=[i2v_pose,i2v_egs],
                      fn = show_traj
                      )

        
        gr.Examples(examples=examples,
            # inputs=[i2v_input_video,i2v_stride],
            inputs=[i2v_input_video, i2v_stride, i2v_center_scale, i2v_pose, i2v_steps, i2v_seed],
            # outputs=[i2v_output_video],
            # fn = image2video.run_gradio,
            # cache_examples="lazy",
        ) 
        
        gr.Markdown(
            """
            <span style='font-size:18px;color: #E7CCCC'>Note: 
            For time quota consideration, we set the sampling steps to be more efficient here.
            You may adjust the parameters according to our 
            <a style='font-size:18px;color: #FF5DB0' href='https://github.com/TrajectoryCrafter/TrajectoryCrafter'>[Github Repo]</a>
             for better results if you have enough time quota.
            </span>
            """
        )
        
    return trajcrafter_iface


trajcrafter_iface = trajcrafter_demo(opts)
trajcrafter_iface.queue(max_size=10)
trajcrafter_iface.launch()
# trajcrafter_iface.launch(server_name="0.0.0.0", server_port=12345, debug=True, share=False, max_threads=10)