Spaces:
Running
on
L40S
Running
on
L40S
Upload ./app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -134,89 +134,59 @@ if BAKE_AVAILEBLE:
|
|
134 |
def gen_save_folder(max_size=30):
|
135 |
os.makedirs('./outputs/app_output', exist_ok=True)
|
136 |
exists = set(int(_) for _ in os.listdir('./outputs/app_output') if not _.startswith("."))
|
137 |
-
if len(exists)
|
138 |
-
shutil.rmtree(f"./outputs/app_output/0")
|
139 |
-
cur_id = 0
|
140 |
-
else:
|
141 |
-
cur_id = min(set(range(max_size)) - exists)
|
142 |
if os.path.exists(f"./outputs/app_output/{(cur_id + 1) % max_size}"):
|
143 |
shutil.rmtree(f"./outputs/app_output/{(cur_id + 1) % max_size}")
|
144 |
-
|
|
|
145 |
os.makedirs(save_folder, exist_ok=True)
|
146 |
print(f"mkdir {save_folder} suceess !!!")
|
147 |
return save_folder
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
rgba.save(dst)
|
164 |
-
return dst
|
165 |
-
|
166 |
-
def stage_2_i2v(image, seed, step, save_folder):
|
167 |
-
if isinstance(image, str):
|
168 |
-
image = Image.open(image)
|
169 |
-
gif_dst = save_folder + '/views.gif'
|
170 |
-
res_img, pils = worker_i2v(image, seed, step)
|
171 |
-
save_gif(pils, gif_dst)
|
172 |
views_img, cond_img = res_img[0], res_img[1]
|
173 |
img_array = np.asarray(views_img, dtype=np.uint8)
|
174 |
show_img = rearrange(img_array, '(n h) (m w) c -> (n m) h w c', n=3, m=2)
|
175 |
-
show_img = show_img[worker_i2v.order, ...]
|
176 |
-
show_img =
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
def stage_3_v23(
|
181 |
-
views_pil,
|
182 |
-
cond_pil,
|
183 |
-
seed,
|
184 |
-
save_folder,
|
185 |
-
target_face_count = 30000,
|
186 |
-
texture_color = 'face'
|
187 |
-
):
|
188 |
-
do_texture_mapping = texture_color == 'face'
|
189 |
worker_v23(
|
190 |
-
|
191 |
-
cond_pil,
|
192 |
-
seed = seed,
|
193 |
save_folder = save_folder,
|
194 |
-
target_face_count =
|
195 |
do_texture_mapping = do_texture_mapping
|
196 |
)
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
if color == "face" and bake:
|
204 |
obj_dst = worker_baker(save_folder, force, front, others, align_times)
|
205 |
glb_dst = obj_dst.replace(".obj", ".glb")
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
gif_dst = obj_dst.replace(".obj", ".gif")
|
216 |
-
worker_gif(obj_dst, gif_dst_path=gif_dst)
|
217 |
-
return gif_dst
|
218 |
-
|
219 |
-
|
220 |
def check_image_available(image):
|
221 |
if image is None:
|
222 |
return "Please upload image", gr.update()
|
@@ -252,7 +222,7 @@ def update_mode(mode):
|
|
252 |
}[mode]
|
253 |
face_change = {
|
254 |
'Vertex color': gr.update(value=120000, maximum=300000),
|
255 |
-
'
|
256 |
'Baking': gr.update(value=10000, maximum=60000)
|
257 |
}[mode]
|
258 |
render_change = {
|
@@ -269,279 +239,133 @@ def update_mode(mode):
|
|
269 |
with gr.Blocks() as demo:
|
270 |
gr.Markdown(CONST_HEADER)
|
271 |
with gr.Row(variant="panel"):
|
272 |
-
|
273 |
-
###### Input region
|
274 |
-
|
275 |
with gr.Column(scale=2):
|
276 |
-
|
277 |
-
### Text iutput region
|
278 |
-
|
279 |
with gr.Tab("Text to 3D"):
|
280 |
with gr.Column():
|
281 |
text = gr.TextArea('一只黑白相间的熊猫在白色背景上居中坐着,呈现出卡通风格和可爱氛围。',
|
282 |
lines=3, max_lines=20, label='Input text (within 70 words)')
|
|
|
|
|
|
|
|
|
283 |
|
284 |
-
textgen_mode = gr.Radio(
|
285 |
-
choices=['Vertex color', 'Face color', 'Baking'],
|
286 |
-
label="Texture mode",
|
287 |
-
value='Baking',
|
288 |
-
interactive=True
|
289 |
-
)
|
290 |
-
|
291 |
-
with gr.Accordion("Custom settings", open=False):
|
292 |
-
textgen_color = gr.Radio(choices=["vertex", "face"], label="Color", value="face")
|
293 |
-
|
294 |
-
with gr.Row():
|
295 |
-
textgen_render = gr.Checkbox(
|
296 |
-
label="Do Rendering",
|
297 |
-
value=True,
|
298 |
-
interactive=True
|
299 |
-
)
|
300 |
-
textgen_bake = gr.Checkbox(
|
301 |
-
label="Do Baking",
|
302 |
-
value=True if BAKE_AVAILEBLE else False,
|
303 |
-
interactive=True if BAKE_AVAILEBLE else False
|
304 |
-
)
|
305 |
-
|
306 |
-
with gr.Row():
|
307 |
-
textgen_seed = gr.Number(value=0, label="T2I seed", precision=0, interactive=True)
|
308 |
-
textgen_SEED = gr.Number(value=0, label="Gen seed", precision=0, interactive=True)
|
309 |
-
|
310 |
-
textgen_step = gr.Slider(
|
311 |
-
value=25,
|
312 |
-
minimum=15,
|
313 |
-
maximum=50,
|
314 |
-
step=1,
|
315 |
-
label="T2I steps",
|
316 |
-
interactive=True
|
317 |
-
)
|
318 |
-
textgen_STEP = gr.Slider(
|
319 |
-
value=50,
|
320 |
-
minimum=20,
|
321 |
-
maximum=80,
|
322 |
-
step=1,
|
323 |
-
label="Gen steps",
|
324 |
-
interactive=True
|
325 |
-
)
|
326 |
-
textgen_max_faces =gr.Slider(
|
327 |
-
value=10000,
|
328 |
-
minimum=2000,
|
329 |
-
maximum=60000,
|
330 |
-
step=1000,
|
331 |
-
label="Face number limit",
|
332 |
-
interactive=True
|
333 |
-
)
|
334 |
-
|
335 |
-
with gr.Accordion("Baking Options", open=False):
|
336 |
-
textgen_force_bake = gr.Checkbox(
|
337 |
-
label="Force (Ignore the degree of matching)",
|
338 |
-
value=False,
|
339 |
-
interactive=True
|
340 |
-
)
|
341 |
-
textgen_front_baking = gr.Radio(
|
342 |
-
choices=['input image', 'multi-view front view', 'auto'],
|
343 |
-
label="Front view baking",
|
344 |
-
value='auto',
|
345 |
-
interactive=True,
|
346 |
-
visible=True
|
347 |
-
)
|
348 |
-
textgen_other_views = gr.CheckboxGroup(
|
349 |
-
choices=['60°', '120°', '180°', '240°', '300°'],
|
350 |
-
label="Other views baking",
|
351 |
-
value=['180°'],
|
352 |
-
interactive=True,
|
353 |
-
visible=True
|
354 |
-
)
|
355 |
-
textgen_align_times = gr.Slider(
|
356 |
-
value=3,
|
357 |
-
minimum=1,
|
358 |
-
maximum=5,
|
359 |
-
step=1,
|
360 |
-
label="Number of alignment attempts per view",
|
361 |
-
interactive=True
|
362 |
-
)
|
363 |
-
|
364 |
-
with gr.Row():
|
365 |
-
textgen_submit = gr.Button("Generate", variant="primary")
|
366 |
-
|
367 |
-
with gr.Row():
|
368 |
-
gr.Examples(examples=example_ts, inputs=[text], label="Text examples", examples_per_page=10)
|
369 |
-
|
370 |
-
|
371 |
-
textgen_mode.change(
|
372 |
-
fn=update_mode,
|
373 |
-
inputs=textgen_mode,
|
374 |
-
outputs=[textgen_color, textgen_bake, textgen_max_faces, textgen_render]
|
375 |
-
)
|
376 |
-
textgen_color.change(
|
377 |
-
fn=lambda x:[
|
378 |
-
gr.update(value=(x=='face'), interactive=(x=='face'), visible=(x=='face')),
|
379 |
-
gr.update(value=(x=='face'), interactive=(x=='face'), visible=(x=='face')),
|
380 |
-
],
|
381 |
-
inputs=textgen_color,
|
382 |
-
outputs=[
|
383 |
-
textgen_bake,
|
384 |
-
textgen_render
|
385 |
-
]
|
386 |
-
)
|
387 |
-
textgen_bake.change(
|
388 |
-
fn= lambda x:[
|
389 |
-
gr.update(visible=x),
|
390 |
-
gr.update(visible=x),
|
391 |
-
gr.update(visible=x),
|
392 |
-
gr.update(visible=x),
|
393 |
-
gr.update(value=10000 if x else 120000, minimum=2000, maximum=60000 if x else 300000)
|
394 |
-
],
|
395 |
-
inputs=textgen_bake,
|
396 |
-
outputs=[
|
397 |
-
textgen_front_baking,
|
398 |
-
textgen_other_views,
|
399 |
-
textgen_align_times,
|
400 |
-
textgen_force_bake,
|
401 |
-
textgen_max_faces
|
402 |
-
]
|
403 |
-
)
|
404 |
-
|
405 |
-
### Image iutput region
|
406 |
-
|
407 |
with gr.Tab("Image to 3D"):
|
408 |
with gr.Row():
|
409 |
input_image = gr.Image(label="Input image", width=256, height=256, type="pil",
|
410 |
image_mode="RGBA", sources="upload", interactive=True)
|
411 |
with gr.Row():
|
412 |
alert_message = gr.Markdown("") # for warning
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
with gr.Accordion("Custom settings", open=False):
|
422 |
-
imggen_color = gr.Radio(choices=["vertex", "face"], label="Color", value="face")
|
423 |
-
|
424 |
-
with gr.Row():
|
425 |
-
imggen_removebg = gr.Checkbox(
|
426 |
-
label="Remove Background",
|
427 |
-
value=True,
|
428 |
-
interactive=True
|
429 |
-
)
|
430 |
-
imggen_render = gr.Checkbox(
|
431 |
-
label="Do Rendering",
|
432 |
-
value=True,
|
433 |
-
interactive=True
|
434 |
-
)
|
435 |
-
imggen_bake = gr.Checkbox(
|
436 |
-
label="Do Baking",
|
437 |
-
value=True if BAKE_AVAILEBLE else False,
|
438 |
-
interactive=True if BAKE_AVAILEBLE else False
|
439 |
-
)
|
440 |
-
imggen_SEED = gr.Number(value=0, label="Gen seed", precision=0, interactive=True)
|
441 |
-
|
442 |
-
imggen_STEP = gr.Slider(
|
443 |
-
value=50,
|
444 |
-
minimum=20,
|
445 |
-
maximum=80,
|
446 |
-
step=1,
|
447 |
-
label="Gen steps",
|
448 |
interactive=True
|
449 |
)
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
456 |
interactive=True
|
457 |
)
|
|
|
|
|
|
|
|
|
|
|
458 |
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
visible=True
|
471 |
-
)
|
472 |
-
imggen_other_views = gr.CheckboxGroup(
|
473 |
-
choices=['60°', '120°', '180°', '240°', '300°'],
|
474 |
-
label="Other views baking",
|
475 |
-
value=['180°'],
|
476 |
-
interactive=True,
|
477 |
-
visible=True
|
478 |
-
)
|
479 |
-
imggen_align_times =gr.Slider(
|
480 |
-
value=3,
|
481 |
-
minimum=1,
|
482 |
-
maximum=5,
|
483 |
-
step=1,
|
484 |
-
label="Number of alignment attempts per view",
|
485 |
-
interactive=True
|
486 |
-
)
|
487 |
-
|
488 |
-
input_image.change(
|
489 |
-
fn=check_image_available,
|
490 |
-
inputs=input_image,
|
491 |
-
outputs=[alert_message, imggen_removebg]
|
492 |
-
)
|
493 |
-
|
494 |
-
imggen_mode.change(
|
495 |
-
fn=update_mode,
|
496 |
-
inputs=imggen_mode,
|
497 |
-
outputs=[imggen_color, imggen_bake, imggen_max_faces, imggen_render]
|
498 |
)
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
outputs=[
|
507 |
-
imggen_bake,
|
508 |
-
imggen_render
|
509 |
-
]
|
510 |
)
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
gr.update( value=10000 if x else 120000, minimum=2000, maximum=60000 if x else 300000)
|
519 |
-
],
|
520 |
-
inputs=imggen_bake,
|
521 |
-
outputs=[
|
522 |
-
imggen_front_baking,
|
523 |
-
imggen_other_views,
|
524 |
-
imggen_align_times,
|
525 |
-
imggen_force_bake,
|
526 |
-
imggen_max_faces
|
527 |
-
]
|
528 |
)
|
529 |
|
530 |
-
with gr.
|
531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
532 |
|
533 |
-
with gr.Row():
|
534 |
-
gr.Examples(examples=example_is, inputs=[input_image],
|
535 |
-
label="Img examples", examples_per_page=10)
|
536 |
-
|
537 |
gr.Markdown(CONST_NOTE)
|
538 |
-
|
539 |
###### Output region
|
540 |
|
541 |
with gr.Column(scale=3):
|
542 |
with gr.Row():
|
543 |
with gr.Column(scale=2):
|
544 |
-
|
545 |
label="Image without background",
|
546 |
type="pil",
|
547 |
image_mode="RGBA",
|
@@ -553,7 +377,7 @@ with gr.Blocks() as demo:
|
|
553 |
type="pil",
|
554 |
interactive=False
|
555 |
)
|
556 |
-
|
557 |
result_3dobj = gr.Model3D(
|
558 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
559 |
label="OBJ vertex color",
|
@@ -562,7 +386,7 @@ with gr.Blocks() as demo:
|
|
562 |
camera_position=[90, 90, None],
|
563 |
interactive=False
|
564 |
)
|
565 |
-
|
566 |
result_3dglb_texture = gr.Model3D(
|
567 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
568 |
label="GLB face color",
|
@@ -578,9 +402,9 @@ with gr.Blocks() as demo:
|
|
578 |
visible=True,
|
579 |
camera_position=[90, 90, None],
|
580 |
interactive=False)
|
581 |
-
|
582 |
result_gif = gr.Image(label="GIF", interactive=False)
|
583 |
-
|
584 |
with gr.Row():
|
585 |
gr.Markdown(
|
586 |
"Due to Gradio limitations, OBJ files are displayed with vertex shading only, "
|
@@ -592,79 +416,24 @@ with gr.Blocks() as demo:
|
|
592 |
#===============================================================
|
593 |
# gradio running code
|
594 |
#===============================================================
|
595 |
-
|
596 |
-
|
597 |
-
cond_image = gr.State()
|
598 |
-
views_image = gr.State()
|
599 |
-
|
600 |
-
def handle_click(save_folder):
|
601 |
-
if save_folder is None:
|
602 |
-
save_folder = gen_save_folder()
|
603 |
-
return save_folder
|
604 |
|
605 |
textgen_submit.click(
|
606 |
-
fn=
|
607 |
-
inputs=[
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
inputs=[text, textgen_seed, textgen_step, save_folder],
|
612 |
-
outputs=[rem_bg_image],
|
613 |
-
).success(
|
614 |
-
fn=stage_2_i2v,
|
615 |
-
inputs=[rem_bg_image, textgen_SEED, textgen_STEP, save_folder],
|
616 |
-
outputs=[views_image, cond_image, result_image],
|
617 |
-
).success(
|
618 |
-
fn=stage_3_v23,
|
619 |
-
inputs=[views_image, cond_image, textgen_SEED, save_folder, textgen_max_faces, textgen_color],
|
620 |
-
outputs=[result_3dobj, result_3dglb_texture],
|
621 |
-
).success(
|
622 |
-
fn=stage_3p_baking,
|
623 |
-
inputs=[save_folder, textgen_color, textgen_bake,
|
624 |
-
textgen_force_bake, textgen_front_baking, textgen_other_views, textgen_align_times],
|
625 |
-
outputs=[result_3dglb_baked],
|
626 |
-
).success(
|
627 |
-
fn=stage_4_gif,
|
628 |
-
inputs=[save_folder, textgen_color, textgen_bake, textgen_render],
|
629 |
-
outputs=[result_gif],
|
630 |
-
).success(lambda: print('Text_to_3D Done ...\n'))
|
631 |
|
632 |
-
|
633 |
imggen_submit.click(
|
634 |
-
fn=
|
635 |
-
inputs=[
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
inputs=[input_image, save_folder, imggen_removebg],
|
640 |
-
outputs=[rem_bg_image],
|
641 |
-
).success(
|
642 |
-
fn=stage_2_i2v,
|
643 |
-
inputs=[rem_bg_image, imggen_SEED, imggen_STEP, save_folder],
|
644 |
-
outputs=[views_image, cond_image, result_image],
|
645 |
-
).success(
|
646 |
-
fn=stage_3_v23,
|
647 |
-
inputs=[views_image, cond_image, imggen_SEED, save_folder, imggen_max_faces, imggen_color],
|
648 |
-
outputs=[result_3dobj, result_3dglb_texture],
|
649 |
-
).success(
|
650 |
-
fn=stage_3p_baking,
|
651 |
-
inputs=[save_folder, imggen_color, imggen_bake,
|
652 |
-
imggen_force_bake, imggen_front_baking, imggen_other_views, imggen_align_times],
|
653 |
-
outputs=[result_3dglb_baked],
|
654 |
-
).success(
|
655 |
-
fn=stage_4_gif,
|
656 |
-
inputs=[save_folder, imggen_color, imggen_bake, imggen_render],
|
657 |
-
outputs=[result_gif],
|
658 |
-
).success(lambda: print('Image_to_3D Done ...\n'))
|
659 |
-
|
660 |
-
#===============================================================
|
661 |
-
# start gradio server
|
662 |
-
#===============================================================
|
663 |
-
CONST_PORT = 8080
|
664 |
-
CONST_MAX_QUEUE = 1
|
665 |
-
CONST_SERVER = '0.0.0.0'
|
666 |
|
667 |
-
demo.queue(max_size=
|
668 |
-
demo.launch(server_name=
|
669 |
# demo.launch()
|
670 |
|
|
|
134 |
def gen_save_folder(max_size=30):
|
135 |
os.makedirs('./outputs/app_output', exist_ok=True)
|
136 |
exists = set(int(_) for _ in os.listdir('./outputs/app_output') if not _.startswith("."))
|
137 |
+
cur_id = min(set(range(max_size)) - exists) if len(exists)<max_size else -1
|
|
|
|
|
|
|
|
|
138 |
if os.path.exists(f"./outputs/app_output/{(cur_id + 1) % max_size}"):
|
139 |
shutil.rmtree(f"./outputs/app_output/{(cur_id + 1) % max_size}")
|
140 |
+
print(f"remove ./outputs/app_output/{(cur_id + 1) % max_size} success !!!")
|
141 |
+
save_folder = f'./outputs/app_output/{max(0, cur_id)}'
|
142 |
os.makedirs(save_folder, exist_ok=True)
|
143 |
print(f"mkdir {save_folder} suceess !!!")
|
144 |
return save_folder
|
145 |
|
146 |
+
|
147 |
+
# @spaces.GPU(duration=150)
|
148 |
+
def gen_pipe(text, image=None, do_removebg=True, sseed=0, sstep=25, SSEED=0, SSTEP=50, color='face',
|
149 |
+
bake=False, render=True, max_faces=12000, force=False, front='auto', others=[180], align_times=3):
|
150 |
+
save_folder = gen_save_folder()
|
151 |
+
image_gen = image is not None
|
152 |
+
if not image_gen:
|
153 |
+
image = worker_t2i(text, sseed, sstep)
|
154 |
+
image.save(save_folder + '/img.png')
|
155 |
+
img_nobg = worker_xbg(image, force=do_removebg if image_gen else True)
|
156 |
+
img_nobg.save(save_folder + '/img_nobg.png')
|
157 |
+
yield img_nobg, None, None, None, None, None
|
158 |
+
res_img, pils = worker_i2v(img_nobg, seed=SSEED, steps=SSTEP)
|
159 |
+
save_gif(pils, save_folder + '/views.gif')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
views_img, cond_img = res_img[0], res_img[1]
|
161 |
img_array = np.asarray(views_img, dtype=np.uint8)
|
162 |
show_img = rearrange(img_array, '(n h) (m w) c -> (n m) h w c', n=3, m=2)
|
163 |
+
show_img = rearrange(show_img[worker_i2v.order, ...], '(n m) h w c -> (n h) (m w) c', n=2, m=3)
|
164 |
+
show_img = Image.fromarray(show_img)
|
165 |
+
yield img_nobg, show_img, None, None, None, None
|
166 |
+
do_texture_mapping = color == 'face'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
worker_v23(
|
168 |
+
views_img, cond_img, seed = SSEED,
|
|
|
|
|
169 |
save_folder = save_folder,
|
170 |
+
target_face_count = max_faces,
|
171 |
do_texture_mapping = do_texture_mapping
|
172 |
)
|
173 |
+
glb_v23 = save_folder + '/mesh.glb' if do_texture_mapping else None
|
174 |
+
obj_v23 = save_folder + '/mesh.obj'
|
175 |
+
obj_v23 = save_folder + '/mesh_vertex_colors.obj'
|
176 |
+
yield img_nobg, show_img, obj_v23, glb_v23, None, None
|
177 |
+
glb_dst = None
|
178 |
+
if do_texture_mapping and bake:
|
|
|
179 |
obj_dst = worker_baker(save_folder, force, front, others, align_times)
|
180 |
glb_dst = obj_dst.replace(".obj", ".glb")
|
181 |
+
yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
|
182 |
+
if do_texture_mapping and render:
|
183 |
+
baked_obj_list = sorted(glob(save_folder + '/view_*/bake/mesh.obj'))
|
184 |
+
obj_dst = baked_obj_list[-1] if len(baked_obj_list)>=1 else save_folder+'/mesh.obj'
|
185 |
+
assert os.path.exists(obj_dst), f"{obj_dst} file not found"
|
186 |
+
gif_dst = obj_dst.replace(".obj", ".gif")
|
187 |
+
worker_gif(obj_dst, gif_dst_path=gif_dst)
|
188 |
+
yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, gif_dst
|
189 |
+
|
|
|
|
|
|
|
|
|
|
|
190 |
def check_image_available(image):
|
191 |
if image is None:
|
192 |
return "Please upload image", gr.update()
|
|
|
222 |
}[mode]
|
223 |
face_change = {
|
224 |
'Vertex color': gr.update(value=120000, maximum=300000),
|
225 |
+
'Face color': gr.update(value=60000, maximum=300000),
|
226 |
'Baking': gr.update(value=10000, maximum=60000)
|
227 |
}[mode]
|
228 |
render_change = {
|
|
|
239 |
with gr.Blocks() as demo:
|
240 |
gr.Markdown(CONST_HEADER)
|
241 |
with gr.Row(variant="panel"):
|
|
|
|
|
|
|
242 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
243 |
with gr.Tab("Text to 3D"):
|
244 |
with gr.Column():
|
245 |
text = gr.TextArea('一只黑白相间的熊猫在白色背景上居中坐着,呈现出卡通风格和可爱氛围。',
|
246 |
lines=3, max_lines=20, label='Input text (within 70 words)')
|
247 |
+
with gr.Row():
|
248 |
+
gr.Examples(examples=example_ts, inputs=[text], label="Text examples", examples_per_page=10)
|
249 |
+
with gr.Row():
|
250 |
+
textgen_submit = gr.Button("Generate", variant="primary")
|
251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
with gr.Tab("Image to 3D"):
|
253 |
with gr.Row():
|
254 |
input_image = gr.Image(label="Input image", width=256, height=256, type="pil",
|
255 |
image_mode="RGBA", sources="upload", interactive=True)
|
256 |
with gr.Row():
|
257 |
alert_message = gr.Markdown("") # for warning
|
258 |
+
with gr.Row():
|
259 |
+
gr.Examples(examples=example_is, inputs=[input_image],
|
260 |
+
label="Img examples", examples_per_page=10)
|
261 |
+
with gr.Row():
|
262 |
+
removebg = gr.Checkbox(
|
263 |
+
label="Remove Background",
|
264 |
+
value=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
interactive=True
|
266 |
)
|
267 |
+
imggen_submit = gr.Button("Generate", variant="primary")
|
268 |
+
|
269 |
+
mode = gr.Radio(
|
270 |
+
choices=['Vertex color', 'Face color', 'Baking'],
|
271 |
+
label="Texture mode",
|
272 |
+
value='Baking',
|
273 |
+
interactive=True
|
274 |
+
)
|
275 |
+
|
276 |
+
with gr.Accordion("Custom settings", open=False):
|
277 |
+
color = gr.Radio(choices=["vertex", "face"], label="Color", value="face")
|
278 |
+
with gr.Row():
|
279 |
+
|
280 |
+
render = gr.Checkbox(
|
281 |
+
label="Do Rendering",
|
282 |
+
value=True,
|
283 |
interactive=True
|
284 |
)
|
285 |
+
bake = gr.Checkbox(
|
286 |
+
label="Do Baking",
|
287 |
+
value=True if BAKE_AVAILEBLE else False,
|
288 |
+
interactive=True if BAKE_AVAILEBLE else False
|
289 |
+
)
|
290 |
|
291 |
+
with gr.Row():
|
292 |
+
seed = gr.Number(value=0, label="T2I seed", precision=0, interactive=True)
|
293 |
+
SEED = gr.Number(value=0, label="Gen seed", precision=0, interactive=True)
|
294 |
+
|
295 |
+
step = gr.Slider(
|
296 |
+
value=25,
|
297 |
+
minimum=15,
|
298 |
+
maximum=50,
|
299 |
+
step=1,
|
300 |
+
label="T2I steps",
|
301 |
+
interactive=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
)
|
303 |
+
STEP = gr.Slider(
|
304 |
+
value=50,
|
305 |
+
minimum=20,
|
306 |
+
maximum=80,
|
307 |
+
step=1,
|
308 |
+
label="Gen steps",
|
309 |
+
interactive=True
|
|
|
|
|
|
|
|
|
310 |
)
|
311 |
+
max_faces = gr.Slider(
|
312 |
+
value=10000,
|
313 |
+
minimum=2000,
|
314 |
+
maximum=60000,
|
315 |
+
step=1000,
|
316 |
+
label="Face number limit",
|
317 |
+
interactive=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
)
|
319 |
|
320 |
+
with gr.Accordion("Baking Options", open=False):
|
321 |
+
force_bake = gr.Checkbox(
|
322 |
+
label="Force (Ignore the degree of matching)",
|
323 |
+
value=False,
|
324 |
+
interactive=True
|
325 |
+
)
|
326 |
+
front_baking = gr.Radio(
|
327 |
+
choices=['input image', 'multi-view front view', 'auto'],
|
328 |
+
label="Front view baking",
|
329 |
+
value='auto',
|
330 |
+
interactive=True,
|
331 |
+
visible=True
|
332 |
+
)
|
333 |
+
other_views = gr.CheckboxGroup(
|
334 |
+
choices=['60°', '120°', '180°', '240°', '300°'],
|
335 |
+
label="Other views baking",
|
336 |
+
value=['180°'],
|
337 |
+
interactive=True,
|
338 |
+
visible=True
|
339 |
+
)
|
340 |
+
align_times =gr.Slider(
|
341 |
+
value=1,
|
342 |
+
minimum=1,
|
343 |
+
maximum=5,
|
344 |
+
step=1,
|
345 |
+
label="Number of alignment attempts per view",
|
346 |
+
interactive=True
|
347 |
+
)
|
348 |
+
|
349 |
+
input_image.change(
|
350 |
+
fn=check_image_available,
|
351 |
+
inputs=input_image,
|
352 |
+
outputs=[alert_message, removebg]
|
353 |
+
)
|
354 |
+
|
355 |
+
mode.change(
|
356 |
+
fn=update_mode,
|
357 |
+
inputs=mode,
|
358 |
+
outputs=[color, bake, max_faces, render]
|
359 |
+
)
|
360 |
|
|
|
|
|
|
|
|
|
361 |
gr.Markdown(CONST_NOTE)
|
362 |
+
|
363 |
###### Output region
|
364 |
|
365 |
with gr.Column(scale=3):
|
366 |
with gr.Row():
|
367 |
with gr.Column(scale=2):
|
368 |
+
rembg_image = gr.Image(
|
369 |
label="Image without background",
|
370 |
type="pil",
|
371 |
image_mode="RGBA",
|
|
|
377 |
type="pil",
|
378 |
interactive=False
|
379 |
)
|
380 |
+
|
381 |
result_3dobj = gr.Model3D(
|
382 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
383 |
label="OBJ vertex color",
|
|
|
386 |
camera_position=[90, 90, None],
|
387 |
interactive=False
|
388 |
)
|
389 |
+
|
390 |
result_3dglb_texture = gr.Model3D(
|
391 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
392 |
label="GLB face color",
|
|
|
402 |
visible=True,
|
403 |
camera_position=[90, 90, None],
|
404 |
interactive=False)
|
405 |
+
|
406 |
result_gif = gr.Image(label="GIF", interactive=False)
|
407 |
+
|
408 |
with gr.Row():
|
409 |
gr.Markdown(
|
410 |
"Due to Gradio limitations, OBJ files are displayed with vertex shading only, "
|
|
|
416 |
#===============================================================
|
417 |
# gradio running code
|
418 |
#===============================================================
|
419 |
+
|
420 |
+
none = gr.State(None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
421 |
|
422 |
textgen_submit.click(
|
423 |
+
fn=gen_pipe,
|
424 |
+
inputs=[text, none, removebg, seed, step, SEED, STEP, color, bake, render, max_faces, force_bake,
|
425 |
+
front_baking, other_views, align_times],
|
426 |
+
outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
|
427 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
|
|
|
429 |
imggen_submit.click(
|
430 |
+
fn=gen_pipe,
|
431 |
+
inputs=[none, input_image, removebg, seed, step, SEED, STEP, color, bake, render, max_faces, force_bake,
|
432 |
+
front_baking, other_views, align_times],
|
433 |
+
outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
|
434 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
|
436 |
+
demo.queue(max_size=1)
|
437 |
+
demo.launch(server_name='0.0.0.0', server_port=8080)
|
438 |
# demo.launch()
|
439 |
|