Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
1eb08e4
·
1 Parent(s): 953b294

Refactor Gradio interface to improve layout and organization, enhancing user experience with clearer input/output sections and updated button functionality for generating 3D shapes.

Browse files
Files changed (1) hide show
  1. app.py +99 -72
app.py CHANGED
@@ -206,90 +206,117 @@ _DESCRIPTION = '''
206
  * If you find the output unsatisfying, try using different seeds:)
207
  '''
208
 
209
- def generate(image, bg_choice, fg_ratio, bg_color, seed_val, guidance, steps):
210
- if image is None:
211
- raise gr.Error("No image uploaded!")
212
- processed = preprocess_image(image, bg_choice, fg_ratio, bg_color)
213
- return gen_image(processed, seed_val, guidance, steps)
214
-
215
- # Create a Blocks interface with minimal settings
216
- with gr.Blocks(
217
- analytics_enabled=False,
218
- title="CRM: Single Image to 3D Textured Mesh with Convolutional Reconstruction Model"
219
- ) as demo:
220
  gr.Markdown(_DESCRIPTION)
221
 
222
  with gr.Row():
223
  with gr.Column():
224
- input_image = gr.Image(
225
- label="Image input",
226
- type="pil",
227
- sources=["upload"]
228
- )
229
- bg_choice = gr.Radio(
230
- choices=["Alpha as mask", "Auto Remove background"],
231
- value="Auto Remove background",
232
- label="Background choice"
233
- )
234
- fg_ratio = gr.Slider(
235
- label="Foreground Ratio",
236
- minimum=0.5,
237
- maximum=1.0,
238
- value=1.0,
239
- step=0.05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  )
241
- bg_color = gr.ColorPicker(
242
- label="Background Color",
243
- value="#7F7F7F",
 
244
  interactive=False
245
  )
246
- seed_val = gr.Number(
247
- value=1234,
248
- label="Seed",
249
- precision=0
250
- )
251
- guidance = gr.Number(
252
- value=5.5,
253
- minimum=3,
254
- maximum=10,
255
- label="Guidance scale"
256
  )
257
- steps = gr.Number(
258
- value=30,
259
- minimum=30,
260
- maximum=100,
261
- label="Sample steps",
262
- precision=0
263
  )
264
- generate_btn = gr.Button("Generate")
265
-
266
- with gr.Column():
267
- output_rgb = gr.Image(label="Output RGB image")
268
- output_ccm = gr.Image(label="Output CCM image")
269
- output_glb = gr.Model3D(label="Output GLB")
270
-
271
- # Connect the button click event
272
- generate_btn.click(
273
- fn=generate,
 
 
274
  inputs=[
275
- input_image,
276
- bg_choice,
277
- fg_ratio,
278
- bg_color,
279
- seed_val,
280
- guidance,
281
- steps
 
 
 
 
 
282
  ],
283
- outputs=[output_rgb, output_ccm, output_glb],
284
  api_name="predict"
285
  )
286
-
287
- # Add examples without caching
288
- gr.Examples(
289
- examples=[[os.path.join("examples", i)] for i in os.listdir("examples")],
290
- inputs=input_image,
291
- cache_examples=False
292
- )
293
 
294
  if __name__ == "__main__":
295
- demo.launch(show_api=True)
 
206
  * If you find the output unsatisfying, try using different seeds:)
207
  '''
208
 
209
+ with gr.Blocks() as demo:
210
+ gr.Markdown("# CRM: Single Image to 3D Textured Mesh with Convolutional Reconstruction Model")
 
 
 
 
 
 
 
 
 
211
  gr.Markdown(_DESCRIPTION)
212
 
213
  with gr.Row():
214
  with gr.Column():
215
+ with gr.Row():
216
+ image_input = gr.Image(
217
+ label="Image input",
218
+ image_mode="RGBA",
219
+ sources=["upload"],
220
+ type="pil"
221
+ )
222
+ processed_image = gr.Image(
223
+ label="Processed Image",
224
+ interactive=False,
225
+ type="pil",
226
+ image_mode="RGB"
227
+ )
228
+
229
+ with gr.Row():
230
+ with gr.Column():
231
+ with gr.Row():
232
+ background_choice = gr.Radio(
233
+ choices=["Alpha as mask", "Auto Remove background"],
234
+ value="Auto Remove background",
235
+ label="Background choice"
236
+ )
237
+ back_groud_color = gr.ColorPicker(
238
+ label="Background Color",
239
+ value="#7F7F7F",
240
+ interactive=False
241
+ )
242
+ foreground_ratio = gr.Slider(
243
+ label="Foreground Ratio",
244
+ minimum=0.5,
245
+ maximum=1.0,
246
+ value=1.0,
247
+ step=0.05
248
+ )
249
+
250
+ with gr.Column():
251
+ seed = gr.Number(
252
+ value=1234,
253
+ label="Seed",
254
+ precision=0
255
+ )
256
+ guidance_scale = gr.Number(
257
+ value=5.5,
258
+ minimum=3,
259
+ maximum=10,
260
+ label="Guidance scale"
261
+ )
262
+ step = gr.Number(
263
+ value=30,
264
+ minimum=30,
265
+ maximum=100,
266
+ label="Sample steps",
267
+ precision=0
268
+ )
269
+
270
+ text_button = gr.Button("Generate 3D shape")
271
+
272
+ gr.Examples(
273
+ examples=[[os.path.join("examples", i)] for i in os.listdir("examples")],
274
+ inputs=[image_input],
275
+ examples_per_page=20,
276
+ cache_examples=False
277
  )
278
+
279
+ with gr.Column():
280
+ image_output = gr.Image(
281
+ label="Output RGB image",
282
  interactive=False
283
  )
284
+ xyz_output = gr.Image(
285
+ label="Output CCM image",
286
+ interactive=False
 
 
 
 
 
 
 
287
  )
288
+ output_model = gr.Model3D(
289
+ label="Output GLB",
290
+ interactive=False
 
 
 
291
  )
292
+ gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
293
+
294
+ # Define the processing chain
295
+ def process_and_generate(image, bg_choice, fg_ratio, bg_color, seed_val, guidance, steps):
296
+ if image is None:
297
+ raise gr.Error("No image uploaded!")
298
+ processed = preprocess_image(image, bg_choice, fg_ratio, bg_color)
299
+ return gen_image(processed, seed_val, guidance, steps)
300
+
301
+ # Connect the button click event with API endpoint
302
+ text_button.click(
303
+ fn=process_and_generate,
304
  inputs=[
305
+ image_input,
306
+ background_choice,
307
+ foreground_ratio,
308
+ back_groud_color,
309
+ seed,
310
+ guidance_scale,
311
+ step
312
+ ],
313
+ outputs=[
314
+ image_output,
315
+ xyz_output,
316
+ output_model
317
  ],
 
318
  api_name="predict"
319
  )
 
 
 
 
 
 
 
320
 
321
  if __name__ == "__main__":
322
+ demo.queue().launch(show_api=True)