Spaces:
Running
on
L40S
Running
on
L40S
JeffreyXiang
commited on
Commit
•
9880f3d
1
Parent(s):
f92f037
Update
Browse files- app.py +51 -6
- trellis/representations/gaussian/gaussian_model.py +9 -0
app.py
CHANGED
@@ -4,11 +4,14 @@ from gradio_litmodel3d import LitModel3D
|
|
4 |
|
5 |
import os
|
6 |
from typing import *
|
|
|
7 |
import numpy as np
|
8 |
import imageio
|
9 |
import uuid
|
|
|
10 |
from PIL import Image
|
11 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
|
|
12 |
from trellis.utils import render_utils, postprocessing_utils
|
13 |
|
14 |
|
@@ -25,6 +28,47 @@ def preprocess_image(image: Image.Image) -> Image.Image:
|
|
25 |
return pipeline.preprocess_image(image)
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
@spaces.GPU
|
29 |
def image_to_3d(image: Image.Image) -> Tuple[dict, str]:
|
30 |
"""
|
@@ -43,25 +87,26 @@ def image_to_3d(image: Image.Image) -> Tuple[dict, str]:
|
|
43 |
video_path = f"/tmp/Trellis-demo/{model_id}.mp4"
|
44 |
os.makedirs(os.path.dirname(video_path), exist_ok=True)
|
45 |
imageio.mimsave(video_path, video, fps=15)
|
46 |
-
|
47 |
-
return
|
48 |
|
49 |
|
50 |
@spaces.GPU
|
51 |
-
def extract_glb(
|
52 |
"""
|
53 |
Extract a GLB file from the 3D model.
|
54 |
|
55 |
Args:
|
56 |
-
|
57 |
mesh_simplify (float): The mesh simplification factor.
|
58 |
texture_size (int): The texture resolution.
|
59 |
|
60 |
Returns:
|
61 |
str: The path to the extracted GLB file.
|
62 |
"""
|
63 |
-
|
64 |
-
|
|
|
65 |
glb.export(glb_path)
|
66 |
return glb_path, glb_path
|
67 |
|
|
|
4 |
|
5 |
import os
|
6 |
from typing import *
|
7 |
+
import torch
|
8 |
import numpy as np
|
9 |
import imageio
|
10 |
import uuid
|
11 |
+
from easydict import EasyDict as edict
|
12 |
from PIL import Image
|
13 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
14 |
+
from trellis.representations import Gaussian, MeshExtractResult
|
15 |
from trellis.utils import render_utils, postprocessing_utils
|
16 |
|
17 |
|
|
|
28 |
return pipeline.preprocess_image(image)
|
29 |
|
30 |
|
31 |
+
def pack_state(gs: Gaussian, mesh: MeshExtractResult, model_id: str) -> dict:
|
32 |
+
return {
|
33 |
+
'gaussian': {
|
34 |
+
**gs.init_params,
|
35 |
+
'_xyz': gs._xyz.cpu().numpy(),
|
36 |
+
'_features_dc': gs._features_dc.cpu().numpy(),
|
37 |
+
'_scaling': gs._scaling.cpu().numpy(),
|
38 |
+
'_rotation': gs._rotation.cpu().numpy(),
|
39 |
+
'_opacity': gs._opacity.cpu().numpy(),
|
40 |
+
},
|
41 |
+
'mesh': {
|
42 |
+
'vertices': mesh.vertices.cpu().numpy(),
|
43 |
+
'faces': mesh.faces.cpu().numpy(),
|
44 |
+
},
|
45 |
+
'model_id': model_id,
|
46 |
+
}
|
47 |
+
|
48 |
+
|
49 |
+
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
50 |
+
gs = Gaussian(
|
51 |
+
aabb=state['gaussian']['aabb'],
|
52 |
+
sh_degree=state['gaussian']['sh_degree'],
|
53 |
+
mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
|
54 |
+
scaling_bias=state['gaussian']['scaling_bias'],
|
55 |
+
opacity_bias=state['gaussian']['opacity_bias'],
|
56 |
+
scaling_activation=state['gaussian']['scaling_activation'],
|
57 |
+
)
|
58 |
+
gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
|
59 |
+
gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
|
60 |
+
gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
|
61 |
+
gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
|
62 |
+
gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
|
63 |
+
|
64 |
+
mesh = edict(
|
65 |
+
vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
|
66 |
+
faces=torch.tensor(state['mesh']['faces'], device='cuda'),
|
67 |
+
)
|
68 |
+
|
69 |
+
return gs, mesh, state['model_id']
|
70 |
+
|
71 |
+
|
72 |
@spaces.GPU
|
73 |
def image_to_3d(image: Image.Image) -> Tuple[dict, str]:
|
74 |
"""
|
|
|
87 |
video_path = f"/tmp/Trellis-demo/{model_id}.mp4"
|
88 |
os.makedirs(os.path.dirname(video_path), exist_ok=True)
|
89 |
imageio.mimsave(video_path, video, fps=15)
|
90 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], model_id)
|
91 |
+
return state, video_path
|
92 |
|
93 |
|
94 |
@spaces.GPU
|
95 |
+
def extract_glb(state: dict, mesh_simplify: float, texture_size: int) -> Tuple[str, str]:
|
96 |
"""
|
97 |
Extract a GLB file from the 3D model.
|
98 |
|
99 |
Args:
|
100 |
+
state (dict): The state of the generated 3D model.
|
101 |
mesh_simplify (float): The mesh simplification factor.
|
102 |
texture_size (int): The texture resolution.
|
103 |
|
104 |
Returns:
|
105 |
str: The path to the extracted GLB file.
|
106 |
"""
|
107 |
+
gs, mesh, model_id = unpack_state(state)
|
108 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size)
|
109 |
+
glb_path = f"/tmp/Trellis-demo/{model_id}.glb"
|
110 |
glb.export(glb_path)
|
111 |
return glb_path, glb_path
|
112 |
|
trellis/representations/gaussian/gaussian_model.py
CHANGED
@@ -15,6 +15,15 @@ class Gaussian:
|
|
15 |
scaling_activation : str = "exp",
|
16 |
device='cuda'
|
17 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
self.sh_degree = sh_degree
|
19 |
self.active_sh_degree = sh_degree
|
20 |
self.mininum_kernel_size = mininum_kernel_size
|
|
|
15 |
scaling_activation : str = "exp",
|
16 |
device='cuda'
|
17 |
):
|
18 |
+
self.init_params = {
|
19 |
+
'aabb': aabb,
|
20 |
+
'sh_degree': sh_degree,
|
21 |
+
'mininum_kernel_size': mininum_kernel_size,
|
22 |
+
'scaling_bias': scaling_bias,
|
23 |
+
'opacity_bias': opacity_bias,
|
24 |
+
'scaling_activation': scaling_activation,
|
25 |
+
}
|
26 |
+
|
27 |
self.sh_degree = sh_degree
|
28 |
self.active_sh_degree = sh_degree
|
29 |
self.mininum_kernel_size = mininum_kernel_size
|