LPDoctor/AIGC-3D
3
1# Open Source Model Licensed under the Apache License Version 2.0 2# and Other Licenses of the Third-Party Components therein:3# The below Model in this distribution may have been modified by THL A29 Limited 4# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.5 6# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. 7# The below software and/or models in this distribution may have been 8# modified by THL A29 Limited ("Tencent Modifications"). 9# All Tencent Modifications are Copyright (C) THL A29 Limited.10 11# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT 12# except for the third-party components listed below. 13# Hunyuan 3D does not impose any additional limitations beyond what is outlined 14# in the repsective licenses of these third-party components. 15# Users must comply with all terms and conditions of original licenses of these third-party 16# components and must ensure that the usage of the third party components adheres to 17# all relevant laws and regulations. 18 19# For avoidance of doubts, Hunyuan 3D means the large language models and 20# their software and algorithms, including trained model weights, parameters (including 21# optimizer states), machine-learning model code, inference-enabling code, training-enabling code, 22# fine-tuning enabling code and other elements of the foregoing made publicly available 23# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.24 25import os26import warnings27import argparse28import gradio as gr29from glob import glob30import shutil31import torch32import numpy as np33from PIL import Image34from einops import rearrange35 36from infer import seed_everything, save_gif37from infer import Text2Image, Removebg, Image2Views, Views2Mesh, GifRenderer38 39warnings.simplefilter('ignore', category=UserWarning)40warnings.simplefilter('ignore', category=FutureWarning)41warnings.simplefilter('ignore', category=DeprecationWarning)42 43parser = argparse.ArgumentParser()44parser.add_argument("--use_lite", default=False, action="store_true")45parser.add_argument("--mv23d_cfg_path", default="./svrm/configs/svrm.yaml", type=str)46parser.add_argument("--mv23d_ckt_path", default="weights/svrm/svrm.safetensors", type=str)47parser.add_argument("--text2image_path", default="weights/hunyuanDiT", type=str)48parser.add_argument("--save_memory", default=False, action="store_true")49parser.add_argument("--device", default="cuda:0", type=str)50args = parser.parse_args()51 52################################################################53# initial setting54################################################################55 56CONST_PORT = 808057CONST_MAX_QUEUE = 158CONST_SERVER = '0.0.0.0'59 60CONST_HEADER = '''61<h2><b>Official π€ Gradio Demo</b></h2><h2><a href='https://github.com/tencent/Hunyuan3D-1' target='_blank'><b>Hunyuan3D-1.0: A Unified Framework for Text-to-3D and Image-to-3D62Generationr</b></a></h2>63Code: <a href='https://github.com/tencent/Hunyuan3D-1' target='_blank'>GitHub</a>. Techenical report: <a href='https://arxiv.org/abs/placeholder' target='_blank'>ArXiv</a>.64 65βοΈβοΈβοΈ**Important Notes:**66- By default, our demo can export a .obj mesh with vertex colors or a .glb mesh.67- If you select "texture mapping," it will export a .obj mesh with a texture map or a .glb mesh.68- If you select "render GIF," it will export a GIF image rendering of the .glb file.69- If the result is unsatisfactory, please try a different seed value (Default: 0).70'''71 72CONST_CITATION = r"""73If HunYuan3D-1 is helpful, please help to β the <a href='https://github.com/tencent/Hunyuan3D-1' target='_blank'>Github Repo</a>. Thanks! [](https://github.com/tencent/Hunyuan3D-1)74---75π **Citation**76If you find our work useful for your research or applications, please cite using this bibtex:77```bibtex78@misc{yang2024tencent,79 title={Tencent Hunyuan3D-1.0: A Unified Framework for Text-to-3D and Image-to-3D Generation},80 author={Xianghui Yang and Huiwen Shi and Bowen Zhang and Fan Yang and Jiacheng Wang and Hongxu Zhao and Xinhai Liu and Xinzhou Wang and Qingxiang Lin and Jiaao Yu and Lifu Wang and Zhuo Chen and Sicong Liu and Yuhong Liu and Yong Yang and Di Wang and Jie Jiang and Chunchao Guo},81 year={2024},82 eprint={2411.02293},83 archivePrefix={arXiv},84 primaryClass={cs.CV}85}86```87"""88 89################################################################90# prepare text examples and image examples91################################################################92 93def get_example_img_list():94 print('Loading example img list ...')95 return sorted(glob('./demos/example_*.png'))96 97def get_example_txt_list():98 print('Loading example txt list ...')99 txt_list = list()100 for line in open('./demos/example_list.txt'):101 txt_list.append(line.strip())102 return txt_list103 104example_is = get_example_img_list()105example_ts = get_example_txt_list()106 107################################################################108# initial models109################################################################110 111worker_xbg = Removebg()112print(f"loading {args.text2image_path}")113worker_t2i = Text2Image(114 pretrain = args.text2image_path, 115 device = args.device, 116 save_memory = args.save_memory117)118worker_i2v = Image2Views(119 use_lite = args.use_lite, 120 device = args.device,121 save_memory = args.save_memory122)123worker_v23 = Views2Mesh(124 args.mv23d_cfg_path, 125 args.mv23d_ckt_path, 126 use_lite = args.use_lite, 127 device = args.device,128 save_memory = args.save_memory129)130worker_gif = GifRenderer(args.device)131 132def stage_0_t2i(text, image, seed, step):133 os.makedirs('./outputs/app_output', exist_ok=True)134 exists = set(int(_) for _ in os.listdir('./outputs/app_output') if not _.startswith("."))135 if len(exists) == 30: shutil.rmtree(f"./outputs/app_output/0");cur_id = 0136 else: cur_id = min(set(range(30)) - exists)137 if os.path.exists(f"./outputs/app_output/{(cur_id + 1) % 30}"):138 shutil.rmtree(f"./outputs/app_output/{(cur_id + 1) % 30}")139 save_folder = f'./outputs/app_output/{cur_id}'140 os.makedirs(save_folder, exist_ok=True)141 142 dst = save_folder + '/img.png'143 144 if not text:145 if image is None: 146 return dst, save_folder147 raise gr.Error("Upload image or provide text ...")148 image.save(dst)149 return dst, save_folder150 151 image = worker_t2i(text, seed, step)152 image.save(dst)153 dst = worker_xbg(image, save_folder)154 return dst, save_folder155 156def stage_1_xbg(image, save_folder): 157 if isinstance(image, str):158 image = Image.open(image)159 dst = save_folder + '/img_nobg.png'160 rgba = worker_xbg(image)161 rgba.save(dst)162 return dst163 164def stage_2_i2v(image, seed, step, save_folder):165 if isinstance(image, str):166 image = Image.open(image)167 gif_dst = save_folder + '/views.gif'168 res_img, pils = worker_i2v(image, seed, step)169 save_gif(pils, gif_dst)170 views_img, cond_img = res_img[0], res_img[1]171 img_array = np.asarray(views_img, dtype=np.uint8)172 show_img = rearrange(img_array, '(n h) (m w) c -> (n m) h w c', n=3, m=2)173 show_img = show_img[worker_i2v.order, ...]174 show_img = rearrange(show_img, '(n m) h w c -> (n h) (m w) c', n=2, m=3)175 show_img = Image.fromarray(show_img) 176 return views_img, cond_img, show_img177 178def stage_3_v23(179 views_pil, 180 cond_pil, 181 seed, 182 save_folder,183 target_face_count = 30000,184 do_texture_mapping = True,185 do_render =True186): 187 do_texture_mapping = do_texture_mapping or do_render188 obj_dst = save_folder + '/mesh_with_colors.obj'189 glb_dst = save_folder + '/mesh.glb'190 worker_v23(191 views_pil, 192 cond_pil, 193 seed = seed, 194 save_folder = save_folder,195 target_face_count = target_face_count,196 do_texture_mapping = do_texture_mapping197 )198 return obj_dst, glb_dst199 200def stage_4_gif(obj_dst, save_folder, do_render_gif=True):201 if not do_render_gif: return None202 gif_dst = save_folder + '/output.gif'203 worker_gif(204 save_folder + '/mesh.obj',205 gif_dst_path = gif_dst206 )207 return gif_dst208# ===============================================================209# gradio display210# ===============================================================211with gr.Blocks() as demo:212 gr.Markdown(CONST_HEADER)213 with gr.Row(variant="panel"):214 with gr.Column(scale=2):215 with gr.Tab("Text to 3D"):216 with gr.Column():217 text = gr.TextArea('δΈεͺι»η½ηΈι΄ηηη«ε¨η½θ²θζ―δΈε±
δΈεηοΌεη°εΊε‘ιι£ζ Όεε―η±ζ°ε΄γ', lines=1, max_lines=10, label='Input text')218 with gr.Row():219 textgen_seed = gr.Number(value=0, label="T2I seed", precision=0)220 textgen_step = gr.Number(value=25, label="T2I step", precision=0)221 textgen_SEED = gr.Number(value=0, label="Gen seed", precision=0)222 textgen_STEP = gr.Number(value=50, label="Gen step", precision=0)223 textgen_max_faces = gr.Number(value=90000, label="max number of faces", precision=0)224 225 with gr.Row():226 textgen_do_texture_mapping = gr.Checkbox(label="texture mapping", value=False, interactive=True)227 textgen_do_render_gif = gr.Checkbox(label="Render gif", value=False, interactive=True)228 textgen_submit = gr.Button("Generate", variant="primary")229 230 with gr.Row():231 gr.Examples(examples=example_ts, inputs=[text], label="Txt examples", examples_per_page=10)232 233 with gr.Tab("Image to 3D"):234 with gr.Column():235 input_image = gr.Image(label="Input image",236 width=256, height=256, type="pil",237 image_mode="RGBA", sources="upload",238 interactive=True)239 with gr.Row(): 240 imggen_SEED = gr.Number(value=0, label="Gen seed", precision=0)241 imggen_STEP = gr.Number(value=50, label="Gen step", precision=0)242 imggen_max_faces = gr.Number(value=90000, label="max number of faces", precision=0)243 244 with gr.Row():245 imggen_do_texture_mapping = gr.Checkbox(label="texture mapping", value=False, interactive=True)246 imggen_do_render_gif = gr.Checkbox(label="Render gif", value=False, interactive=True)247 imggen_submit = gr.Button("Generate", variant="primary") 248 with gr.Row():249 gr.Examples(250 examples=example_is, 251 inputs=[input_image], 252 label="Img examples",253 examples_per_page=10254 )255 256 with gr.Column(scale=3):257 with gr.Row():258 with gr.Column(scale=2):259 rem_bg_image = gr.Image(label="No backgraound image", type="pil",260 image_mode="RGBA", interactive=False)261 with gr.Column(scale=3):262 result_image = gr.Image(label="Multi views", type="pil", interactive=False)263 264 with gr.Row(): 265 result_3dobj = gr.Model3D(266 clear_color=[0.0, 0.0, 0.0, 0.0],267 label="Output Obj",268 show_label=True,269 visible=True,270 camera_position=[90, 90, None],271 interactive=False272 )273 274 result_3dglb = gr.Model3D(275 clear_color=[0.0, 0.0, 0.0, 0.0],276 label="Output Glb",277 show_label=True,278 visible=True,279 camera_position=[90, 90, None],280 interactive=False281 )282 result_gif = gr.Image(label="Rendered GIF", interactive=False)283 284 with gr.Row(): 285 gr.Markdown("""286 We recommend downloading and opening Glb with 3D software, such as Blender, MeshLab, etc.287 288 Limited by gradio, Obj file here only be shown as vertex shading, but Glb can be texture shading.289 """)290 291#===============================================================292# gradio running code293#===============================================================294 295 none = gr.State(None)296 save_folder = gr.State()297 cond_image = gr.State()298 views_image = gr.State()299 text_image = gr.State()300 301 textgen_submit.click(302 fn=stage_0_t2i, inputs=[text, none, textgen_seed, textgen_step], 303 outputs=[rem_bg_image, save_folder],304 ).success(305 fn=stage_2_i2v, inputs=[rem_bg_image, textgen_SEED, textgen_STEP, save_folder], 306 outputs=[views_image, cond_image, result_image],307 ).success(308 fn=stage_3_v23, inputs=[views_image, cond_image, textgen_SEED, save_folder, 309 textgen_max_faces, textgen_do_texture_mapping,310 textgen_do_render_gif], 311 outputs=[result_3dobj, result_3dglb],312 ).success(313 fn=stage_4_gif, inputs=[result_3dglb, save_folder, textgen_do_render_gif], 314 outputs=[result_gif],315 ).success(lambda: print('Text_to_3D Done ...'))316 317 imggen_submit.click(318 fn=stage_0_t2i, inputs=[none, input_image, textgen_seed, textgen_step], 319 outputs=[text_image, save_folder],320 ).success(321 fn=stage_1_xbg, inputs=[text_image, save_folder], 322 outputs=[rem_bg_image],323 ).success(324 fn=stage_2_i2v, inputs=[rem_bg_image, imggen_SEED, imggen_STEP, save_folder], 325 outputs=[views_image, cond_image, result_image],326 ).success(327 fn=stage_3_v23, inputs=[views_image, cond_image, imggen_SEED, save_folder, 328 imggen_max_faces, imggen_do_texture_mapping, 329 imggen_do_render_gif], 330 outputs=[result_3dobj, result_3dglb],331 ).success(332 fn=stage_4_gif, inputs=[result_3dglb, save_folder, imggen_do_render_gif], 333 outputs=[result_gif],334 ).success(lambda: print('Image_to_3D Done ...'))335 336#===============================================================337# start gradio server338#===============================================================339 340 gr.Markdown(CONST_CITATION)341 demo.queue(max_size=CONST_MAX_QUEUE)342 demo.launch(server_name=CONST_SERVER, server_port=CONST_PORT)343 344 