hunyuan3d-custom / minimal_demo.py
davidvgilmore's picture
Upload minimal_demo.py with huggingface_hub
9e3bab6 verified
# Open Source Model Licensed under the Apache License Version 2.0
# and Other Licenses of the Third-Party Components therein:
# The below Model in this distribution may have been modified by THL A29 Limited
# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
import torch
from PIL import Image
import os
from hy3dgen.rembg import BackgroundRemover
from hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline, FaceReducer, FloaterRemover, DegenerateFaceRemover
from hy3dgen.text2image import HunyuanDiTPipeline
def image_to_3d(image_path='assets/demo.png'):
rembg = BackgroundRemover()
model_path = 'tencent/Hunyuan3D-2' # Use Hugging Face model path
image = Image.open(image_path)
image = image.resize((1024, 1024))
if image.mode == 'RGB':
image = rembg(image)
pipeline = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(model_path)
mesh = pipeline(image=image, num_inference_steps=30, mc_algo='mc',
generator=torch.manual_seed(2025))[0]
mesh = FloaterRemover()(mesh)
mesh = DegenerateFaceRemover()(mesh)
mesh = FaceReducer()(mesh)
mesh.export('mesh.glb')
try:
from hy3dgen.texgen import Hunyuan3DPaintPipeline
pipeline = Hunyuan3DPaintPipeline.from_pretrained(model_path)
mesh = pipeline(mesh, image=image)
mesh.export('texture.glb')
except Exception as e:
print(e)
print('Please try to install requirements by following README.md')
def text_to_3d(prompt='a car'):
rembg = BackgroundRemover()
t2i = HunyuanDiTPipeline('Tencent-Hunyuan--HunyuanDiT-v1.1-Diffusers-Distilled')
model_path = 'tencent/Hunyuan3D-2' # Use Hugging Face model path
i23d = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(model_path)
image = t2i(prompt)
image = rembg(image)
mesh = i23d(image, num_inference_steps=30, mc_algo='mc')[0]
mesh = FloaterRemover()(mesh)
mesh = DegenerateFaceRemover()(mesh)
mesh = FaceReducer()(mesh)
mesh.export('t2i_demo.glb')
if __name__ == '__main__':
image_to_3d()
# text_to_3d()