File size: 8,676 Bytes
e528a23
 
07df94d
 
e528a23
 
 
 
 
 
77df1b9
f5f5ec2
77df1b9
 
 
 
f5f5ec2
daec925
e528a23
 
daec925
e528a23
f5f5ec2
e528a23
 
07df94d
daec925
 
 
 
f5f5ec2
77df1b9
e528a23
07df94d
77df1b9
07df94d
2ba6c81
77df1b9
07df94d
 
 
 
116a4b2
f5f5ec2
e528a23
daec925
 
 
 
 
 
e528a23
 
 
 
07df94d
 
 
 
e528a23
 
 
 
daec925
e528a23
daec925
e528a23
daec925
 
07df94d
 
 
 
daec925
e528a23
07df94d
e528a23
f5f5ec2
07df94d
72731f5
f5f5ec2
300d330
 
 
 
 
 
f5f5ec2
300d330
 
 
 
 
 
 
 
 
f5f5ec2
77df1b9
c2df102
e528a23
c2df102
e528a23
116a4b2
daec925
07df94d
72731f5
e528a23
116a4b2
daec925
e528a23
07df94d
 
 
 
 
 
 
 
e528a23
c2df102
116a4b2
c2df102
 
300d330
c2df102
07df94d
 
 
72731f5
07df94d
c2df102
 
300d330
c2df102
 
 
300d330
c2df102
07df94d
 
 
72731f5
07df94d
c2df102
 
300d330
e528a23
5ec739c
 
f5f5ec2
 
300d330
5ec739c
4018482
 
 
 
 
 
 
e0dd335
0036851
 
 
 
4018482
 
05b8a95
 
 
 
 
 
a09e8ae
5c91241
27c5c28
05b8a95
 
4018482
e7df04e
 
 
 
 
 
 
 
 
 
 
 
4018482
f5f5ec2
c2df102
d8a08db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5f5ec2
d8a08db
 
 
 
 
 
 
 
 
 
 
 
 
f5f5ec2
d8a08db
 
 
cc5db9a
fd446b6
07df94d
 
 
300d330
07df94d
 
 
 
 
300d330
07df94d
 
 
 
 
300d330
07df94d
 
2df239a
07df94d
 
 
 
f5f5ec2
 
 
2df239a
07df94d
 
 
 
300d330
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
from __future__ import absolute_import, division, print_function

import os
import sys
import cv2
import yaml
import numpy as np
import gradio as gr
from huggingface_hub import hf_hub_download

try:
    import spaces  
    gpu_decorator = spaces.GPU
except Exception:
    gpu_decorator = lambda f: f


PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.append(PROJECT_ROOT)

from networks.models import make  # noqa: E402


WEIGHTS_REPO = "Insta360-Research/DAP-weights"
WEIGHTS_FILE = "model.pth"
CONFIG_PATH = os.path.join(PROJECT_ROOT, "config", "infer.yaml")

model = None
device = "cpu"


import matplotlib

def colorize_depth_fixed(depth_u8: np.ndarray, cmap: str = "Spectral") -> np.ndarray:
    """
    depth_u8: uint8, 0~255
    return: RGB uint8
    """
    disp = depth_u8.astype(np.float32) / 255.0
    colored = matplotlib.colormaps[cmap](disp)[..., :3]
    colored = (colored * 255).astype(np.uint8)
    return np.ascontiguousarray(colored)


def load_model(config_path: str):
    import torch
    import torch.nn as nn

    global device
    device = "cuda" if torch.cuda.is_available() else "cpu"

    with open(config_path, "r") as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    print(f"Downloading weights from HF: {WEIGHTS_REPO}/{WEIGHTS_FILE}")
    model_path = hf_hub_download(
        repo_id=WEIGHTS_REPO,
        filename=WEIGHTS_FILE
    )
    print(f"βœ… Weights downloaded to: {model_path}")

    state = torch.load(model_path, map_location=device)

    m = make(config["model"])
    if any(k.startswith("module") for k in state.keys()):
        m = nn.DataParallel(m)

    m = m.to(device)
    m_state = m.state_dict()
    m.load_state_dict(
        {k: v for k, v in state.items() if k in m_state},
        strict=False
    )
    m.eval()
    print("βœ… Model loaded.")
    return m


