lemonaddie commited on
Commit
fc928e3
1 Parent(s): 7091858

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +124 -258
app1.py CHANGED
@@ -1,10 +1,9 @@
1
- import spaces
2
  import functools
3
  import os
4
  import shutil
5
  import sys
6
-
7
  import git
 
8
  import gradio as gr
9
  import numpy as np
10
  import torch as torch
@@ -12,283 +11,150 @@ from PIL import Image
12
 
13
  from gradio_imageslider import ImageSlider
14
 
15
- @spaces.GPU
16
- def process(
17
- pipe,
18
- path_input,
19
- ensemble_size,
20
- denoise_steps,
21
- processing_res,
22
- domain,
23
- normal_out_vis=None,
24
- path_out_fp32=None,
25
- path_out_vis=None,
26
 
27
- ):
28
- if path_out_vis is not None:
29
- return (
30
- [normal_out_vis, path_out_vis],
31
- [normal_out_vis, path_out_fp32, path_out_vis],
32
- )
33
 
34
- input_image = Image.open(path_input)
35
 
36
- # pipe_out = pipe(
37
- # input_image,
38
- # ensemble_size=ensemble_size,
39
- # denoising_steps=denoise_steps,
40
- # processing_res=processing_res,
41
- # domain=domain,
42
- # batch_size=1 if processing_res == 0 else 0,
43
- # show_progress_bar=True,
44
- # )
45
 
 
 
 
 
 
 
 
 
 
 
 
46
  pipe_out = pipe(
47
- input_image,
48
- denoising_steps=3,
49
- ensemble_size=1,
50
- processing_res=768,
51
  batch_size=0,
52
- guidance_scale=3,
53
- domain="indoor",
54
  show_progress_bar=True,
55
  )
56
 
57
- depth_pred = pipe_out.depth_np
58
  depth_colored = pipe_out.depth_colored
59
  normal_colored = pipe_out.normal_colored
60
- depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
61
-
62
- path_output_dir = os.path.splitext(path_input)[0] + "_output"
63
- os.makedirs(path_output_dir, exist_ok=True)
64
 
65
- name_base = os.path.splitext(os.path.basename(path_input))[0]
66
- path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
67
- normal_out_vis = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
68
- path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")
69
 
70
- #np.save(path_out_fp32, depth_pred)
71
- #Image.fromarray(normal_out_vis).save(normal_out_vis)
72
- depth_colored.save(path_out_vis)
73
 
74
- return (
75
- [normal_out_vis, path_out_vis],
76
- [normal_out_vis, path_out_fp32, path_out_vis],
77
- )
78
 
79
 
80
- def run_demo_server(pipe):
81
- process_pipe = functools.partial(process, pipe)
82
- os.environ["GRADIO_ALLOW_FLAGGING"] = "never"
83
-
84
- with gr.Blocks(
85
- analytics_enabled=False,
86
- title="Marigold Depth Estimation",
87
- css="""
88
- #download {
89
- height: 118px;
90
- }
91
- .slider .inner {
92
- width: 5px;
93
- background: #FFF;
94
- }
95
- .viewport {
96
- aspect-ratio: 4/3;
97
- }
98
- """,
99
- ) as demo:
100
- gr.Markdown(
101
- """
102
- <h1 align="center">GeoWizard</h1>
103
- <p align="center">
104
- <a title="Website" href="https://fuxiao0719.github.io/projects/geowizard/" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
105
- <img src="https://www.obukhov.ai/img/badges/badge-website.svg">
106
- </a>
107
- <a title="arXiv" href="https://arxiv.org/abs/2403.12013" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
108
- <img src="https://www.obukhov.ai/img/badges/badge-pdf.svg">
109
- </a>
110
- <a title="Github" href="https://github.com/fuxiao0719/GeoWizard" target="_blank" rel="noopener noreferrer" style="display: inline-block;">
111
- <img src="https://img.shields.io/github/stars/fuxiao0719/GeoWizard" alt="badge-github-stars">
112
- </a>
113
- </p>
114
- <p align="justify">
115
- GeoWizard is a Wizard who spells 3D geometry from a single image.
116
- Upload your image into the <b>left</b> side.
117
- </p>
118
- """
119
- )
120
 
 
121
  with gr.Row():
