File size: 13,934 Bytes
02f6d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50e5356
02f6d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78c99d1
02f6d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# MIT License

# Copyright (c) 2024 Jiahao Shao

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import functools
import os
import zipfile
import tempfile
from io import BytesIO

import spaces
import gradio as gr
import numpy as np
import torch as torch
from PIL import Image
from tqdm import tqdm
import mediapy as media

from huggingface_hub import login

from chronodepth_pipeline import ChronoDepthPipeline
from gradio_patches.examples import Examples

default_seed = 2024

default_num_inference_steps = 5
default_num_frames = 10
default_window_size = 9
default_video_processing_resolution = 768
default_video_out_max_frames = 80
default_decode_chunk_size = 10

def process_video(
    pipe,
    path_input,
    num_inference_steps=default_num_inference_steps,
    num_frames=default_num_frames,
    window_size=default_window_size,
    out_max_frames=default_video_out_max_frames,
    progress=gr.Progress(),
):
    if path_input is None:
        raise gr.Error(
            "Missing video in the first pane: upload a file or use one from the gallery below."
        )

    name_base, name_ext = os.path.splitext(os.path.basename(path_input))
    print(f"Processing video {name_base}{name_ext}")

    path_output_dir = tempfile.mkdtemp()
    path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.mp4")
    path_out_16bit = os.path.join(path_output_dir, f"{name_base}_depth_16bit.zip")

    generator = torch.Generator(device=pipe.device).manual_seed(default_seed)

    import time
    start_time = time.time()
    zipf = None
    try:
        if window_size is None or window_size == num_frames:
            inpaint_inference = False
        else:
            inpaint_inference = True
        data_ls = []
        video_data = media.read_video(path_input)
        video_length = len(video_data)
        fps = video_data.metadata.fps

        duration_sec = video_length / fps

        out_duration_sec = out_max_frames / fps
        if duration_sec > out_duration_sec:
            gr.Warning(
                f"Only the first ~{int(out_duration_sec)} seconds will be processed; "
                f"use alternative setups such as ChronoDepth on github for full processing"
            )
            video_length = out_max_frames

        for i in tqdm(range(video_length-num_frames+1)):
            is_first_clip = i == 0
            is_last_clip = i == video_length - num_frames
            is_new_clip = (
                (inpaint_inference and i % window_size == 0)
                or (inpaint_inference == False and i % num_frames == 0)
            )
            if is_first_clip or is_last_clip or is_new_clip:
                data_ls.append(np.array(video_data[i: i+num_frames])) # [t, H, W, 3]

        zipf = zipfile.ZipFile(path_out_16bit, "w", zipfile.ZIP_DEFLATED)

        depth_colored_pred = []
        depth_pred = []
        # -------------------- Inference and saving --------------------
        with torch.no_grad():
            for iter, batch in enumerate(tqdm(data_ls)):
                rgb_int = batch
                input_images = [Image.fromarray(rgb_int[i]) for i in range(num_frames)]

                # Predict depth
                if iter == 0: # First clip
                    pipe_out = pipe(
                        input_images,
                        num_frames=len(input_images),
                        num_inference_steps=num_inference_steps,
                        decode_chunk_size=default_decode_chunk_size,
                        motion_bucket_id=127,
                        fps=7,
                        noise_aug_strength=0.0,
                        generator=generator,
                    )
                elif inpaint_inference and (iter == len(data_ls) - 1): # temporal inpaint inference for last clip
                    last_window_size = window_size if video_length%window_size == 0 else video_length%window_size
                    pipe_out = pipe(
                        input_images,
                        num_frames=num_frames,
                        num_inference_steps=num_inference_steps,
                        decode_chunk_size=default_decode_chunk_size,
                        motion_bucket_id=127,
                        fps=7,
                        noise_aug_strength=0.0,
                        generator=generator,
                        depth_pred_last=depth_frames_pred_ts[last_window_size:],
                    )
                elif inpaint_inference and iter > 0: # temporal inpaint inference
                    pipe_out = pipe(
                        input_images,
                        num_frames=num_frames,
                        num_inference_steps=num_inference_steps,
                        decode_chunk_size=default_decode_chunk_size,
                        motion_bucket_id=127,
                        fps=7,
                        noise_aug_strength=0.0,
                        generator=generator,
                        depth_pred_last=depth_frames_pred_ts[window_size:],
                    )
                else: # separate inference
                    pipe_out = pipe(
                        input_images,
                        num_frames=num_frames,
                        num_inference_steps=num_inference_steps,
                        decode_chunk_size=default_decode_chunk_size,
                        motion_bucket_id=127,
                        fps=7,
                        noise_aug_strength=0.0,
                        generator=generator,
                    )

                depth_frames_pred = [pipe_out.depth_np[i] for i in range(num_frames)]

                depth_frames_colored_pred = []
                for i in range(num_frames):
                    depth_frame_colored_pred = np.array(pipe_out.depth_colored[i])
                    depth_frames_colored_pred.append(depth_frame_colored_pred)
                depth_frames_colored_pred = np.stack(depth_frames_colored_pred, axis=0)

                depth_frames_pred = np.stack(depth_frames_pred, axis=0)
                depth_frames_pred_ts = torch.from_numpy(depth_frames_pred).to(pipe.device)
                depth_frames_pred_ts = depth_frames_pred_ts * 2 - 1

                if inpaint_inference == False:
                    if iter == len(data_ls) - 1:
                        last_window_size = num_frames if video_length%num_frames == 0 else video_length%num_frames
                        depth_colored_pred.append(depth_frames_colored_pred[-last_window_size:])
                        depth_pred.append(depth_frames_pred[-last_window_size:])
                    else:
                        depth_colored_pred.append(depth_frames_colored_pred)
                        depth_pred.append(depth_frames_pred)
                else:
                    if iter == 0:
                        depth_colored_pred.append(depth_frames_colored_pred)
                        depth_pred.append(depth_frames_pred)
                    elif iter == len(data_ls) - 1:
                        depth_colored_pred.append(depth_frames_colored_pred[-last_window_size:])
                        depth_pred.append(depth_frames_pred[-last_window_size:])
                    else:
                        depth_colored_pred.append(depth_frames_colored_pred[-window_size:])
                        depth_pred.append(depth_frames_pred[-window_size:])

        depth_colored_pred = np.concatenate(depth_colored_pred, axis=0)
        depth_pred = np.concatenate(depth_pred, axis=0)

        # -------------------- Save results --------------------
        # Save images
        for i in tqdm(range(len(depth_pred))):
            archive_path = os.path.join(
                f"{name_base}_depth_16bit", f"{i:05d}.png"
            )
            img_byte_arr = BytesIO()
            depth_16bit = Image.fromarray((depth_pred[i] * 65535.0).astype(np.uint16))
            depth_16bit.save(img_byte_arr, format="png")
            img_byte_arr.seek(0)
            zipf.writestr(archive_path, img_byte_arr.read())

        # Export to video
        media.write_video(path_out_vis, depth_colored_pred, fps=fps)
    finally:
        if zipf is not None:
            zipf.close()

    end_time = time.time()
    print(f"Processing time: {end_time - start_time} seconds")
    return (
        path_out_vis,
        [path_out_vis, path_out_16bit],
    )


