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
app.py
CHANGED
@@ -206,90 +206,117 @@ _DESCRIPTION = '''
|
|
206 |
* If you find the output unsatisfying, try using different seeds:)
|
207 |
'''
|
208 |
|
209 |
-
|
210 |
-
|
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 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
)
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
244 |
interactive=False
|
245 |
)
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
precision=0
|
250 |
-
)
|
251 |
-
guidance = gr.Number(
|
252 |
-
value=5.5,
|
253 |
-
minimum=3,
|
254 |
-
maximum=10,
|
255 |
-
label="Guidance scale"
|
256 |
)
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
maximum=100,
|
261 |
-
label="Sample steps",
|
262 |
-
precision=0
|
263 |
)
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
274 |
inputs=[
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
|
|
|
|
|
|
|
|
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)
|