122
- with gr.Column():
123
- input_image = gr.Image(
124
- label="Input Image",
125
- type="filepath",
126
- )
127
- with gr.Accordion("Advanced options", open=False):
128
- ensemble_size = gr.Slider(
129
- label="Ensemble size",
130
- minimum=1,
131
- maximum=20,
132
- step=1,
133
- value=1,
134
- )
135
- denoise_steps = gr.Slider(
136
- label="Number of denoising steps",
137
- minimum=1,
138
- maximum=20,
139
- step=1,
140
- value=10,
141
- )
142
- processing_res = gr.Radio(
143
- [
144
- ("Native", 0),
145
- ("Recommended", 768),
146
- ],
147
- label="Processing resolution",
148
- value=768,
149
- )
150
- domain = gr.Radio(
151
- [
152
- ("indoor", "indoor"),
153
- ("outdoor", "outdoor"),
154
- ("object", "object"),
155
- ],
156
- label="scene type",
157
- value='indoor',
158
- )
159
- input_output_16bit = gr.File(
160
- label="Predicted depth (16-bit)",
161
- visible=False,
162
- )
163
- input_output_fp32 = gr.File(
164
- label="Predicted depth (32-bit)",
165
- visible=False,
166
- )
167
- input_output_vis = gr.File(
168
- label="Predicted depth (red-near, blue-far)",
169
- visible=False,
170
- )
171
- with gr.Row():
172
- submit_btn = gr.Button(value="Compute Depth", variant="primary")
173
- clear_btn = gr.Button(value="Clear")
174
- with gr.Column():
175
- output_slider = ImageSlider(
176
- label="Predicted depth (red-near, blue-far)",
177
- type="filepath",
178
- show_download_button=True,
179
- show_share_button=True,
180
- interactive=False,
181
- elem_classes="slider",
182
- position=0.25,
183
- )
184
- files = gr.Files(
185
- label="Depth outputs",
186
- elem_id="download",
187
- interactive=False,
188
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
- blocks_settings_depth = [ensemble_size, denoise_steps, processing_res, domain]
191
- blocks_settings = blocks_settings_depth
192
- map_id_to_default = {b._id: b.value for b in blocks_settings}
193
-
194
- inputs = [
195
- input_image,
196
- ensemble_size,
197
- denoise_steps,
198
- processing_res,
199
- domain,
200
- input_output_16bit,
201
- input_output_fp32,
202
- input_output_vis,
203
-
204
- ]
205
- outputs = [
206
- submit_btn,
207
- input_image,
208
- output_slider,
209
- files,
210
- ]
211
-
212
- def submit_depth_fn(*args):
213
- out = list(process_pipe(*args))
214
- out = [gr.Button(interactive=False), gr.Image(interactive=False)] + out
215
- return out
216
-
217
- submit_btn.click(
218
- fn=submit_depth_fn,
219
- inputs=inputs,
220
- outputs=outputs,
221
- concurrency_limit=1,
222
- )
223
-
224
-
225
- def clear_fn():
226
- out = []
227
- for b in blocks_settings:
228
- out.append(map_id_to_default[b._id])
229
- out += [
230
- gr.Button(interactive=True),
231
- gr.Button(interactive=True),
232
- gr.Image(value=None, interactive=True),
233
- None, None, None, None, None, None, None,
234
- ]
235
- return out
236
-
237
- clear_btn.click(
238
- fn=clear_fn,
239
- inputs=[],
240
- outputs=blocks_settings + [
241
- submit_btn,
242
- input_image,
243
- input_output_16bit,
244
- input_output_fp32,
245
- input_output_vis,
246
- output_slider,
247
- files,
248
- ],
249
- )
250
-
251
- demo.queue(
252
- api_open=False,
253
- ).launch(
254
- server_name="0.0.0.0",
255
- server_port=7860,
256
- )
257
-
258
-
259
- def main():
260
-
261
- REPO_URL = "https://github.com/lemonaddie/geowizard.git"
262
- CHECKPOINT = "lemonaddie/Geowizard"
263
- REPO_DIR = "geowizard"
264
-
265
- if os.path.isdir(REPO_DIR):
266
- shutil.rmtree(REPO_DIR)
267
-
268
- repo = git.Repo.clone_from(REPO_URL, REPO_DIR)
269
- sys.path.append(os.path.join(os.getcwd(), REPO_DIR))
270
-
271
- from pipeline.depth_normal_pipeline_clip_cfg import DepthNormalEstimationPipeline
272
-
273
- #device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
274
- pipe = DepthNormalEstimationPipeline.from_pretrained(CHECKPOINT)
275
-
276
- try:
277
- import xformers
278
- pipe.enable_xformers_memory_efficient_attention()
279
- except:
280
- pass # run without xformers
281
-
282
- try:
283
- import xformers
284
- pipe.enable_xformers_memory_efficient_attention()
285
- except:
286
- pass # run without xformers
287
 
288
- pipe = pipe.to('cuda')
289
- run_demo_server(pipe)
 
 
 
 
 
 
 
290
 
291
 
292
- if __name__ == "__main__":
293
- main()
294
 
 
 
1
  import functools
2
  import os
3
  import shutil
4
  import sys
 
5
  import git
6
+
7
  import gradio as gr
8
  import numpy as np
9
  import torch as torch
 
11
 
12
  from gradio_imageslider import ImageSlider
13
 
14
+ import spaces
15
+
16
+ REPO_URL = "https://github.com/lemonaddie/geowizard.git"
17
+ CHECKPOINT = "lemonaddie/Geowizard"
18
+ REPO_DIR = "geowizard"
19
+
20
+ if os.path.isdir(REPO_DIR):
21
+ shutil.rmtree(REPO_DIR)
 
 
 
22
 
23
+ repo = git.Repo.clone_from(REPO_URL, REPO_DIR)
24
+ sys.path.append(os.path.join(os.getcwd(), REPO_DIR))
 
 
 
 
25
 
26
+ from pipeline.depth_normal_pipeline_clip_cfg import DepthNormalEstimationPipeline
27
 
28
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29
+ pipe = DepthNormalEstimationPipeline.from_pretrained(CHECKPOINT)
30
+
31
+ try:
32
+ import xformers
33
+ pipe.enable_xformers_memory_efficient_attention()
34
+ except:
35
+ pass # run without xformers
 
36
 
37
+ pipe = pipe.to(device)
38
+ #run_demo_server(pipe)
39
+
40
+ @spaces.GPU
41
+ def depth_normal(img,
42
+ denoising_steps,
43
+ ensemble_size,
44
+ processing_res,
45
+ guidance_scale,
46
+ domain):
47
+ img = img.resize((processing_res, processing_res), Image.Resampling.LANCZOS)
48
  pipe_out = pipe(
49
+ img,
50
+ denoising_steps=denoising_steps,
51
+ ensemble_size=ensemble_size,
52
+ processing_res=processing_res,
53
  batch_size=0,
54
+ guidance_scale=guidance_scale,
55
+ domain=domain,
56
  show_progress_bar=True,
57
  )
58
 
 
59
  depth_colored = pipe_out.depth_colored
60
  normal_colored = pipe_out.normal_colored
61
+
62
+ return depth_colored, normal_colored
 
 
63
 
 
 
 
 
64
 
 
 
 
65
 
66
+ def run_demo():
 
 
 
67
 
68
 
69
+ custom_theme = gr.themes.Soft(primary_hue="blue").set(
70
+ button_secondary_background_fill="*neutral_100",
71
+ button_secondary_background_fill_hover="*neutral_200")
72
+ custom_css = '''#disp_image {
73
+ text-align: center; /* Horizontally center the content */
74
+ }'''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ with gr.Blocks(title=_TITLE, theme=custom_theme, css=custom_css) as demo:
77
  with gr.Row():
78
+ with gr.Column(scale=1):
79
+ gr.Markdown('# ' + _TITLE)
80
+ gr.Markdown(_DESCRIPTION)
81
+ with gr.Row(variant='panel'):
82
+ with gr.Column(scale=1):
83
+ input_image = gr.Image(type='pil', image_mode='RGBA', height=320, label='Input image', tool=None)
84
+
85
+ example_folder = os.path.join(os.path.dirname(__file__), "./files")
86
+ example_fns = [os.path.join(example_folder, example) for example in os.listdir(example_folder)]
87
+ gr.Examples(
88
+ examples=example_fns,
89
+ inputs=[input_image],
90
+ # outputs=[input_image],
91
+ cache_examples=False,
92
+ label='Examples (click one of the images below to start)',
93
+ examples_per_page=30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  )
95
+ with gr.Column(scale=1):
96
+
97
+ with gr.Accordion('Advanced options', open=True):
98
+ with gr.Row():
99
+
100
+ domain = gr.Radio(
101
+ [
102
+ ("Outdoor", "outdoor"),
103
+ ("Indoor", "indoor"),
104
+ ("Object", "object"),
105
+ ],
106
+ label="Data Domain",
107
+ value="indoor",
108
+ )
109
+ guidance_scale = gr.Slider(
110
+ label="Classifier Free Guidance Scale",
111
+ minimum=1,
112
+ maximum=5,
113
+ step=1,
114
+ value=3,
115
+ )
116
+ denoise_steps = gr.Slider(
117
+ label="Number of denoising steps",
118
+ minimum=1,
119
+ maximum=20,
120
+ step=1,
121
+ value=10,
122
+ )
123
+ ensemble_size = gr.Slider(
124
+ label="Ensemble size",
125
+ minimum=1,
126
+ maximum=15,
127
+ step=1,
128
+ value=1,
129
+ )
130
+ processing_res = gr.Radio(
131
+ [
132
+ ("Native", 0),
133
+ ("Recommended", 768),
134
+ ],
135
+ label="Processing resolution",
136
+ value=768,
137
+ )
138
+
139
+
140
+ run_btn = gr.Button('Generate', variant='primary', interactive=True)
141
+ with gr.Row():
142
+ depth = gr.Image(interactive=False, height=384, show_label=False)
143
+ with gr.Row():
144
+ normal = gr.Image(interactive=False, height=384, show_label=False)
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ run_btn.success(fn=partial(depth_normal),
148
+ inputs=[input_image, denoising_steps,
149
+ ensemble_size,
150
+ processing_res,
151
+ guidance_scale,
152
+ domain],
153
+ outputs=[depth, normal]
154
+ )
155
+ demo.queue().launch(share=True, max_threads=80)
156
 
157
 
158
+ if __name__ == '__main__':
159
+ fire.Fire(run_demo)
160