davidvgilmore commited on
Commit
9e3bab6
·
verified ·
1 Parent(s): 435c3c4

Upload minimal_demo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. minimal_demo.py +61 -0
minimal_demo.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import torch
7
+ from PIL import Image
8
+ import os
9
+
10
+ from hy3dgen.rembg import BackgroundRemover
11
+ from hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline, FaceReducer, FloaterRemover, DegenerateFaceRemover
12
+ from hy3dgen.text2image import HunyuanDiTPipeline
13
+
14
+
15
+ def image_to_3d(image_path='assets/demo.png'):
16
+ rembg = BackgroundRemover()
17
+ model_path = 'tencent/Hunyuan3D-2' # Use Hugging Face model path
18
+
19
+ image = Image.open(image_path)
20
+ image = image.resize((1024, 1024))
21
+
22
+ if image.mode == 'RGB':
23
+ image = rembg(image)
24
+
25
+ pipeline = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(model_path)
26
+
27
+ mesh = pipeline(image=image, num_inference_steps=30, mc_algo='mc',
28
+ generator=torch.manual_seed(2025))[0]
29
+ mesh = FloaterRemover()(mesh)
30
+ mesh = DegenerateFaceRemover()(mesh)
31
+ mesh = FaceReducer()(mesh)
32
+ mesh.export('mesh.glb')
33
+
34
+ try:
35
+ from hy3dgen.texgen import Hunyuan3DPaintPipeline
36
+ pipeline = Hunyuan3DPaintPipeline.from_pretrained(model_path)
37
+ mesh = pipeline(mesh, image=image)
38
+ mesh.export('texture.glb')
39
+ except Exception as e:
40
+ print(e)
41
+ print('Please try to install requirements by following README.md')
42
+
43
+
44
+ def text_to_3d(prompt='a car'):
45
+ rembg = BackgroundRemover()
46
+ t2i = HunyuanDiTPipeline('Tencent-Hunyuan--HunyuanDiT-v1.1-Diffusers-Distilled')
47
+ model_path = 'tencent/Hunyuan3D-2' # Use Hugging Face model path
48
+ i23d = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(model_path)
49
+
50
+ image = t2i(prompt)
51
+ image = rembg(image)
52
+ mesh = i23d(image, num_inference_steps=30, mc_algo='mc')[0]
53
+ mesh = FloaterRemover()(mesh)
54
+ mesh = DegenerateFaceRemover()(mesh)
55
+ mesh = FaceReducer()(mesh)
56
+ mesh.export('t2i_demo.glb')
57
+
58
+
59
+ if __name__ == '__main__':
60
+ image_to_3d()
61
+ # text_to_3d()