xinjie.wang commited on
Commit
044ab04
·
1 Parent(s): 5aae6b8
Files changed (1) hide show
  1. app.py +19 -417
app.py CHANGED
@@ -66,429 +66,31 @@ def end_session(req: gr.Request) -> None:
66
  with gr.Blocks(
67
  delete_cache=(43200, 43200)
68
  ) as demo:
69
- gr.Markdown(
70
- f"""
71
- ## Image to 3D Asset Pipeline \n
72
- version: {VERSION} \n
73
- The service is temporarily deployed on `dev015-10.34.8.82: CUDA 4`.
74
- """
75
- )
76
- with gr.Row():
77
- with gr.Column(scale=2):
78
- with gr.Tabs() as input_tabs:
79
- with gr.Tab(
80
- label="Image(auto seg)", id=0
81
- ) as single_image_input_tab:
82
- image_prompt = gr.Image(
83
- label="Input Image",
84
- format="png",
85
- image_mode="RGBA",
86
- type="pil",
87
- height=300,
88
- )
89
- gr.Markdown(
90
- """
91
- If you are not satisfied with the auto segmentation
92
- result, please switch to the `Image(SAM seg)` tab."""
93
- )
94
- with gr.Tab(
95
- label="Image(SAM seg)", id=1
96
- ) as samimage_input_tab:
97
- with gr.Row():
98
- with gr.Column(scale=1):
99
- image_prompt_sam = gr.Image(
100
- label="Input Image", type="numpy", height=400
101
- )
102
- image_seg_sam = gr.Image(
103
- label="SAM Seg Image",
104
- image_mode="RGBA",
105
- type="pil",
106
- height=400,
107
- visible=False,
108
- )
109
- with gr.Column(scale=1):
110
- image_mask_sam = gr.AnnotatedImage()
111
-
112
- fg_bg_radio = gr.Radio(
113
- ["foreground_point", "background_point"],
114
- label="Select foreground(green) or background(red) points, by default foreground", # noqa
115
- value="foreground_point",
116
- )
117
- gr.Markdown(
118
- """ Click the `Input Image` to select SAM points,
119
- after get the satisified segmentation, click `Generate`
120
- button to generate the 3D asset. \n
121
- Note: If the segmented foreground is too small relative
122
- to the entire image area, the generation will fail.
123
- """
124
- )
125
-
126
- with gr.Accordion(label="Generation Settings", open=False):
127
- with gr.Row():
128
- seed = gr.Slider(
129
- 0, MAX_SEED, label="Seed", value=0, step=1
130
- )
131
- with gr.Row():
132
- randomize_seed = gr.Checkbox(
133
- label="Randomize Seed", value=False
134
- )
135
- project_delight = gr.Checkbox(
136
- label="Backproject delighting",
137
- value=True,
138
- )
139
- gr.Markdown("Geo Structure Generation")
140
- with gr.Row():
141
- ss_guidance_strength = gr.Slider(
142
- 0.0,
143
- 10.0,
144
- label="Guidance Strength",
145
- value=7.5,
146
- step=0.1,
147
- )
148
- ss_sampling_steps = gr.Slider(
149
- 1, 50, label="Sampling Steps", value=12, step=1
150
- )
151
- gr.Markdown("Visual Appearance Generation")
152
- with gr.Row():
153
- slat_guidance_strength = gr.Slider(
154
- 0.0,
155
- 10.0,
156
- label="Guidance Strength",
157
- value=3.0,
158
- step=0.1,
159
- )
160
- slat_sampling_steps = gr.Slider(
161
- 1, 50, label="Sampling Steps", value=12, step=1
162
- )
163
-
164
- generate_btn = gr.Button(
165
- "Generate(~0.5 mins)", variant="primary", interactive=False
166
- )
167
- model_output_obj = gr.Textbox(label="raw mesh .obj", visible=False)
168
- with gr.Row():
169
- extract_rep3d_btn = gr.Button(
170
- "Extract 3D Representation(~2 mins)",
171
- variant="primary",
172
- interactive=False,
173
- )
174
- with gr.Accordion(
175
- label="Enter Asset Attributes(optional)", open=False
176
- ):
177
- asset_cat_text = gr.Textbox(
178
- label="Enter Asset Category (e.g., chair)"
179
- )
180
- height_range_text = gr.Textbox(
181
- label="Enter Height Range in meter (e.g., 0.5-0.6)"
182
- )
183
- mass_range_text = gr.Textbox(
184
- label="Enter Mass Range in kg (e.g., 1.1-1.2)"
185
- )
186
- asset_version_text = gr.Textbox(
187
- label=f"Enter version (e.g., {VERSION})"
188
- )
189
- with gr.Row():
190
- extract_urdf_btn = gr.Button(
191
- "Extract URDF(~1 mins)",
192
- variant="primary",
193
- interactive=False,
194
- )
195
- with gr.Row():
196
- gr.Markdown(
197
- "#### Estimated Asset 3D Attributes(No input required)"
198
- )
199
- with gr.Row():
200
- est_type_text = gr.Textbox(
201
- label="Asset category", interactive=False
202
- )
203
- est_height_text = gr.Textbox(
204
- label="Real height(.m)", interactive=False
205
- )
206
- est_mass_text = gr.Textbox(
207
- label="Mass(.kg)", interactive=False
208
- )
209
- est_mu_text = gr.Textbox(
210
- label="Friction coefficient", interactive=False
211
- )
212
- with gr.Row():
213
- download_urdf = gr.DownloadButton(
214
- label="Download URDF", variant="primary", interactive=False
215
- )
216
-
217
- gr.Markdown(
218
- """ NOTE: If `Asset Attributes` are provided, the provided
219
- properties will be used; otherwise, the GPT-preset properties
220
- will be applied. \n
221
- The `Download URDF` file is restored to the real scale and
222
- has quality inspection, open with an editor to view details.
223
- """
224
- )
225
-
226
- with gr.Row() as single_image_example:
227
- examples = gr.Examples(
228
- label="Image Gallery",
229
- examples=[
230
- [f"assets/example_image/{image}"]
231
- for image in os.listdir(
232
- "assets/example_image"
233
- )
234
- ],
235
- inputs=[image_prompt],
236
- # fn=partial(
237
- # preprocess_image_fn,
238
- # model=RBG_REMOVER,
239
- # buffer=IMAGE_BUFFER,
240
- # ),
241
- outputs=[image_prompt],
242
- # run_on_click=True,
243
- examples_per_page=32,
244
- )
245
-
246
- with gr.Row(visible=False) as single_sam_image_example:
247
- examples = gr.Examples(
248
- label="Image Gallery",
249
- examples=[
250
- f"assets/example_image/{image}"
251
- for image in os.listdir(
252
- "assets/example_image"
253
- )
254
- ],
255
- inputs=[image_prompt_sam],
256
- # fn=partial(
257
- # preprocess_sam_image_fn,
258
- # buffer=IMAGE_BUFFER,
259
- # model=SAM_PREDICTOR,
260
- # ),
261
- outputs=[image_prompt_sam],
262
- # run_on_click=True,
263
- examples_per_page=32,
264
- )
265
- with gr.Column(scale=1):
266
- video_output = gr.Video(
267
- label="Generated 3D Asset",
268
- autoplay=True,
269
- loop=True,
270
  height=300,
 
 
271
  )
