lemonaddie commited on
Commit
5610b71
1 Parent(s): 77fa61c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +250 -233
app.py CHANGED
@@ -13,242 +13,259 @@ from gradio_imageslider import ImageSlider
13
 
14
  import spaces
15
 
16
- def process(
17
- pipe,
18
- path_input,
19
- ensemble_size,
20
- denoise_steps,
21
- processing_res,
22
- path_out_16bit=None,
23
- path_out_fp32=None,
24
- path_out_vis=None,
25
- ):
26
-
27
- if path_out_vis is not None:
28
- return (
29
- [path_out_16bit, path_out_vis],
30
- [path_out_16bit, path_out_fp32, path_out_vis],
31
- )
32
-
33
- input_image = Image.open(path_input)
34
-
35
- pipe_out = pipe(
36
- input_image,
37
- denoising_steps=denoise_steps,
38
- ensemble_size=ensemble_size,
39
- processing_res=processing_res,
40
- batch_size=1 if processing_res == 0 else 0,
41
- guidance_scale=3,
42
- domain="indoor",
43
- show_progress_bar=True,
44
- )
45
-
46
- depth_pred = pipe_out.depth_np
47
- depth_colored = pipe_out.depth_colored
48
- depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
49
-
50
- path_output_dir = os.path.splitext(path_input)[0] + "_output"
51
- os.makedirs(path_output_dir, exist_ok=True)
52
-
53
- name_base = os.path.splitext(os.path.basename(path_input))[0]
54
- path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
55
- path_out_16bit = os.path.join(path_output_dir, f"{name_base}_depth_16bit.png")
56
- path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")
57
-
58
- np.save(path_out_fp32, depth_pred)
59
- Image.fromarray(depth_16bit).save(path_out_16bit, mode="I;16")
60
- depth_colored.save(path_out_vis)
61
-
62
- return (
63
- [path_out_16bit, path_out_vis],
64
- [path_out_16bit, path_out_fp32, path_out_vis],
65
- )
66
-
67
 
68
  @spaces.GPU
69
  def run_demo_server(pipe):
