liuyuan-pal commited on
Commit
7b92dd6
1 Parent(s): b03210b

update layout

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -23,15 +23,17 @@ _DESCRIPTION = '''
23
  Given a single-view image, SyncDreamer is able to generate multiview-consistent images, which enables direct 3D reconstruction with NeuS or NeRF without SDS loss </br>
24
 
25
  Procedure: </br>
26
- **Step 0**. Upload an image or select an example. ==> The foreground is masked out by SAM. </br>
27
- **Step 1**. Select "Crop size" and click "Crop it". ==> The foreground object is centered and resized. </br>
28
  **Step 2**. Select "Elevation angle "and click "Run generation". ==> Generate multiview images. (This costs about 30s.) </br>
 
29
  To reconstruct a NeRF or a 3D mesh from the generated images, please refer to our [github repository](https://github.com/liuyuan-pal/SyncDreamer).
30
  '''
31
- _USER_GUIDE0 = "Step0: Please upload an image in the block above (or choose an example shown in the left)."
32
- _USER_GUIDE1 = "Step1: Please select a **Crop size** and click **Crop it**."
33
  _USER_GUIDE2 = "Step2: Please choose a **Elevation angle** and click **Run Generate**. This costs about 30s."
34
- _USER_GUIDE3 = "Generated multiview images are shown below!"
 
 
35
 
36
  deployed = True
37
 
@@ -63,6 +65,7 @@ class BackgroundRemoval:
63
  return image
64
 
65
  def resize_inputs(image_input, crop_size):
 
66
  alpha_np = np.asarray(image_input)[:, :, 3]
67
  coords = np.stack(np.nonzero(alpha_np), 1)[:, (1, 0)]
68
  min_x, min_y = np.min(coords, 0)
