Spaces:
Running
on
Zero
Running
on
Zero
gokaygokay
commited on
Upload 194 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +36 -0
- README.md +16 -0
- app.py +414 -0
- assets/example_image/T.png +0 -0
- assets/example_image/typical_building_building.png +0 -0
- assets/example_image/typical_building_castle.png +0 -0
- assets/example_image/typical_building_colorful_cottage.png +0 -0
- assets/example_image/typical_building_maya_pyramid.png +0 -0
- assets/example_image/typical_building_mushroom.png +0 -0
- assets/example_image/typical_building_space_station.png +0 -0
- assets/example_image/typical_creature_dragon.png +0 -0
- assets/example_image/typical_creature_elephant.png +0 -0
- assets/example_image/typical_creature_furry.png +0 -0
- assets/example_image/typical_creature_quadruped.png +0 -0
- assets/example_image/typical_creature_robot_crab.png +0 -0
- assets/example_image/typical_creature_robot_dinosour.png +0 -0
- assets/example_image/typical_creature_rock_monster.png +0 -0
- assets/example_image/typical_humanoid_block_robot.png +0 -0
- assets/example_image/typical_humanoid_dragonborn.png +0 -0
- assets/example_image/typical_humanoid_dwarf.png +0 -0
- assets/example_image/typical_humanoid_goblin.png +0 -0
- assets/example_image/typical_humanoid_mech.png +0 -0
- assets/example_image/typical_misc_crate.png +0 -0
- assets/example_image/typical_misc_fireplace.png +0 -0
- assets/example_image/typical_misc_gate.png +0 -0
- assets/example_image/typical_misc_lantern.png +0 -0
- assets/example_image/typical_misc_magicbook.png +0 -0
- assets/example_image/typical_misc_mailbox.png +0 -0
- assets/example_image/typical_misc_monster_chest.png +0 -0
- assets/example_image/typical_misc_paper_machine.png +0 -0
- assets/example_image/typical_misc_phonograph.png +0 -0
- assets/example_image/typical_misc_portal2.png +0 -0
- assets/example_image/typical_misc_storage_chest.png +0 -0
- assets/example_image/typical_misc_telephone.png +0 -0
- assets/example_image/typical_misc_television.png +0 -0
- assets/example_image/typical_misc_workbench.png +0 -0
- assets/example_image/typical_vehicle_biplane.png +0 -0
- assets/example_image/typical_vehicle_bulldozer.png +0 -0
- assets/example_image/typical_vehicle_cart.png +0 -0
- assets/example_image/typical_vehicle_excavator.png +0 -0
- assets/example_image/typical_vehicle_helicopter.png +0 -0
- assets/example_image/typical_vehicle_locomotive.png +0 -0
- assets/example_image/typical_vehicle_pirate_ship.png +0 -0
- assets/example_image/weatherworn_misc_paper_machine3.png +0 -0
- assets/example_multi_image/character_1.png +0 -0
- assets/example_multi_image/character_2.png +0 -0
- assets/example_multi_image/character_3.png +0 -0
- assets/example_multi_image/mushroom_1.png +0 -0
- assets/example_multi_image/mushroom_2.png +0 -0
- assets/example_multi_image/mushroom_3.png +0 -0
.gitattributes
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: TRELLIS
|
3 |
+
emoji: 🏢
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.44.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
short_description: Scalable and Versatile 3D Generation from images
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
15 |
+
|
16 |
+
Paper: https://huggingface.co/papers/2412.01506
|
app.py
ADDED
@@ -0,0 +1,414 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from gradio_litmodel3d import LitModel3D
|
4 |
+
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
os.environ['SPCONV_ALGO'] = 'native'
|
8 |
+
from typing import *
|
9 |
+
import torch
|
10 |
+
import numpy as np
|
11 |
+
import imageio
|
12 |
+
from easydict import EasyDict as edict
|
13 |
+
from PIL import Image
|
14 |
+
from trellis.pipelines import TrellisImageTo3DPipeline
|
15 |
+
from trellis.representations import Gaussian, MeshExtractResult
|
16 |
+
from trellis.utils import render_utils, postprocessing_utils
|
17 |
+
|
18 |
+
|
19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
21 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
22 |
+
|
23 |
+
|
24 |
+
def start_session(req: gr.Request):
|
25 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
26 |
+
os.makedirs(user_dir, exist_ok=True)
|
27 |
+
|
28 |
+
|
29 |
+
def end_session(req: gr.Request):
|
30 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
31 |
+
shutil.rmtree(user_dir)
|
32 |
+
|
33 |
+
|
34 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
35 |
+
"""
|
36 |
+
Preprocess the input image.
|
37 |
+
|
38 |
+
Args:
|
39 |
+
image (Image.Image): The input image.
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
Image.Image: The preprocessed image.
|
43 |
+
"""
|
44 |
+
processed_image = pipeline.preprocess_image(image)
|
45 |
+
return processed_image
|
46 |
+
|
47 |
+
|
48 |
+
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
49 |
+
"""
|
50 |
+
Preprocess a list of input images.
|
51 |
+
|
52 |
+
Args:
|
53 |
+
images (List[Tuple[Image.Image, str]]): The input images.
|
54 |
+
|
55 |
+
Returns:
|
56 |
+
List[Image.Image]: The preprocessed images.
|
57 |
+
"""
|
58 |
+
images = [image[0] for image in images]
|
59 |
+
processed_images = [pipeline.preprocess_image(image) for image in images]
|
60 |
+
return processed_images
|
61 |
+
|
62 |
+
|
63 |
+
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
64 |
+
return {
|
65 |
+
'gaussian': {
|
66 |
+
**gs.init_params,
|
67 |
+
'_xyz': gs._xyz.cpu().numpy(),
|
68 |
+
'_features_dc': gs._features_dc.cpu().numpy(),
|
69 |
+
'_scaling': gs._scaling.cpu().numpy(),
|
70 |
+
'_rotation': gs._rotation.cpu().numpy(),
|
71 |
+
'_opacity': gs._opacity.cpu().numpy(),
|
72 |
+
},
|
73 |
+
'mesh': {
|
74 |
+
'vertices': mesh.vertices.cpu().numpy(),
|
75 |
+
'faces': mesh.faces.cpu().numpy(),
|
76 |
+
},
|
77 |
+
}
|
78 |
+
|
79 |
+
|
80 |
+
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
81 |
+
gs = Gaussian(
|
82 |
+
aabb=state['gaussian']['aabb'],
|
83 |
+
sh_degree=state['gaussian']['sh_degree'],
|
84 |
+
mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
|
85 |
+
scaling_bias=state['gaussian']['scaling_bias'],
|
86 |
+
opacity_bias=state['gaussian']['opacity_bias'],
|
87 |
+
scaling_activation=state['gaussian']['scaling_activation'],
|
88 |
+
)
|
89 |
+
gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
|
90 |
+
gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
|
91 |
+
gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
|
92 |
+
gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
|
93 |
+
gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
|
94 |
+
|
95 |
+
mesh = edict(
|
96 |
+
vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
|
97 |
+
faces=torch.tensor(state['mesh']['faces'], device='cuda'),
|
98 |
+
)
|
99 |
+
|
100 |
+
return gs, mesh
|
101 |
+
|
102 |
+
|
103 |
+
def get_seed(randomize_seed: bool, seed: int) -> int:
|
104 |
+
"""
|
105 |
+
Get the random seed.
|
106 |
+
"""
|
107 |
+
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
108 |
+
|
109 |
+
|
110 |
+
@spaces.GPU
|
111 |
+
def image_to_3d(
|
112 |
+
image: Image.Image,
|
113 |
+
multiimages: List[Tuple[Image.Image, str]],
|
114 |
+
is_multiimage: bool,
|
115 |
+
seed: int,
|
116 |
+
ss_guidance_strength: float,
|
117 |
+
ss_sampling_steps: int,
|
118 |
+
slat_guidance_strength: float,
|
119 |
+
slat_sampling_steps: int,
|
120 |
+
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
121 |
+
req: gr.Request,
|
122 |
+
) -> Tuple[dict, str]:
|
123 |
+
"""
|
124 |
+
Convert an image to a 3D model.
|
125 |
+
|
126 |
+
Args:
|
127 |
+
image (Image.Image): The input image.
|
128 |
+
multiimages (List[Tuple[Image.Image, str]]): The input images in multi-image mode.
|
129 |
+
is_multiimage (bool): Whether is in multi-image mode.
|
130 |
+
seed (int): The random seed.
|
131 |
+
ss_guidance_strength (float): The guidance strength for sparse structure generation.
|
132 |
+
ss_sampling_steps (int): The number of sampling steps for sparse structure generation.
|
133 |
+
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
134 |
+
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
135 |
+
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
136 |
+
|
137 |
+
Returns:
|
138 |
+
dict: The information of the generated 3D model.
|
139 |
+
str: The path to the video of the 3D model.
|
140 |
+
"""
|
141 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
142 |
+
if not is_multiimage:
|
143 |
+
outputs = pipeline.run(
|
144 |
+
image,
|
145 |
+
seed=seed,
|
146 |
+
formats=["gaussian", "mesh"],
|
147 |
+
preprocess_image=False,
|
148 |
+
sparse_structure_sampler_params={
|
149 |
+
"steps": ss_sampling_steps,
|
150 |
+
"cfg_strength": ss_guidance_strength,
|
151 |
+
},
|
152 |
+
slat_sampler_params={
|
153 |
+
"steps": slat_sampling_steps,
|
154 |
+
"cfg_strength": slat_guidance_strength,
|
155 |
+
},
|
156 |
+
)
|
157 |
+
else:
|
158 |
+
outputs = pipeline.run_multi_image(
|
159 |
+
[image[0] for image in multiimages],
|
160 |
+
seed=seed,
|
161 |
+
formats=["gaussian", "mesh"],
|
162 |
+
preprocess_image=False,
|
163 |
+
sparse_structure_sampler_params={
|
164 |
+
"steps": ss_sampling_steps,
|
165 |
+
"cfg_strength": ss_guidance_strength,
|
166 |
+
},
|
167 |
+
slat_sampler_params={
|
168 |
+
"steps": slat_sampling_steps,
|
169 |
+
"cfg_strength": slat_guidance_strength,
|
170 |
+
},
|
171 |
+
mode=multiimage_algo,
|
172 |
+
)
|
173 |
+
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
174 |
+
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
175 |
+
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
176 |
+
video_path = os.path.join(user_dir, 'sample.mp4')
|
177 |
+
imageio.mimsave(video_path, video, fps=15)
|
178 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
179 |
+
torch.cuda.empty_cache()
|
180 |
+
return state, video_path
|
181 |
+
|
182 |
+
|
183 |
+
@spaces.GPU(duration=90)
|
184 |
+
def extract_glb(
|
185 |
+
state: dict,
|
186 |
+
mesh_simplify: float,
|
187 |
+
texture_size: int,
|
188 |
+
req: gr.Request,
|
189 |
+
) -> Tuple[str, str]:
|
190 |
+
"""
|
191 |
+
Extract a GLB file from the 3D model.
|
192 |
+
|
193 |
+
Args:
|
194 |
+
state (dict): The state of the generated 3D model.
|
195 |
+
mesh_simplify (float): The mesh simplification factor.
|
196 |
+
texture_size (int): The texture resolution.
|
197 |
+
|
198 |
+
Returns:
|
199 |
+
str: The path to the extracted GLB file.
|
200 |
+
"""
|
201 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
202 |
+
gs, mesh = unpack_state(state)
|
203 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
204 |
+
glb_path = os.path.join(user_dir, 'sample.glb')
|
205 |
+
glb.export(glb_path)
|
206 |
+
torch.cuda.empty_cache()
|
207 |
+
return glb_path, glb_path
|
208 |
+
|
209 |
+
|
210 |
+
@spaces.GPU
|
211 |
+
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
212 |
+
"""
|
213 |
+
Extract a Gaussian file from the 3D model.
|
214 |
+
|
215 |
+
Args:
|
216 |
+
state (dict): The state of the generated 3D model.
|
217 |
+
|
218 |
+
Returns:
|
219 |
+
str: The path to the extracted Gaussian file.
|
220 |
+
"""
|
221 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
222 |
+
gs, _ = unpack_state(state)
|
223 |
+
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
224 |
+
gs.save_ply(gaussian_path)
|
225 |
+
torch.cuda.empty_cache()
|
226 |
+
return gaussian_path, gaussian_path
|
227 |
+
|
228 |
+
|
229 |
+
def prepare_multi_example() -> List[Image.Image]:
|
230 |
+
multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
|
231 |
+
images = []
|
232 |
+
for case in multi_case:
|
233 |
+
_images = []
|
234 |
+
for i in range(1, 4):
|
235 |
+
img = Image.open(f'assets/example_multi_image/{case}_{i}.png')
|
236 |
+
W, H = img.size
|
237 |
+
img = img.resize((int(W / H * 512), 512))
|
238 |
+
_images.append(np.array(img))
|
239 |
+
images.append(Image.fromarray(np.concatenate(_images, axis=1)))
|
240 |
+
return images
|
241 |
+
|
242 |
+
|
243 |
+
def split_image(image: Image.Image) -> List[Image.Image]:
|
244 |
+
"""
|
245 |
+
Split an image into multiple views.
|
246 |
+
"""
|
247 |
+
image = np.array(image)
|
248 |
+
alpha = image[..., 3]
|
249 |
+
alpha = np.any(alpha>0, axis=0)
|
250 |
+
start_pos = np.where(~alpha[:-1] & alpha[1:])[0].tolist()
|
251 |
+
end_pos = np.where(alpha[:-1] & ~alpha[1:])[0].tolist()
|
252 |
+
images = []
|
253 |
+
for s, e in zip(start_pos, end_pos):
|
254 |
+
images.append(Image.fromarray(image[:, s:e+1]))
|
255 |
+
return [preprocess_image(image) for image in images]
|
256 |
+
|
257 |
+
|
258 |
+
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
259 |
+
gr.Markdown("""
|
260 |
+
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
261 |
+
* Upload an image and click "Generate" to create a 3D asset. If the image has alpha channel, it be used as the mask. Otherwise, we use `rembg` to remove the background.
|
262 |
+
* If you find the generated 3D asset satisfactory, click "Extract GLB" to extract the GLB file and download it.
|
263 |
+
|
264 |
+
✨New: 1) Experimental multi-image support. 2) Gaussian file extraction.
|
265 |
+
""")
|
266 |
+
|
267 |
+
with gr.Row():
|
268 |
+
with gr.Column():
|
269 |
+
with gr.Tabs() as input_tabs:
|
270 |
+
with gr.Tab(label="Single Image", id=0) as single_image_input_tab:
|
271 |
+
image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=300)
|
272 |
+
with gr.Tab(label="Multiple Images", id=1) as multiimage_input_tab:
|
273 |
+
multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)
|
274 |
+
gr.Markdown("""
|
275 |
+
Input different views of the object in separate images.
|
276 |
+
|
277 |
+
*NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
|
278 |
+
""")
|
279 |
+
|
280 |
+
with gr.Accordion(label="Generation Settings", open=False):
|
281 |
+
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
282 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
283 |
+
gr.Markdown("Stage 1: Sparse Structure Generation")
|
284 |
+
with gr.Row():
|
285 |
+
ss_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=7.5, step=0.1)
|
286 |
+
ss_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
287 |
+
gr.Markdown("Stage 2: Structured Latent Generation")
|
288 |
+
with gr.Row():
|
289 |
+
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
290 |
+
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
291 |
+
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
|
292 |
+
|
293 |
+
generate_btn = gr.Button("Generate")
|
294 |
+
|
295 |
+
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
296 |
+
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
297 |
+
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
298 |
+
|
299 |
+
with gr.Row():
|
300 |
+
extract_glb_btn = gr.Button("Extract GLB", interactive=False)
|
301 |
+
extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
|
302 |
+
gr.Markdown("""
|
303 |
+
*NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
|
304 |
+
""")
|
305 |
+
|
306 |
+
with gr.Column():
|
307 |
+
video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
|
308 |
+
model_output = LitModel3D(label="Extracted GLB/Gaussian", exposure=10.0, height=300)
|
309 |
+
|
310 |
+
with gr.Row():
|
311 |
+
download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
|
312 |
+
download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
|
313 |
+
|
314 |
+
is_multiimage = gr.State(False)
|
315 |
+
output_buf = gr.State()
|
316 |
+
|
317 |
+
# Example images at the bottom of the page
|
318 |
+
with gr.Row() as single_image_example:
|
319 |
+
examples = gr.Examples(
|
320 |
+
examples=[
|
321 |
+
f'assets/example_image/{image}'
|
322 |
+
for image in os.listdir("assets/example_image")
|
323 |
+
],
|
324 |
+
inputs=[image_prompt],
|
325 |
+
fn=preprocess_image,
|
326 |
+
outputs=[image_prompt],
|
327 |
+
run_on_click=True,
|
328 |
+
examples_per_page=64,
|
329 |
+
)
|
330 |
+
with gr.Row(visible=False) as multiimage_example:
|
331 |
+
examples_multi = gr.Examples(
|
332 |
+
examples=prepare_multi_example(),
|
333 |
+
inputs=[image_prompt],
|
334 |
+
fn=split_image,
|
335 |
+
outputs=[multiimage_prompt],
|
336 |
+
run_on_click=True,
|
337 |
+
examples_per_page=8,
|
338 |
+
)
|
339 |
+
|
340 |
+
# Handlers
|
341 |
+
demo.load(start_session)
|
342 |
+
demo.unload(end_session)
|
343 |
+
|
344 |
+
single_image_input_tab.select(
|
345 |
+
lambda: tuple([False, gr.Row.update(visible=True), gr.Row.update(visible=False)]),
|
346 |
+
outputs=[is_multiimage, single_image_example, multiimage_example]
|
347 |
+
)
|
348 |
+
multiimage_input_tab.select(
|
349 |
+
lambda: tuple([True, gr.Row.update(visible=False), gr.Row.update(visible=True)]),
|
350 |
+
outputs=[is_multiimage, single_image_example, multiimage_example]
|
351 |
+
)
|
352 |
+
|
353 |
+
image_prompt.upload(
|
354 |
+
preprocess_image,
|
355 |
+
inputs=[image_prompt],
|
356 |
+
outputs=[image_prompt],
|
357 |
+
)
|
358 |
+
multiimage_prompt.upload(
|
359 |
+
preprocess_images,
|
360 |
+
inputs=[multiimage_prompt],
|
361 |
+
outputs=[multiimage_prompt],
|
362 |
+
)
|
363 |
+
|
364 |
+
generate_btn.click(
|
365 |
+
get_seed,
|
366 |
+
inputs=[randomize_seed, seed],
|
367 |
+
outputs=[seed],
|
368 |
+
).then(
|
369 |
+
image_to_3d,
|
370 |
+
inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo],
|
371 |
+
outputs=[output_buf, video_output],
|
372 |
+
).then(
|
373 |
+
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
374 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
375 |
+
)
|
376 |
+
|
377 |
+
video_output.clear(
|
378 |
+
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
|
379 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
380 |
+
)
|
381 |
+
|
382 |
+
extract_glb_btn.click(
|
383 |
+
extract_glb,
|
384 |
+
inputs=[output_buf, mesh_simplify, texture_size],
|
385 |
+
outputs=[model_output, download_glb],
|
386 |
+
).then(
|
387 |
+
lambda: gr.Button(interactive=True),
|
388 |
+
outputs=[download_glb],
|
389 |
+
)
|
390 |
+
|
391 |
+
extract_gs_btn.click(
|
392 |
+
extract_gaussian,
|
393 |
+
inputs=[output_buf],
|
394 |
+
outputs=[model_output, download_gs],
|
395 |
+
).then(
|
396 |
+
lambda: gr.Button(interactive=True),
|
397 |
+
outputs=[download_gs],
|
398 |
+
)
|
399 |
+
|
400 |
+
model_output.clear(
|
401 |
+
lambda: gr.Button(interactive=False),
|
402 |
+
outputs=[download_glb],
|
403 |
+
)
|
404 |
+
|
405 |
+
|
406 |
+
# Launch the Gradio app
|
407 |
+
if __name__ == "__main__":
|
408 |
+
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
409 |
+
pipeline.cuda()
|
410 |
+
try:
|
411 |
+
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
412 |
+
except:
|
413 |
+
pass
|
414 |
+
demo.launch()
|
assets/example_image/T.png
ADDED
assets/example_image/typical_building_building.png
ADDED
assets/example_image/typical_building_castle.png
ADDED
assets/example_image/typical_building_colorful_cottage.png
ADDED
assets/example_image/typical_building_maya_pyramid.png
ADDED
assets/example_image/typical_building_mushroom.png
ADDED
assets/example_image/typical_building_space_station.png
ADDED
assets/example_image/typical_creature_dragon.png
ADDED
assets/example_image/typical_creature_elephant.png
ADDED
assets/example_image/typical_creature_furry.png
ADDED
assets/example_image/typical_creature_quadruped.png
ADDED
assets/example_image/typical_creature_robot_crab.png
ADDED
assets/example_image/typical_creature_robot_dinosour.png
ADDED
assets/example_image/typical_creature_rock_monster.png
ADDED
assets/example_image/typical_humanoid_block_robot.png
ADDED
assets/example_image/typical_humanoid_dragonborn.png
ADDED
assets/example_image/typical_humanoid_dwarf.png
ADDED
assets/example_image/typical_humanoid_goblin.png
ADDED
assets/example_image/typical_humanoid_mech.png
ADDED
assets/example_image/typical_misc_crate.png
ADDED
assets/example_image/typical_misc_fireplace.png
ADDED
assets/example_image/typical_misc_gate.png
ADDED
assets/example_image/typical_misc_lantern.png
ADDED
assets/example_image/typical_misc_magicbook.png
ADDED
assets/example_image/typical_misc_mailbox.png
ADDED
assets/example_image/typical_misc_monster_chest.png
ADDED
assets/example_image/typical_misc_paper_machine.png
ADDED
assets/example_image/typical_misc_phonograph.png
ADDED
assets/example_image/typical_misc_portal2.png
ADDED
assets/example_image/typical_misc_storage_chest.png
ADDED
assets/example_image/typical_misc_telephone.png
ADDED
assets/example_image/typical_misc_television.png
ADDED
assets/example_image/typical_misc_workbench.png
ADDED
assets/example_image/typical_vehicle_biplane.png
ADDED
assets/example_image/typical_vehicle_bulldozer.png
ADDED
assets/example_image/typical_vehicle_cart.png
ADDED
assets/example_image/typical_vehicle_excavator.png
ADDED
assets/example_image/typical_vehicle_helicopter.png
ADDED
assets/example_image/typical_vehicle_locomotive.png
ADDED
assets/example_image/typical_vehicle_pirate_ship.png
ADDED
assets/example_image/weatherworn_misc_paper_machine3.png
ADDED
assets/example_multi_image/character_1.png
ADDED
assets/example_multi_image/character_2.png
ADDED
assets/example_multi_image/character_3.png
ADDED
assets/example_multi_image/mushroom_1.png
ADDED
assets/example_multi_image/mushroom_2.png
ADDED
assets/example_multi_image/mushroom_3.png
ADDED