272
- model_output_gs = gr.Model3D(
273
- label="Gaussian Representation", height=300, interactive=False
274
- )
275
- aligned_gs = gr.Textbox(visible=False)
276
- with gr.Row():
277
- model_output_mesh = LitModel3D(
278
- label="Mesh Representation",
279
- height=300,
280
- exposure=10,
281
- interactive=False
282
- )
283
- gr.Markdown(
284
- """ The rendering of `Gaussian Representation` takes additional 10s. """ # noqa
285
- )
286
-
287
- is_samimage = gr.State(False)
288
- output_buf = gr.State()
289
- selected_points = gr.State(value=[])
290
 
291
  demo.load(start_session)
292
  demo.unload(end_session)
293
 
294
- single_image_input_tab.select(
295
- lambda: tuple(
296
- [False, gr.Row.update(visible=True), gr.Row.update(visible=False)]
297
- ),
298
- outputs=[is_samimage, single_image_example, single_sam_image_example],
299
- )
300
- samimage_input_tab.select(
301
- lambda: tuple(
302
- [True, gr.Row.update(visible=True), gr.Row.update(visible=False)]
303
- ),
304
- outputs=[is_samimage, single_sam_image_example, single_image_example],
305
- )
306
-
307
- image_prompt.upload(
308
- # partial(preprocess_image_fn, model=RBG_REMOVER, buffer=IMAGE_BUFFER),
309
- inputs=[image_prompt],
310
- outputs=[image_prompt],
311
- )
312
- image_prompt.change(
313
- lambda: tuple(
314
- [
315
- gr.Button(interactive=False),
316
- gr.Button(interactive=False),
317
- gr.Button(interactive=False),
318
- None,
319
- "",
320
- None,
321
- None,
322
- "",
323
- "",
324
- "",
325
- "",
326
- "",
327
- "",
328
- "",
329
- "",
330
- ]
331
- ),
332
- outputs=[
333
- extract_rep3d_btn,
334
- extract_urdf_btn,
335
- download_urdf,
336
- model_output_gs,
337
- aligned_gs,
338
- model_output_mesh,
339
- video_output,
340
- asset_cat_text,
341
- height_range_text,
342
- mass_range_text,
343
- asset_version_text,
344
- est_type_text,
345
- est_height_text,
346
- est_mass_text,
347
- est_mu_text,
348
- ],
349
- )
350
- image_prompt.change(
351
- active_btn_by_content,
352
- inputs=image_prompt,
353
- outputs=generate_btn,
354
- )
355
-
356
- image_prompt_sam.upload(
357
- # partial(
358
- # preprocess_sam_image_fn, buffer=IMAGE_BUFFER, model=SAM_PREDICTOR
359
- # ),
360
- inputs=[image_prompt_sam],
361
- outputs=[image_prompt_sam],
362
- )
363
- image_prompt_sam.change(
364
- lambda: tuple(
365
- [
366
- gr.Button(interactive=False),
367
- gr.Button(interactive=False),
368
- gr.Button(interactive=False),
369
- None,
370
- None,
371
- None,
372
- "",
373
- "",
374
- "",
375
- "",
376
- "",
377
- "",
378
- "",
379
- "",
380
- None,
381
- [],
382
- ]
383
- ),
384
- outputs=[
385
- extract_rep3d_btn,
386
- extract_urdf_btn,
387
- download_urdf,
388
- model_output_gs,
389
- model_output_mesh,
390
- video_output,
391
- asset_cat_text,
392
- height_range_text,
393
- mass_range_text,
394
- asset_version_text,
395
- est_type_text,
396
- est_height_text,
397
- est_mass_text,
398
- est_mu_text,
399
- image_mask_sam,
400
- selected_points,
401
- ],
402
- )
403
-
404
- image_prompt_sam.select(
405
- select_point,
406
- [
407
- image_prompt_sam,
408
- selected_points,
409
- fg_bg_radio,
410
- # gr.State(lambda: SAM_PREDICTOR),
411
- ],
412
- [image_mask_sam, image_seg_sam],
413
- )
414
- image_seg_sam.change(
415
- active_btn_by_content,
416
- inputs=image_seg_sam,
417
- outputs=generate_btn,
418
- )
419
-
420
- generate_btn.click(
421
- get_seed,
422
- inputs=[randomize_seed, seed],
423
- outputs=[seed],
424
- ).success(
425
- image_to_3d,
426
- inputs=[
427
- image_prompt,
428
- seed,
429
- ss_guidance_strength,
430
- ss_sampling_steps,
431
- slat_guidance_strength,
432
- slat_sampling_steps,
433
- # gr.State(lambda: IMAGE_BUFFER),
434
- # gr.State(lambda: PIPELINE),
435
- gr.State(lambda: TMP_DIR),
436
- image_seg_sam,
437
- is_samimage,
438
- ],
439
- outputs=[output_buf, video_output],
440
- ).success(
441
- lambda: gr.Button(interactive=True),
442
- outputs=[extract_rep3d_btn],
443
- )
444
-
445
- extract_rep3d_btn.click(
446
- extract_3d_representations_v2,
447
- inputs=[
448
- output_buf,
449
- project_delight,
450
- gr.State(lambda: TMP_DIR),
451
- # gr.State(lambda: DELIGHT),
452
- # gr.State(lambda: IMAGESR_MODEL),
453
- ],
454
- outputs=[
455
- model_output_mesh,
456
- model_output_gs,
457
- model_output_obj,
458
- aligned_gs,
459
- ],
460
- ).success(
461
- lambda: gr.Button(interactive=True),
462
- outputs=[extract_urdf_btn],
463
- )
464
-
465
- extract_urdf_btn.click(
466
- extract_urdf,
467
- inputs=[
468
- aligned_gs,
469
- model_output_obj,
470
- asset_cat_text,
471
- height_range_text,
472
- mass_range_text,
473
- asset_version_text,
474
- gr.State(lambda: TMP_DIR),
475
- # gr.State(lambda: URDF_CONVERTOR),
476
- # gr.State(lambda: IMAGE_BUFFER),
477
- # gr.State(lambda: CHECKERS),
478
- ],
479
- outputs=[
480
- download_urdf,
481
- est_type_text,
482
- est_height_text,
483
- est_mass_text,
484
- est_mu_text,
485
- ],
486
- queue=True,
487
- show_progress="full",
488
- ).success(
489
- lambda: gr.Button(interactive=True),
490
- outputs=[download_urdf],
491
- )
492
 
493
 
494
  if __name__ == "__main__":
 
66
  with gr.Blocks(
67
  delete_cache=(43200, 43200)
68
  ) as demo:
69
+ with gr.Column(scale=1):
70
+ video_output = gr.Video(
71
+ label="Generated 3D Asset",
72
+ autoplay=True,
73
+ loop=True,
74
+ height=300,
75
+ )
76
+ model_output_gs = gr.Model3D(
77
+ label="Gaussian Representation", height=300, interactive=False
78
+ )
79
+ aligned_gs = gr.Textbox(visible=False)
80
+ with gr.Row():
81
+ model_output_mesh = LitModel3D(
82
+ label="Mesh Representation",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  height=300,
84
+ exposure=10,
85
+ interactive=False
86
  )
87
+ gr.Markdown(
88
+ """ The rendering of `Gaussian Representation` takes additional 10s. """ # noqa
89
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  demo.load(start_session)
92
  demo.unload(end_session)
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
 
96
  if __name__ == "__main__":