File size: 9,052 Bytes
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea3f8a
7c4a89c
 
 
 
 
 
0243d32
93b7099
7c4a89c
 
93b7099
7c4a89c
 
 
3b130d8
 
7c4a89c
 
 
 
2038182
 
 
 
 
 
 
 
 
 
7c4a89c
 
045bc76
2038182
 
 
 
 
7c4a89c
 
 
 
 
93b7099
7c4a89c
 
 
 
 
 
 
93b7099
7c4a89c
 
a4cfff6
 
7c4a89c
 
 
93b7099
 
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2038182
7c4a89c
2038182
7c4a89c
 
2038182
7c4a89c
 
2038182
 
7c4a89c
 
 
a4cfff6
d1f3024
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6837c13
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0243d32
 
 
 
 
 
 
 
 
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
768afe4
0243d32
7c4a89c
 
 
 
 
 
 
0243d32
7c4a89c
 
 
0243d32
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6837c13
7c4a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
6837c13
7c4a89c
 
 
 
 
 
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
import spaces
import functools
import os
import shutil
import sys

import git
import gradio as gr
import numpy as np
import torch as torch
from PIL import Image

from gradio_imageslider import ImageSlider

@spaces.GPU
def process(
    pipe,
    path_input,
    ensemble_size,
    denoise_steps,
    processing_res,
    domain,
    normal_out_vis=None,
    path_out_fp32=None,
    path_out_vis=None,
    
):
    if path_out_vis is not None:
        return (
            [normal_out_vis, path_out_vis],
            [normal_out_vis, path_out_fp32, path_out_vis],
        )

    input_image = Image.open(path_input)

    # pipe_out = pipe(
    #     input_image,
    #     ensemble_size=ensemble_size,
    #     denoising_steps=denoise_steps,
    #     processing_res=processing_res,
    #     domain=domain,
    #     batch_size=1 if processing_res == 0 else 0,
    #     show_progress_bar=True,
    # )

    pipe_out = pipe(
        input_image,
        denoising_steps=3,
        ensemble_size=1,
        processing_res=768,
        batch_size=0,
        guidance_scale=3,
        domain="indoor",
        show_progress_bar=True,
    )

    depth_pred = pipe_out.depth_np
    depth_colored = pipe_out.depth_colored
    normal_colored = pipe_out.normal_colored
    depth_16bit = (depth_pred * 65535.0).astype(np.uint16)

    path_output_dir = os.path.splitext(path_input)[0] + "_output"
    os.makedirs(path_output_dir, exist_ok=True)

    name_base = os.path.splitext(os.path.basename(path_input))[0]
    path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
    normal_out_vis = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
    path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")

    #np.save(path_out_fp32, depth_pred)
    #Image.fromarray(normal_out_vis).save(normal_out_vis)
    depth_colored.save(path_out_vis)

    return (
        [normal_out_vis, path_out_vis],
        [normal_out_vis, path_out_fp32, path_out_vis],
    )