def run_demo_server(pipe):
    process_pipe_video = spaces.GPU(
        functools.partial(process_video, pipe), duration=220
    )
    os.environ["GRADIO_ALLOW_FLAGGING"] = "never"

    with gr.Blocks(
        analytics_enabled=False,
        title="ChronoDepth Video Depth Estimation",
        css="""
            #download {
                height: 118px;
            }
            .slider .inner {
                width: 5px;
                background: #FFF;
            }
            .viewport {
                aspect-ratio: 4/3;
            }
            h1 {
                text-align: center;
                display: block;
            }
            h2 {
                text-align: center;
                display: block;
            }
            h3 {
                text-align: center;
                display: block;
            }
        """,
    ) as demo:
        gr.Markdown(
            """
            # ChronoDepth Video Depth Estimation

            <p align="center">
            <a title="Website" href="https://jhaoshao.github.io/ChronoDepth/" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://img.shields.io/website?url=https%3A%2F%2Fjhaoshao.github.io%2FChronoDepth%2F&up_message=ChronoDepth&up_color=blue&style=flat&logo=timescale&logoColor=%23FFDC0F">
            </a>
            <a title="arXiv" href="https://arxiv.org/abs/2312.02145" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://img.shields.io/badge/arXiv-PDF-b31b1b">
            </a>
            <a title="Github" href="https://github.com/jhaoshao/ChronoDepth" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://img.shields.io/github/stars/jhaoshao/ChronoDepth?label=GitHub%20%E2%98%85&logo=github&color=C8C" alt="badge-github-stars">
            </a>
            </p>

            ChronoDepth is the state-of-the-art video depth estimator for videos in the wild. 
            Upload your video and have a try!<br>
            We set denoising steps to 5, number of frames for each video clip to 10, and overlap between clips to 1.

        """
        )

        with gr.Row():
            with gr.Column():
                video_input = gr.Video(
                    label="Input Video",
                    sources=["upload"],
                )
                with gr.Row():
                    video_submit_btn = gr.Button(
                        value="Compute Depth", variant="primary"
                    )
                    video_reset_btn = gr.Button(value="Reset")
            with gr.Column():
                video_output_video = gr.Video(
                    label="Output video depth (red-near, blue-far)",
                    interactive=False,
                )
                video_output_files = gr.Files(
                    label="Depth outputs",
                    elem_id="download",
                    interactive=False,
                )
        Examples(
            fn=process_pipe_video,
            examples=[
                os.path.join("files", name)
                for name in [
                    "sora_e2.mp4",
                    "sora_1758192960116785459.mp4",
                ]
            ],
            inputs=[video_input],
            outputs=[video_output_video, video_output_files],
            cache_examples=True,
            directory_name="examples_video",
        )

        video_submit_btn.click(
            fn=process_pipe_video,
            inputs=[video_input],
            outputs=[video_output_video, video_output_files],
            concurrency_limit=1,
        )

        video_reset_btn.click(
            fn=lambda: (None, None, None),
            inputs=[],
            outputs=[video_input, video_output_video],
            concurrency_limit=1,
        )

        demo.queue(
            api_open=False,
        ).launch(
            server_name="0.0.0.0",
            server_port=7860,
        )


def main():
    CHECKPOINT = "jhshao/ChronoDepth"

    if "HF_TOKEN_LOGIN" in os.environ:
        login(token=os.environ["HF_TOKEN_LOGIN"])

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Running on device: {device}")
    pipe = ChronoDepthPipeline.from_pretrained(CHECKPOINT)
    try:
        import xformers

        pipe.enable_xformers_memory_efficient_attention()
    except:
        pass  # run without xformers

    pipe = pipe.to(device)
    run_demo_server(pipe)


if __name__ == "__main__":
    main()