model = load_model(CONFIG_PATH)


COLORBAR_DIR = os.path.join(PROJECT_ROOT, "colorbars")
colorbar_100m_color = cv2.imread(os.path.join(COLORBAR_DIR, "colorbar_100m_color.png"))
colorbar_100m_gray = cv2.imread(os.path.join(COLORBAR_DIR, "colorbar_100m_gray.png"))
colorbar_10m_color = cv2.imread(os.path.join(COLORBAR_DIR, "colorbar_10m_color.png"))
colorbar_10m_gray = cv2.imread(os.path.join(COLORBAR_DIR, "colorbar_10m_gray.png"))


if colorbar_100m_color is not None:
    colorbar_100m_color = cv2.cvtColor(colorbar_100m_color, cv2.COLOR_BGR2RGB)
if colorbar_100m_gray is not None:
    colorbar_100m_gray = cv2.cvtColor(colorbar_100m_gray, cv2.COLOR_BGR2RGB)
if colorbar_10m_color is not None:
    colorbar_10m_color = cv2.cvtColor(colorbar_10m_color, cv2.COLOR_BGR2RGB)
if colorbar_10m_gray is not None:
    colorbar_10m_gray = cv2.cvtColor(colorbar_10m_gray, cv2.COLOR_BGR2RGB)


@gpu_decorator
def infer_raw(img_rgb: np.ndarray):
    if img_rgb is None:
        return None

    import torch

    img = img_rgb.astype(np.float32) / 255.0
    tensor = torch.from_numpy(img.transpose(2, 0, 1)).unsqueeze(0).to(device)

    with torch.inference_mode():
        outputs = model(tensor)

        if isinstance(outputs, dict) and "pred_depth" in outputs:
            if "pred_mask" in outputs:
                mask = 1 - outputs["pred_mask"]
                mask = mask > 0.5
                outputs["pred_depth"][~mask] = 1
            pred = outputs["pred_depth"][0].cpu().squeeze().numpy()
        else:
            pred = outputs[0].cpu().squeeze().numpy()

    return pred.astype(np.float32)

def visualize_100m(pred: np.ndarray):
    if pred is None:
        return None, None, None, None, None

    pred_clip = np.clip(pred, 0.0, 1.0)
    depth_gray = (pred_clip * 255).astype(np.uint8)
    depth_color = colorize_depth_fixed(depth_gray, cmap="Spectral")

    npy_path = "/tmp/depth_100m.npy"
    np.save(npy_path, pred)

    return depth_color, depth_gray, npy_path, colorbar_100m_color, colorbar_100m_gray

def visualize_10m(pred: np.ndarray):
    if pred is None:
        return None, None, None, None, None

    pred_clip = np.clip(pred, 0.0, 0.1)
    depth_gray = (pred_clip * 10 * 255).astype(np.uint8)
    depth_color = colorize_depth_fixed(depth_gray, cmap="Spectral")

    npy_path = "/tmp/depth_10m.npy"
    np.save(npy_path, pred)

    return depth_color, depth_gray, npy_path, colorbar_10m_color, colorbar_10m_gray

@gpu_decorator
def infer_and_vis_100m(img_rgb: np.ndarray):
    pred = infer_raw(img_rgb)              
    color, gray, npy, cbar_color, cbar_gray = visualize_100m(pred)  
    return pred, color, gray, npy, cbar_color, cbar_gray

example_paths = [
    "hfdemo/01.jpg",
    "hfdemo/02.jpg",
    "hfdemo/03.jpg",
    "hfdemo/04.jpg",
    "hfdemo/05.jpg",
    "hfdemo/06.jpg",
    "hfdemo/07.jpg",
    "hfdemo/08.jpg",
    "hfdemo/09.jpg",
    "hfdemo/10.jpg",
    "hfdemo/11.jpg",
]

