Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
from gradio_litmodel3d import LitModel3D
|
|
|
|
| 4 |
import os
|
| 5 |
import shutil
|
|
|
|
| 6 |
os.environ['SPCONV_ALGO'] = 'native'
|
| 7 |
from typing import *
|
| 8 |
import torch
|
|
@@ -13,303 +15,157 @@ 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 |
-
|
| 18 |
-
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
| 19 |
-
os.makedirs(TMP_DIR, exist_ok=True)
|
| 20 |
-
|
| 21 |
-
def start_session(req: gr.Request):
|
| 22 |
-
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 23 |
-
os.makedirs(user_dir, exist_ok=True)
|
| 24 |
-
|
| 25 |
-
def end_session(req: gr.Request):
|
| 26 |
-
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 27 |
-
shutil.rmtree(user_dir)
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
"""
|
| 31 |
-
Preprocess the input image.
|
| 32 |
-
Args:
|
| 33 |
-
image (Image.Image): The input image.
|
| 34 |
-
Returns:
|
| 35 |
-
Image.Image: The preprocessed image.
|
| 36 |
"""
|
| 37 |
-
|
| 38 |
-
return processed_image
|
| 39 |
-
|
| 40 |
-
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
| 41 |
"""
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
},
|
| 64 |
-
'mesh': {
|
| 65 |
-
'vertices': mesh.vertices.cpu().numpy(),
|
| 66 |
-
'faces': mesh.faces.cpu().numpy(),
|
| 67 |
-
},
|
| 68 |
-
}
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
-
return
|
| 91 |
-
|
| 92 |
-
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 93 |
-
"""
|
| 94 |
-
Get the random seed.
|
| 95 |
-
"""
|
| 96 |
-
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
| 97 |
-
|
| 98 |
-
@spaces.GPU
|
| 99 |
-
def image_to_3d(
|
| 100 |
-
image: Image.Image,
|
| 101 |
-
multiimages: List[Tuple[Image.Image, str]],
|
| 102 |
-
is_multiimage: bool,
|
| 103 |
-
seed: int,
|
| 104 |
-
ss_guidance_strength: float,
|
| 105 |
-
ss_sampling_steps: int,
|
| 106 |
-
slat_guidance_strength: float,
|
| 107 |
-
slat_sampling_steps: int,
|
| 108 |
-
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 109 |
-
req: gr.Request,
|
| 110 |
-
) -> Tuple[dict, str]:
|
| 111 |
-
"""
|
| 112 |
-
Convert an image to a 3D model.
|
| 113 |
-
Args:
|
| 114 |
-
image (Image.Image): The input image.
|
| 115 |
-
multiimages (List[Tuple[Image.Image, str]]): The input images in multi-image mode.
|
| 116 |
-
is_multiimage (bool): Whether is in multi-image mode.
|
| 117 |
-
seed (int): The random seed.
|
| 118 |
-
ss_guidance_strength (float): The guidance strength for sparse structure generation.
|
| 119 |
-
ss_sampling_steps (int): The number of sampling steps for sparse structure generation.
|
| 120 |
-
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
| 121 |
-
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
| 122 |
-
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
| 123 |
-
Returns:
|
| 124 |
-
dict: The information of the generated 3D model.
|
| 125 |
-
str: The path to the video of the 3D model.
|
| 126 |
-
"""
|
| 127 |
-
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 128 |
-
if not is_multiimage:
|
| 129 |
-
outputs = pipeline.run(
|
| 130 |
-
image,
|
| 131 |
-
seed=seed,
|
| 132 |
-
formats=["gaussian", "mesh"],
|
| 133 |
-
preprocess_image=False,
|
| 134 |
-
sparse_structure_sampler_params={
|
| 135 |
-
"steps": ss_sampling_steps,
|
| 136 |
-
"cfg_strength": ss_guidance_strength,
|
| 137 |
-
},
|
| 138 |
-
slat_sampler_params={
|
| 139 |
-
"steps": slat_sampling_steps,
|
| 140 |
-
"cfg_strength": slat_guidance_strength,
|
| 141 |
-
},
|
| 142 |
-
)
|
| 143 |
-
else:
|
| 144 |
-
outputs = pipeline.run_multi_image(
|
| 145 |
-
[image[0] for image in multiimages],
|
| 146 |
-
seed=seed,
|
| 147 |
-
formats=["gaussian", "mesh"],
|
| 148 |
-
preprocess_image=False,
|
| 149 |
-
sparse_structure_sampler_params={
|
| 150 |
-
"steps": ss_sampling_steps,
|
| 151 |
-
"cfg_strength": ss_guidance_strength,
|
| 152 |
-
},
|
| 153 |
-
slat_sampler_params={
|
| 154 |
-
"steps": slat_sampling_steps,
|
| 155 |
-
"cfg_strength": slat_guidance_strength,
|
| 156 |
-
},
|
| 157 |
-
mode=multiimage_algo,
|
| 158 |
-
)
|
| 159 |
-
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 160 |
-
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 161 |
-
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 162 |
-
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 163 |
-
imageio.mimsave(video_path, video, fps=15)
|
| 164 |
-
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
| 165 |
-
torch.cuda.empty_cache()
|
| 166 |
-
return state, video_path
|
| 167 |
|
|
|
|
| 168 |
@spaces.GPU(duration=90)
|
| 169 |
def extract_glb(
|
| 170 |
state: dict,
|
| 171 |
mesh_simplify: float,
|
| 172 |
texture_size: int,
|
|
|
|
|
|
|
|
|
|
| 173 |
req: gr.Request,
|
| 174 |
) -> Tuple[str, str]:
|
| 175 |
"""
|
| 176 |
-
Extract a GLB file from the 3D model.
|
| 177 |
-
Args:
|
| 178 |
-
state (dict): The state of the generated 3D model.
|
| 179 |
-
mesh_simplify (float): The mesh simplification factor.
|
| 180 |
-
texture_size (int): The texture resolution.
|
| 181 |
-
Returns:
|
| 182 |
-
str: The path to the extracted GLB file.
|
| 183 |
"""
|
| 184 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 185 |
gs, mesh = unpack_state(state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 187 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 188 |
glb.export(glb_path)
|
| 189 |
torch.cuda.empty_cache()
|
| 190 |
return glb_path, glb_path
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
Extract a Gaussian file from the 3D model.
|
| 196 |
-
Args:
|
| 197 |
-
state (dict): The state of the generated 3D model.
|
| 198 |
-
Returns:
|
| 199 |
-
str: The path to the extracted Gaussian file.
|
| 200 |
-
"""
|
| 201 |
-
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 202 |
-
gs, _ = unpack_state(state)
|
| 203 |
-
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
| 204 |
-
gs.save_ply(gaussian_path)
|
| 205 |
-
torch.cuda.empty_cache()
|
| 206 |
-
return gaussian_path, gaussian_path
|
| 207 |
-
|
| 208 |
-
with gr.Blocks(theme=gr.themes.Default(), delete_cache=(600, 600)) as demo:
|
| 209 |
-
with gr.Row():
|
| 210 |
-
with gr.Column():
|
| 211 |
-
with gr.Tabs() as input_tabs:
|
| 212 |
-
with gr.Tab(label="Single Image", id=0) as single_image_input_tab:
|
| 213 |
-
image_prompt = gr.Image(label="Image Prompt", format="png", image_mode="RGBA", type="pil", height=300)
|
| 214 |
-
with gr.Tab(label="Multiple Images", id=1) as multiimage_input_tab:
|
| 215 |
-
multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)
|
| 216 |
-
|
| 217 |
-
with gr.Accordion(label="Generation Settings", open=False):
|
| 218 |
-
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 219 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 220 |
-
with gr.Row():
|
| 221 |
-
ss_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=7.5, step=0.1)
|
| 222 |
-
ss_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 223 |
-
with gr.Row():
|
| 224 |
-
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
| 225 |
-
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 226 |
-
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
|
| 227 |
-
|
| 228 |
-
generate_btn = gr.Button("Generate", variant="primary")
|
| 229 |
-
|
| 230 |
-
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
| 231 |
-
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
| 232 |
-
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
| 233 |
-
|
| 234 |
-
with gr.Row():
|
| 235 |
-
extract_glb_btn = gr.Button("Extract GLB", interactive=False)
|
| 236 |
-
extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
|
| 237 |
-
|
| 238 |
-
with gr.Column():
|
| 239 |
-
video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
|
| 240 |
-
model_output = LitModel3D(label="Extracted GLB/Gaussian", exposure=10.0, height=300)
|
| 241 |
-
|
| 242 |
-
with gr.Row():
|
| 243 |
-
download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
|
| 244 |
-
download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
|
| 254 |
-
lambda: False,
|
| 255 |
-
outputs=[is_multiimage]
|
| 256 |
-
)
|
| 257 |
-
multiimage_input_tab.select(
|
| 258 |
-
lambda: True,
|
| 259 |
-
outputs=[is_multiimage]
|
| 260 |
-
)
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
| 266 |
)
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
inputs=[multiimage_prompt],
|
| 270 |
-
outputs=[multiimage_prompt],
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
generate_btn.click(
|
| 274 |
-
get_seed,
|
| 275 |
-
inputs=[randomize_seed, seed],
|
| 276 |
-
outputs=[seed],
|
| 277 |
-
).then(
|
| 278 |
-
image_to_3d,
|
| 279 |
-
inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo],
|
| 280 |
-
outputs=[output_buf, video_output],
|
| 281 |
-
).then(
|
| 282 |
-
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
| 283 |
-
outputs=[extract_glb_btn, extract_gs_btn],
|
| 284 |
-
)
|
| 285 |
-
|
| 286 |
-
video_output.clear(
|
| 287 |
-
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
|
| 288 |
-
outputs=[extract_glb_btn, extract_gs_btn],
|
| 289 |
-
)
|
| 290 |
-
|
| 291 |
extract_glb_btn.click(
|
| 292 |
extract_glb,
|
| 293 |
-
inputs=[output_buf, mesh_simplify, texture_size],
|
| 294 |
outputs=[model_output, download_glb],
|
| 295 |
).then(
|
| 296 |
lambda: gr.Button(interactive=True),
|
| 297 |
outputs=[download_glb],
|
| 298 |
)
|
| 299 |
|
| 300 |
-
|
| 301 |
-
extract_gaussian,
|
| 302 |
-
inputs=[output_buf],
|
| 303 |
-
outputs=[model_output, download_gs],
|
| 304 |
-
).then(
|
| 305 |
-
lambda: gr.Button(interactive=True),
|
| 306 |
-
outputs=[download_gs],
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
model_output.clear(
|
| 310 |
-
lambda: gr.Button(interactive=False),
|
| 311 |
-
outputs=[download_glb],
|
| 312 |
-
)
|
| 313 |
|
| 314 |
# Launch the Gradio app
|
| 315 |
if __name__ == "__main__":
|
|
@@ -319,4 +175,4 @@ if __name__ == "__main__":
|
|
| 319 |
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
| 320 |
except:
|
| 321 |
pass
|
| 322 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
from gradio_litmodel3d import LitModel3D
|
| 4 |
+
|
| 5 |
import os
|
| 6 |
import shutil
|
| 7 |
+
import trimesh # New import
|
| 8 |
os.environ['SPCONV_ALGO'] = 'native'
|
| 9 |
from typing import *
|
| 10 |
import torch
|
|
|
|
| 15 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
| 16 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 17 |
from trellis.utils import render_utils, postprocessing_utils
|
| 18 |
+
from scipy.spatial import ConvexHull # New import
|
| 19 |
|
| 20 |
+
# [Previous imports and constants remain the same...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def optimize_building_mesh(mesh, angle_threshold=15, planar_threshold=0.02):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
+
Optimize a building mesh by preserving architectural features while reducing complexity.
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
+
# Convert vertices to numpy array for processing
|
| 27 |
+
vertices = np.array(mesh.vertices)
|
| 28 |
+
faces = np.array(mesh.faces)
|
| 29 |
+
|
| 30 |
+
# 1. Detect planar surfaces
|
| 31 |
+
normals = mesh.face_normals
|
| 32 |
+
planar_groups = []
|
| 33 |
+
processed = set()
|
| 34 |
|
| 35 |
+
for i in range(len(faces)):
|
| 36 |
+
if i in processed:
|
| 37 |
+
continue
|
| 38 |
|
| 39 |
+
# Find connected faces with similar normals
|
| 40 |
+
similar_faces = {i}
|
| 41 |
+
stack = [i]
|
| 42 |
+
while stack:
|
| 43 |
+
current = stack.pop()
|
| 44 |
+
neighbors = mesh.face_adjacency[mesh.face_adjacency[:,0] == current][:,1]
|
| 45 |
+
for n in neighbors:
|
| 46 |
+
if n not in processed:
|
| 47 |
+
angle = np.arccos(np.dot(normals[current], normals[n])) * 180 / np.pi
|
| 48 |
+
if angle < angle_threshold:
|
| 49 |
+
similar_faces.add(n)
|
| 50 |
+
stack.append(n)
|
| 51 |
+
processed.add(n)
|
| 52 |
+
|
| 53 |
+
if len(similar_faces) > 0:
|
| 54 |
+
planar_groups.append(list(similar_faces))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# 2. Simplify each planar group while preserving edges
|
| 57 |
+
new_vertices = []
|
| 58 |
+
new_faces = []
|
| 59 |
+
vertex_map = {}
|
| 60 |
+
|
| 61 |
+
for group in planar_groups:
|
| 62 |
+
# Get vertices for this group
|
| 63 |
+
group_faces = faces[group]
|
| 64 |
+
group_verts = vertices[np.unique(group_faces)]
|
| 65 |
+
|
| 66 |
+
# Find best fitting plane
|
| 67 |
+
centroid = np.mean(group_verts, axis=0)
|
| 68 |
+
_, _, vh = np.linalg.svd(group_verts - centroid)
|
| 69 |
+
normal = vh[2]
|
| 70 |
+
|
| 71 |
+
# Project vertices to plane and simplify
|
| 72 |
+
projected = group_verts - np.dot(group_verts - centroid, normal)[:, np.newaxis] * normal
|
| 73 |
+
|
| 74 |
+
# Create simplified convex hull for this section
|
| 75 |
+
hull = ConvexHull(projected[:,:2])
|
| 76 |
+
hull_vertices = projected[hull.vertices]
|
| 77 |
+
|
| 78 |
+
# Add to new mesh
|
| 79 |
+
start_idx = len(new_vertices)
|
| 80 |
+
new_vertices.extend(hull_vertices)
|
| 81 |
+
|
| 82 |
+
# Triangulate the hull
|
| 83 |
+
for i in range(1, len(hull_vertices) - 1):
|
| 84 |
+
new_faces.append([start_idx, start_idx + i, start_idx + i + 1])
|
| 85 |
|
| 86 |
+
# 3. Create new optimized mesh
|
| 87 |
+
optimized_mesh = trimesh.Trimesh(
|
| 88 |
+
vertices=np.array(new_vertices),
|
| 89 |
+
faces=np.array(new_faces)
|
| 90 |
)
|
| 91 |
|
| 92 |
+
return optimized_mesh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Modify the existing extract_glb function
|
| 95 |
@spaces.GPU(duration=90)
|
| 96 |
def extract_glb(
|
| 97 |
state: dict,
|
| 98 |
mesh_simplify: float,
|
| 99 |
texture_size: int,
|
| 100 |
+
is_building: bool, # New parameter
|
| 101 |
+
angle_threshold: float, # New parameter
|
| 102 |
+
planar_threshold: float, # New parameter
|
| 103 |
req: gr.Request,
|
| 104 |
) -> Tuple[str, str]:
|
| 105 |
"""
|
| 106 |
+
Extract a GLB file from the 3D model with optional building optimization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
"""
|
| 108 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 109 |
gs, mesh = unpack_state(state)
|
| 110 |
+
|
| 111 |
+
if is_building:
|
| 112 |
+
# Convert to trimesh for optimization
|
| 113 |
+
trimesh_mesh = trimesh.Trimesh(
|
| 114 |
+
vertices=mesh.vertices.cpu().numpy(),
|
| 115 |
+
faces=mesh.faces.cpu().numpy()
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Apply building-specific optimization
|
| 119 |
+
optimized_mesh = optimize_building_mesh(
|
| 120 |
+
trimesh_mesh,
|
| 121 |
+
angle_threshold=angle_threshold,
|
| 122 |
+
planar_threshold=planar_threshold
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Convert back to original format
|
| 126 |
+
mesh.vertices = torch.tensor(optimized_mesh.vertices, device='cuda')
|
| 127 |
+
mesh.faces = torch.tensor(optimized_mesh.faces, device='cuda')
|
| 128 |
+
|
| 129 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 130 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 131 |
glb.export(glb_path)
|
| 132 |
torch.cuda.empty_cache()
|
| 133 |
return glb_path, glb_path
|
| 134 |
|
| 135 |
+
# Modify the main UI code section
|
| 136 |
+
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
| 137 |
+
# [Previous UI code remains the same until GLB Extraction Settings...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
| 140 |
+
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
| 141 |
+
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
| 142 |
+
# Add new building optimization controls
|
| 143 |
+
with gr.Row():
|
| 144 |
+
is_building = gr.Checkbox(label="Enable Building Optimization", value=False)
|
| 145 |
+
with gr.Column(visible=False) as building_settings:
|
| 146 |
+
angle_threshold = gr.Slider(5, 45, label="Edge Angle Threshold", value=15, step=1)
|
| 147 |
+
planar_threshold = gr.Slider(0.01, 0.1, label="Planar Surface Threshold", value=0.02, step=0.01)
|
| 148 |
|
| 149 |
+
# [Rest of the UI code remains the same until the event handlers...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# Add visibility toggle for building settings
|
| 152 |
+
is_building.change(
|
| 153 |
+
lambda x: gr.Column.update(visible=x),
|
| 154 |
+
inputs=[is_building],
|
| 155 |
+
outputs=[building_settings]
|
| 156 |
)
|
| 157 |
+
|
| 158 |
+
# Modify the extract_glb button click handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
extract_glb_btn.click(
|
| 160 |
extract_glb,
|
| 161 |
+
inputs=[output_buf, mesh_simplify, texture_size, is_building, angle_threshold, planar_threshold],
|
| 162 |
outputs=[model_output, download_glb],
|
| 163 |
).then(
|
| 164 |
lambda: gr.Button(interactive=True),
|
| 165 |
outputs=[download_glb],
|
| 166 |
)
|
| 167 |
|
| 168 |
+
# [Rest of the code remains the same...]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
# Launch the Gradio app
|
| 171 |
if __name__ == "__main__":
|
|
|
|
| 175 |
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
| 176 |
except:
|
| 177 |
pass
|
| 178 |
+
demo.launch()
|