Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
50e2279
·
1 Parent(s): 83d7ca7

Refactor Gradio interface in app.py to enhance layout and usability, including updates to image input settings, background choice options, and button functionality for generating 3D shapes. Streamlined processing function connections for improved clarity.

Browse files
Files changed (1) hide show
  1. app.py +45 -66
app.py CHANGED
@@ -237,97 +237,76 @@ _DESCRIPTION = '''
237
  '''
238
 
239
  # Create the Gradio interface
240
- with gr.Blocks(title="CRM: Single Image to 3D") as demo:
241
  gr.Markdown("# CRM: Single Image to 3D Textured Mesh with Convolutional Reconstruction Model")
242
  gr.Markdown(_DESCRIPTION)
243
-
244
  with gr.Row():
245
  with gr.Column():
246
  with gr.Row():
247
  image_input = gr.Image(
248
  label="Image input",
 
 
249
  type="pil",
250
- height=300
251
- )
252
- processed_image = gr.Image(
253
- label="Processed Image",
254
- type="pil",
255
- height=300
256
  )
257
-
258
  with gr.Row():
259
  with gr.Column():
260
- background_choice = gr.Radio(
261
- choices=["Alpha as mask", "Auto Remove background", "Custom Background"],
262
- value="Auto Remove background",
263
- label="Background choice"
264
- )
265
- back_ground_color = gr.ColorPicker(
266
- label="Background Color",
267
- value="#7F7F7F"
268
- )
269
  foreground_ratio = gr.Slider(
270
  label="Foreground Ratio",
271
  minimum=0.5,
272
  maximum=1.0,
273
  value=1.0,
274
- step=0.05
275
  )
276
 
277
  with gr.Column():
278
- seed = gr.Number(
279
- value=1234,
280
- label="Seed",
281
- precision=0
282
- )
283
- guidance_scale = gr.Number(
284
- value=5.5,
285
- minimum=3,
286
- maximum=10,
287
- label="Guidance scale"
288
- )
289
- step = gr.Number(
290
- value=30,
291
- minimum=30,
292
- maximum=100,
293
- label="Sample steps",
294
- precision=0
295
- )
296
-
297
- text_button = gr.Button("Generate 3D shape", variant="primary")
298
-
299
- # Only add examples if the directory exists
300
  if os.path.exists("examples") and os.listdir("examples"):
301
  gr.Examples(
302
- examples=[[os.path.join("examples", i)] for i in os.listdir("examples") if i.lower().endswith(('.png', '.jpg', '.jpeg'))],
303
- inputs=[image_input]
 
304
  )
305
-
306
  with gr.Column():
307
- image_output = gr.Image(label="Output RGB image")
308
- xyz_output = gr.Image(label="Output CCM image")
309
- output_model = gr.Model3D(label="Output GLB")
 
 
 
310
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
311
 
312
- # Connect the button to the processing function
313
- text_button.click(
314
- fn=process_and_generate,
315
- inputs=[
316
- image_input,
317
- background_choice,
318
- foreground_ratio,
319
- back_ground_color,
320
- seed,
321
- guidance_scale,
322
- step
323
- ],
324
- outputs=[
325
- processed_image,
326
- image_output,
327
- xyz_output,
328
- output_model
329
- ],
330
- api_name="predict"
 
331
  )
332
 
333
  if __name__ == "__main__":
 
237
  '''
238
 
239
  # Create the Gradio interface
240
+ with gr.Blocks() as demo:
241
  gr.Markdown("# CRM: Single Image to 3D Textured Mesh with Convolutional Reconstruction Model")
242
  gr.Markdown(_DESCRIPTION)
 
243
  with gr.Row():
244
  with gr.Column():
245
  with gr.Row():
246
  image_input = gr.Image(
247
  label="Image input",
248
+ image_mode="RGBA",
249
+ sources="upload",
250
  type="pil",
 
 
 
 
 
 
251
  )
252
+ processed_image = gr.Image(label="Processed Image", interactive=False, type="pil", image_mode="RGB")
253
  with gr.Row():
254
  with gr.Column():
255
+ with gr.Row():
256
+ background_choice = gr.Radio([
257
+ "Alpha as mask",
258
+ "Auto Remove background"
259
+ ], value="Auto Remove background",
260
+ label="Background choice")
261
+ back_ground_color = gr.ColorPicker(label="Background Color", value="#7F7F7F", interactive=False)
 
 
262
  foreground_ratio = gr.Slider(
263
  label="Foreground Ratio",
264
  minimum=0.5,
265
  maximum=1.0,
266
  value=1.0,
267
+ step=0.05,
268
  )
269
 
270
  with gr.Column():
271
+ seed = gr.Number(value=1234, label="Seed", precision=0)
272
+ guidance_scale = gr.Number(value=5.5, minimum=3, maximum=10, label="Guidance scale")
273
+ step = gr.Number(value=30, minimum=30, maximum=100, label="Sample steps", precision=0)
274
+ text_button = gr.Button("Generate 3D shape")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  if os.path.exists("examples") and os.listdir("examples"):
276
  gr.Examples(
277
+ examples=[os.path.join("examples", i) for i in os.listdir("examples") if i.lower().endswith(('.png', '.jpg', '.jpeg'))],
278
+ inputs=[image_input],
279
+ examples_per_page=20,
280
  )
 
281
  with gr.Column():
282
+ image_output = gr.Image(interactive=False, label="Output RGB image")
283
+ xyz_output = gr.Image(interactive=False, label="Output CCM image")
284
+ output_model = gr.Model3D(
285
+ label="Output GLB",
286
+ interactive=False,
287
+ )
288
  gr.Markdown("Note: Ensure that the input image is correctly pre-processed into a grey background, otherwise the results will be unpredictable.")
289
 
290
+ inputs = [
291
+ processed_image,
292
+ seed,
293
+ guidance_scale,
294
+ step,
295
+ ]
296
+ outputs = [
297
+ image_output,
298
+ xyz_output,
299
+ output_model,
300
+ ]
301
+
302
+ text_button.click(fn=check_input_image, inputs=[image_input]).success(
303
+ fn=preprocess_image,
304
+ inputs=[image_input, background_choice, foreground_ratio, back_ground_color],
305
+ outputs=[processed_image],
306
+ ).success(
307
+ fn=gen_image,
308
+ inputs=inputs,
309
+ outputs=outputs,
310
  )
311
 
312
  if __name__ == "__main__":