example_gen_paths = [
    "hfdemo/generated_00.jpg",
    "hfdemo/generated_01.jpg",
    "hfdemo/generated_02.jpg",
    "hfdemo/generated_03.jpg",
    "hfdemo/generated_04.jpg",
    "hfdemo/generated_05.jpg",
    "hfdemo/generated_06.jpg",
    "hfdemo/generated_07.jpg",
]

with gr.Blocks() as demo:
    gr.Markdown(
    """
    # πŸŒ€ DAP Depth Prediction Demo
    
    Here are our resources:
    
    - πŸ’» **Code**: [https://github.com/Insta360-Research-Team/DAP](https://github.com/Insta360-Research-Team/DAP)
    - 🌐 **Web Page**: [https://insta360-research-team.github.io/DAP_website/](https://insta360-research-team.github.io/DAP_website/)
    - 🧠 **Pretrained Model**: [https://huggingface.co/Insta360-Research/DAP-weights](https://huggingface.co/Insta360-Research/DAP-weights)
    """
    )
    gr.Markdown("# Official Depth Prediction demo for **[DAP](https://insta360-research-team.github.io/DAP_website/)**")

    raw_depth = gr.State() 

    with gr.Row():
    
        with gr.Column(scale=10):
            inp = gr.Image(
                type="numpy",
                label="Input Image",
                height=360
            )
    
            gr.Markdown("### Examples (click to load)")
            gr.Examples(examples=example_paths, inputs=inp)
    
            gr.Markdown("### Examples from Gemini (click to load)")
            gr.Examples(examples=example_gen_paths, inputs=inp)
    
            btn_infer = gr.Button("Run Inference", variant="primary")
            btn_100m = gr.Button("Visualize (100m)")
            btn_10m = gr.Button("Visualize (10m)")
    
            gr.Markdown(
                """
                <small>
                <b>Visualization range:</b><br>
                β€’ <b>100m</b>: recommended for <b>outdoor</b> scenes<br>
                β€’ <b>10m</b>: recommended for <b>indoor</b> scenes<br>
                (Only affects visualization, not the raw depth output)
                </small>
                """,
                elem_id="vis_hint",
            )
    
        with gr.Column(scale=11):
    
            # -------- Row 1: Color Depth --------
            with gr.Row():
                with gr.Column(scale=10):
                    out_color = gr.Image(
                        label="Depth (Color)",
                        height=260
                    )
    
                with gr.Column(scale=1, min_width=80):
                    colorbar_color = gr.Image(
                        label="Scale",
                        height=260,
                        show_label=False
                    )
    
            with gr.Row():
                with gr.Column(scale=10):
                    out_gray = gr.Image(
                        label="Depth (Gray)",
                        height=260
                    )
    
                with gr.Column(scale=1, min_width=80):
                    colorbar_gray = gr.Image(
                        label="Scale",
                        height=260,
                        show_label=False
                    )
    
            out_npy = gr.File(label="Depth (.npy)")


    btn_infer.click(
        fn=infer_and_vis_100m,
        inputs=inp,
        outputs=[raw_depth, out_color, out_gray, out_npy, colorbar_color, colorbar_gray],
    )

    btn_100m.click(
        fn=visualize_100m,
        inputs=raw_depth,
        outputs=[out_color, out_gray, out_npy, colorbar_color, colorbar_gray],
    )

    btn_10m.click(
        fn=visualize_10m,
        inputs=raw_depth,
        outputs=[out_color, out_gray, out_npy, colorbar_color, colorbar_gray],
    )


if __name__ == "__main__":
    host = os.environ.get("HOST", "0.0.0.0")
    port = int(os.environ.get("PORT", "7860"))

    demo.queue(
        max_size=32,         
        default_concurrency_limit=1,  
    ).launch(
        server_name=host,
        server_port=port,
        ssr_mode=False,
        show_error=True,
    )