def run_demo_server(pipe):
    process_pipe = functools.partial(process, pipe)
    os.environ["GRADIO_ALLOW_FLAGGING"] = "never"

    with gr.Blocks(
        analytics_enabled=False,
        title="Marigold Depth Estimation",
        css="""
            #download {
                height: 118px;
            }
            .slider .inner {
                width: 5px;
                background: #FFF;
            }
            .viewport {
                aspect-ratio: 4/3;
            }
        """,
    ) as demo:
        gr.Markdown(
            """
            <h1 align="center">GeoWizard</h1>
            <p align="center">
            <a title="Website" href="https://fuxiao0719.github.io/projects/geowizard/" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://www.obukhov.ai/img/badges/badge-website.svg">
            </a>
            <a title="arXiv" href="https://arxiv.org/abs/2403.12013" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://www.obukhov.ai/img/badges/badge-pdf.svg">
            </a>
            <a title="Github" href="https://github.com/fuxiao0719/GeoWizard" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
                <img src="https://img.shields.io/github/stars/fuxiao0719/GeoWizard" alt="badge-github-stars">
            </a>
            </p>
            <p align="justify">
                GeoWizard is a Wizard who spells 3D geometry from a single image. 
                Upload your image into the <b>left</b> side. 
            </p>
        """
        )

        with gr.Row():
            with gr.Column():
                input_image = gr.Image(
                    label="Input Image",
                    type="filepath",
                )
                with gr.Accordion("Advanced options", open=False):
                    ensemble_size = gr.Slider(
                        label="Ensemble size",
                        minimum=1,
                        maximum=20,
                        step=1,
                        value=1,
                    )
                    denoise_steps = gr.Slider(
                        label="Number of denoising steps",
                        minimum=1,
                        maximum=20,
                        step=1,
                        value=10,
                    )
                    processing_res = gr.Radio(
                        [
                            ("Native", 0),
                            ("Recommended", 768),
                        ],
                        label="Processing resolution",
                        value=768,
                    )
                    domain = gr.Radio(
                        [
                            ("indoor", "indoor"),
                            ("outdoor", "outdoor"),
                            ("object", "object"),
                        ],
                        label="scene type",
                        value='indoor',
                    )
                input_output_16bit = gr.File(
                    label="Predicted depth (16-bit)",
                    visible=False,
                )
                input_output_fp32 = gr.File(
                    label="Predicted depth (32-bit)",
                    visible=False,
                )
                input_output_vis = gr.File(
                    label="Predicted depth (red-near, blue-far)",
                    visible=False,
                )
                with gr.Row():
                    submit_btn = gr.Button(value="Compute Depth", variant="primary")
                    clear_btn = gr.Button(value="Clear")
            with gr.Column():
                output_slider = ImageSlider(
                    label="Predicted depth (red-near, blue-far)",
                    type="filepath",
                    show_download_button=True,
                    show_share_button=True,
                    interactive=False,
                    elem_classes="slider",
                    position=0.25,
                )
                files = gr.Files(
                    label="Depth outputs",
                    elem_id="download",
                    interactive=False,
                )

        blocks_settings_depth = [ensemble_size, denoise_steps, processing_res, domain]
        blocks_settings = blocks_settings_depth
        map_id_to_default = {b._id: b.value for b in blocks_settings}

        inputs = [
            input_image,
            ensemble_size,
            denoise_steps,
            processing_res,
            domain,
            input_output_16bit,
            input_output_fp32,
            input_output_vis,

        ]
        outputs = [
            submit_btn,
            input_image,
            output_slider,
            files,
        ]

        def submit_depth_fn(*args):
            out = list(process_pipe(*args))
            out = [gr.Button(interactive=False), gr.Image(interactive=False)] + out
            return out

        submit_btn.click(
            fn=submit_depth_fn,
            inputs=inputs,
            outputs=outputs,
            concurrency_limit=1,
        )


        def clear_fn():
            out = []
            for b in blocks_settings:
                out.append(map_id_to_default[b._id])
            out += [
                gr.Button(interactive=True),
                gr.Button(interactive=True),
                gr.Image(value=None, interactive=True),
                None, None, None, None, None, None, None,
            ]
            return out

        clear_btn.click(
            fn=clear_fn,
            inputs=[],
            outputs=blocks_settings + [
                submit_btn,
                input_image,
                input_output_16bit,
                input_output_fp32,
                input_output_vis,
                output_slider,
                files,
            ],
        )

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


def main():

    REPO_URL = "https://github.com/lemonaddie/geowizard.git"
    CHECKPOINT = "lemonaddie/Geowizard"
    REPO_DIR = "geowizard"
    
    if os.path.isdir(REPO_DIR):
        shutil.rmtree(REPO_DIR)
    
    repo = git.Repo.clone_from(REPO_URL, REPO_DIR)
    sys.path.append(os.path.join(os.getcwd(), REPO_DIR))

    from pipeline.depth_normal_pipeline_clip_cfg import DepthNormalEstimationPipeline

    #device = torch.device("cuda" if torch.cuda.is_available() else "cpu")  
    pipe = DepthNormalEstimationPipeline.from_pretrained(CHECKPOINT)
    
    try:
        import xformers
        pipe.enable_xformers_memory_efficient_attention()
    except:
        pass  # run without xformers

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

    pipe = pipe.to('cuda')
    run_demo_server(pipe)


if __name__ == "__main__":
    main()