@@ -119,6 +122,7 @@ def generate(model, sample_steps, batch_view_num, sample_num, cfg_scale, seed, i
119
 
120
 
121
  def sam_predict(predictor, removal, raw_im):
 
122
  if deployed:
123
  raw_im.thumbnail([512, 512], Image.Resampling.LANCZOS)
124
  image_nobg = removal(raw_im.convert('RGB'))
@@ -213,7 +217,7 @@ def run_demo():
213
  with gr.Column(scale=0.8):
214
  sam_block = gr.Image(type='pil', image_mode='RGBA', label="SAM output", height=256, interactive=False)
215
  crop_size.render()
216
- crop_btn = gr.Button('Crop it', variant='primary', interactive=True)
217
  fig1 = gr.Image(value=Image.open('assets/elevation.jpg'), type='pil', image_mode='RGB', height=256, show_label=False, tool=None, interactive=False)
218
 
219
  with gr.Column(scale=0.8):
@@ -230,14 +234,22 @@ def run_demo():
230
 
231
  output_block = gr.Image(type='pil', image_mode='RGB', label="Outputs of SyncDreamer", height=256, interactive=False)
232
 
 
 
 
 
 
233
  update_guide = lambda GUIDE_TEXT: gr.update(value=GUIDE_TEXT)
234
- image_block.change(fn=partial(sam_predict, mask_predictor, removal), inputs=[image_block], outputs=[sam_block], queue=False)\
235
- .success(fn=partial(update_guide, _USER_GUIDE1), outputs=[guide_text], queue=False)
 
 
 
236
 
237
  crop_size.change(fn=resize_inputs, inputs=[sam_block, crop_size], outputs=[input_block], queue=False)\
238
- .success(fn=partial(update_guide, _USER_GUIDE2), outputs=[guide_text], queue=False)
239
- crop_btn.click(fn=resize_inputs, inputs=[sam_block, crop_size], outputs=[input_block], queue=False)\
240
- .success(fn=partial(update_guide, _USER_GUIDE2), outputs=[guide_text], queue=False)
241
 
242
  run_btn.click(partial(generate, model), inputs=[sample_steps, batch_view_num, sample_num, cfg_scale, seed, input_block, elevation], outputs=[output_block], queue=False)\
243
  .success(fn=partial(update_guide, _USER_GUIDE3), outputs=[guide_text], queue=False)
 
23
  Given a single-view image, SyncDreamer is able to generate multiview-consistent images, which enables direct 3D reconstruction with NeuS or NeRF without SDS loss </br>
24
 
25
  Procedure: </br>
26
+ **Step 1**. Upload an image or select an example. ==> The foreground is masked out by SAM and we crop it as inputs. </br>
 
27
  **Step 2**. Select "Elevation angle "and click "Run generation". ==> Generate multiview images. (This costs about 30s.) </br>
28
+ You may adjust the **Crop size** and **Elevation angle** to get a better result! <br>
29
  To reconstruct a NeRF or a 3D mesh from the generated images, please refer to our [github repository](https://github.com/liuyuan-pal/SyncDreamer).
30
  '''
31
+ _USER_GUIDE0 = "Step1: Please upload an image in the block above (or choose an example shown in the left)."
32
+ # _USER_GUIDE1 = "Step1: Please select a **Crop size** and click **Crop it**."
33
  _USER_GUIDE2 = "Step2: Please choose a **Elevation angle** and click **Run Generate**. This costs about 30s."
34
+ _USER_GUIDE3 = "Generated multiview images are shown below! (You may adjust the **Crop size** and **Elevation angle** to get a better result!)"
35
+
36
+ others = '''**Step 1**. Select "Crop size" and click "Crop it". ==> The foreground object is centered and resized. </br>'''
37
 
38
  deployed = True
39
 
 
65
  return image
66
 
67
  def resize_inputs(image_input, crop_size):
68
+ if image_input is None: return None
69
  alpha_np = np.asarray(image_input)[:, :, 3]
70
  coords = np.stack(np.nonzero(alpha_np), 1)[:, (1, 0)]
71
  min_x, min_y = np.min(coords, 0)
 
122
 
123
 
124
  def sam_predict(predictor, removal, raw_im):
125
+ if raw_im is None: return None
126
  if deployed:
127
  raw_im.thumbnail([512, 512], Image.Resampling.LANCZOS)
128
  image_nobg = removal(raw_im.convert('RGB'))
 
217
  with gr.Column(scale=0.8):
218
  sam_block = gr.Image(type='pil', image_mode='RGBA', label="SAM output", height=256, interactive=False)
219
  crop_size.render()
220
+ # crop_btn = gr.Button('Crop it', variant='primary', interactive=True)
221
  fig1 = gr.Image(value=Image.open('assets/elevation.jpg'), type='pil', image_mode='RGB', height=256, show_label=False, tool=None, interactive=False)
222
 
223
  with gr.Column(scale=0.8):
 
234
 
235
  output_block = gr.Image(type='pil', image_mode='RGB', label="Outputs of SyncDreamer", height=256, interactive=False)
236
 
237
+ def update_guide2(text, im):
238
+ if im is None:
239
+ return _USER_GUIDE0
240
+ else:
241
+ return text
242
  update_guide = lambda GUIDE_TEXT: gr.update(value=GUIDE_TEXT)
243
+
244
+ image_block.clear(fn=partial(update_guide, _USER_GUIDE0), outputs=[guide_text], queue=False)
245
+ image_block.change(fn=partial(sam_predict, mask_predictor, removal), inputs=[image_block], outputs=[sam_block], queue=False) \
246
+ .success(fn=resize_inputs, inputs=[sam_block, crop_size], outputs=[input_block], queue=False)\
247
+ .success(fn=partial(update_guide2, _USER_GUIDE2), inputs=[image_block], outputs=[guide_text], queue=False)\
248
 
249
  crop_size.change(fn=resize_inputs, inputs=[sam_block, crop_size], outputs=[input_block], queue=False)\
250
+ .success(fn=partial(update_guide, _USER_GUIDE2), outputs=[guide_text], queue=False)
251
+ # crop_btn.click(fn=resize_inputs, inputs=[sam_block, crop_size], outputs=[input_block], queue=False)\
252
+ # .success(fn=partial(update_guide, _USER_GUIDE2), outputs=[guide_text], queue=False)
253
 
254
  run_btn.click(partial(generate, model), inputs=[sample_steps, batch_view_num, sample_num, cfg_scale, seed, input_block, elevation], outputs=[output_block], queue=False)\
255
  .success(fn=partial(update_guide, _USER_GUIDE3), outputs=[guide_text], queue=False)