70
- process_pipe = functools.partial(process, pipe)
71
- os.environ["GRADIO_ALLOW_FLAGGING"] = "never"
72
-
73
- with gr.Blocks(
74
- analytics_enabled=False,
75
- title="GeoWizard Depth and Normal Estimation",
76
- css="""
77
- #download {
78
- height: 118px;
79
- }
80
- .slider .inner {
81
- width: 5px;
82
- background: #FFF;
83
- }
84
- .viewport {
85
- aspect-ratio: 4/3;
86
- }
87
- """,
88
- ) as demo:
89
- gr.Markdown(
90
- """
91
- <h1 align="center">Geowizard Depth & Normal Estimation</h1>
92
- """
93
- )
94
-
95
- with gr.Row():
96
- with gr.Column():
97
- input_image = gr.Image(
98
- label="Input Image",
99
- type="filepath",
100
- )
101
- with gr.Accordion("Advanced options", open=False):
102
- domain = gr.Radio(
103
- [
104
- ("Outdoor", "outdoor"),
105
- ("Indoor", "indoor"),
106
- ("Object", "object"),
107
- ],
108
- label="Data Domain",
109
- value="indoor",
110
- )
111
- cfg_scale = gr.Slider(
112
- label="Classifier Free Guidance Scale",
113
- minimum=1,
114
- maximum=5,
115
- step=1,
116
- value=3,
117
- )
118
- denoise_steps = gr.Slider(
119
- label="Number of denoising steps",
120
- minimum=1,
121
- maximum=20,
122
- step=1,
123
- value=2,
124
- )
125
- ensemble_size = gr.Slider(
126
- label="Ensemble size",
127
- minimum=1,
128
- maximum=15,
129
- step=1,
130
- value=1,
131
- )
132
- processing_res = gr.Radio(
133
- [
134
- ("Native", 0),
135
- ("Recommended", 768),
136
- ],
137
- label="Processing resolution",
138
- value=768,
139
- )
140
- input_output_16bit = gr.File(
141
- label="Predicted depth (16-bit)",
142
- visible=False,
143
- )
144
- input_output_fp32 = gr.File(
145
- label="Predicted depth (32-bit)",
146
- visible=False,
147
- )
148
- input_output_vis = gr.File(
149
- label="Predicted depth (red-near, blue-far)",
150
- visible=False,
151
- )
152
- with gr.Row():
153
- submit_btn = gr.Button(value="Compute", variant="primary")
154
- clear_btn = gr.Button(value="Clear")
155
- with gr.Column():
156
- output_slider = ImageSlider(
157
- label="Predicted depth (red-near, blue-far)",
158
- type="filepath",
159
- show_download_button=True,
160
- show_share_button=True,
161
- interactive=False,
162
- elem_classes="slider",
163
- position=0.25,
164
- )
165
- files = gr.Files(
166
- label="Depth outputs",
167
- elem_id="download",
168
- interactive=False,
169
- )
170
-
171
- blocks_settings_depth = [ensemble_size, denoise_steps, processing_res]
172
- blocks_settings = blocks_settings_depth
173
- map_id_to_default = {b._id: b.value for b in blocks_settings}
174
-
175
- inputs = [
176
- input_image,
177
- ensemble_size,
178
- denoise_steps,
179
- processing_res,
180
- input_output_16bit,
181
- input_output_fp32,
182
- input_output_vis,
183
- ]
184
- outputs = [
185
- submit_btn,
186
- input_image,
187
- output_slider,
188
- files,
189
- ]
190
-
191
- def submit_depth_fn(*args):
192
- out = list(process_pipe(*args))
193
- out = [gr.Button(interactive=False), gr.Image(interactive=False)] + out
194
- return out
195
-
196
- submit_btn.click(
197
- fn=submit_depth_fn,
198
- inputs=inputs,
199
- outputs=outputs,
200
- concurrency_limit=1,
201
- )
202
-
203
- gr.Examples(
204
- fn=submit_depth_fn,
205
- examples=[
206
- [
207
- "files/bee.jpg",
208
- 10, # ensemble_size
209
- 10, # denoise_steps
210
- 768, # processing_res
211
- "files/bee_depth_16bit.png",
212
- "files/bee_depth_fp32.npy",
213
- "files/bee_depth_colored.png",
214
- ],
215
- ],
216
- inputs=inputs,
217
- outputs=outputs,
218
- cache_examples=True,
219
- )
220
-
221
- def clear_fn():
222
- out = []
223
- for b in blocks_settings:
224
- out.append(map_id_to_default[b._id])
225
- out += [
226
- gr.Button(interactive=True),
227
- gr.Image(value=None, interactive=True),
228
- None, None, None, None, None, None, None,
229
- ]
230
- return out
231
-
232
- clear_btn.click(
233
- fn=clear_fn,
234
- inputs=[],
235
- outputs=blocks_settings + [
236
- submit_btn,
237
- input_image,
238
- input_output_16bit,
239
- input_output_fp32,
240
- input_output_vis,
241
- output_slider,
242
- files,
243
- ],
244
- )
245
-
246
- demo.queue(
247
- api_open=False,
248
- ).launch(
249
- server_name="0.0.0.0",
250
- server_port=7860,
251
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
 
254
  def main():
 
13
 
14
  import spaces
15
 
16
+ def identity(img):
17
+ return img, img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  @spaces.GPU
20
  def run_demo_server(pipe):
21
+ title = "Geowizard"
22
+ description = "Gradio demo for Geowizard."
23
+
24
+ examples = ["files/bee.jpg"]
25
+
26
+ gr.Interface(
27
+ depth_normal,
28
+ inputs=[gr.Image(type='pil', label="Original Image"),
29
+ outputs=[gr.Image(type="pil",label="Output Depth"), gr.Image(type="pil",label="Output Normal")],
30
+ title=title, description=description, article='1', examples=examples, analytics_enabled=False).launch()
31
+
32
+
33
+ # def process(
34
+ # pipe,
35
+ # path_input,
36
+ # ensemble_size,
37
+ # denoise_steps,
38
+ # processing_res,
39
+ # path_out_16bit=None,
40
+ # path_out_fp32=None,
41
+ # path_out_vis=None,
42
+ # ):
43
+
44
+ # if path_out_vis is not None:
45
+ # return (
46
+ # [path_out_16bit, path_out_vis],
47
+ # [path_out_16bit, path_out_fp32, path_out_vis],
48
+ # )
49
+
50
+ # input_image = Image.open(path_input)
51
+
52
+ # pipe_out = pipe(
53
+ # input_image,
54
+ # denoising_steps=denoise_steps,
55
+ # ensemble_size=ensemble_size,
56
+ # processing_res=processing_res,
57
+ # batch_size=1 if processing_res == 0 else 0,
58
+ # guidance_scale=3,
59
+ # domain="indoor",
60
+ # show_progress_bar=True,
61
+ # )
62
+
63
+ # depth_pred = pipe_out.depth_np
64
+ # depth_colored = pipe_out.depth_colored
65
+ # depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
66
+
67
+ # path_output_dir = os.path.splitext(path_input)[0] + "_output"
68
+ # os.makedirs(path_output_dir, exist_ok=True)
69
+
70
+ # name_base = os.path.splitext(os.path.basename(path_input))[0]
71
+ # path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
72
+ # path_out_16bit = os.path.join(path_output_dir, f"{name_base}_depth_16bit.png")
73
+ # path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")
74
+
75
+ # np.save(path_out_fp32, depth_pred)
76
+ # Image.fromarray(depth_16bit).save(path_out_16bit, mode="I;16")
77
+ # depth_colored.save(path_out_vis)
78
+
79
+ # return (
80
+ # [path_out_16bit, path_out_vis],
81
+ # [path_out_16bit, path_out_fp32, path_out_vis],
82
+ # )
83
+
84
+
85
+ # @spaces.GPU
86
+ # def run_demo_server(pipe):
87
+ # process_pipe = functools.partial(process, pipe)
88
+ # os.environ["GRADIO_ALLOW_FLAGGING"] = "never"
89
+
90
+ # with gr.Blocks(
91
+ # analytics_enabled=False,
92
+ # title="GeoWizard Depth and Normal Estimation",
93
+ # css="""
94
+ # #download {
95
+ # height: 118px;
96
+ # }
97
+ # .slider .inner {
98
+ # width: 5px;
99
+ # background: #FFF;
100
+ # }
101
+ # .viewport {
102
+ # aspect-ratio: 4/3;
103
+ # }
104
+ # """,
105
+ # ) as demo:
106
+ # gr.Markdown(
107
+ # """
108
+ # <h1 align="center">Geowizard Depth & Normal Estimation</h1>
109
+ # """
110
+ # )
111
+
112
+ # with gr.Row():
113
+ # with gr.Column():
114
+ # input_image = gr.Image(
115
+ # label="Input Image",
116
+ # type="filepath",
117
+ # )
118
+ # with gr.Accordion("Advanced options", open=False):
119
+ # domain = gr.Radio(
120
+ # [
121
+ # ("Outdoor", "outdoor"),
122
+ # ("Indoor", "indoor"),
123
+ # ("Object", "object"),
124
+ # ],
125
+ # label="Data Domain",
126
+ # value="indoor",
127
+ # )
128
+ # cfg_scale = gr.Slider(
129
+ # label="Classifier Free Guidance Scale",
130
+ # minimum=1,
131
+ # maximum=5,
132
+ # step=1,
133
+ # value=3,
134
+ # )
135
+ # denoise_steps = gr.Slider(
136
+ # label="Number of denoising steps",
137
+ # minimum=1,
138
+ # maximum=20,
139
+ # step=1,
140
+ # value=2,
141
+ # )
142
+ # ensemble_size = gr.Slider(
143
+ # label="Ensemble size",
144
+ # minimum=1,
145
+ # maximum=15,
146
+ # step=1,
147
+ # value=1,
148
+ # )
149
+ # processing_res = gr.Radio(
150
+ # [
151
+ # ("Native", 0),
152
+ # ("Recommended", 768),
153
+ # ],
154
+ # label="Processing resolution",
155
+ # value=768,
156
+ # )
157
+ # input_output_16bit = gr.File(
158
+ # label="Predicted depth (16-bit)",
159
+ # visible=False,
160
+ # )
161
+ # input_output_fp32 = gr.File(
162
+ # label="Predicted depth (32-bit)",
163
+ # visible=False,
164
+ # )
165
+ # input_output_vis = gr.File(
166
+ # label="Predicted depth (red-near, blue-far)",
167
+ # visible=False,
168
+ # )
169
+ # with gr.Row():
170
+ # submit_btn = gr.Button(value="Compute", variant="primary")
171
+ # clear_btn = gr.Button(value="Clear")
172
+ # with gr.Column():
173
+ # output_slider = ImageSlider(
174
+ # label="Predicted depth (red-near, blue-far)",
175
+ # type="filepath",
176
+ # show_download_button=True,
177
+ # show_share_button=True,
178
+ # interactive=False,
179
+ # elem_classes="slider",
180
+ # position=0.25,
181
+ # )
182
+ # files = gr.Files(
183
+ # label="Depth outputs",
184
+ # elem_id="download",
185
+ # interactive=False,
186
+ # )
187
+
188
+ # blocks_settings_depth = [ensemble_size, denoise_steps, processing_res]
189
+ # blocks_settings = blocks_settings_depth
190
+ # map_id_to_default = {b._id: b.value for b in blocks_settings}
191
+
192
+ # inputs = [
193
+ # input_image,
194
+ # ensemble_size,
195
+ # denoise_steps,
196
+ # processing_res,
197
+ # input_output_16bit,
198
+ # input_output_fp32,
199
+ # input_output_vis,
200
+ # ]
201
+ # outputs = [
202
+ # submit_btn,
203
+ # input_image,
204
+ # output_slider,
205
+ # files,
206
+ # ]
207
+
208
+ # def submit_depth_fn(*args):
209
+ # out = list(process_pipe(*args))
210
+ # out = [gr.Button(interactive=False), gr.Image(interactive=False)] + out
211
+ # return out
212
+
213
+ # submit_btn.click(
214
+ # fn=submit_depth_fn,
215
+ # inputs=inputs,
216
+ # outputs=outputs,
217
+ # concurrency_limit=1,
218
+ # )
219
+
220
+ # gr.Examples(
221
+ # fn=submit_depth_fn,
222
+ # examples=[
223
+ # [
224
+ # "files/bee.jpg",
225
+ # 10, # ensemble_size
226
+ # 10, # denoise_steps
227
+ # 768, # processing_res
228
+ # "files/bee_depth_16bit.png",
229
+ # "files/bee_depth_fp32.npy",
230
+ # "files/bee_depth_colored.png",
231
+ # ],
232
+ # ],
233
+ # inputs=inputs,
234
+ # outputs=outputs,
235
+ # cache_examples=True,
236
+ # )
237
+
238
+ # def clear_fn():
239
+ # out = []
240
+ # for b in blocks_settings:
241
+ # out.append(map_id_to_default[b._id])
242
+ # out += [
243
+ # gr.Button(interactive=True),
244
+ # gr.Image(value=None, interactive=True),
245
+ # None, None, None, None, None, None, None,
246
+ # ]
247
+ # return out
248
+
249
+ # clear_btn.click(
250
+ # fn=clear_fn,
251
+ # inputs=[],
252
+ # outputs=blocks_settings + [
253
+ # submit_btn,
254
+ # input_image,
255
+ # input_output_16bit,
256
+ # input_output_fp32,
257
+ # input_output_vis,
258
+ # output_slider,
259
+ # files,
260
+ # ],
261
+ # )
262
+
263
+ # demo.queue(
264
+ # api_open=False,
265
+ # ).launch(
266
+ # server_name="0.0.0.0",
267
+ # server_port=7860,
268
+ # )
269
 
270
 
271
  def main():