Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,21 @@
|
|
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 *
|
@@ -97,6 +110,7 @@ def segment_and_overlay(image, points, sam_predictor):
|
|
97 |
return overlaid, visible_mask
|
98 |
|
99 |
|
|
|
100 |
def image_to_3d(
|
101 |
images: List[tuple],
|
102 |
masks: List[np.ndarray],
|
@@ -133,33 +147,38 @@ def image_to_3d(
|
|
133 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
134 |
imageio.mimsave(video_path, video, fps=15)
|
135 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
|
|
136 |
return state, video_path
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
163 |
|
164 |
|
165 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from gradio_litmodel3d import LitModel3D
|
3 |
|
4 |
import os
|
5 |
+
if os.environ.get("SPACES_ZERO_GPU") is not None:
|
6 |
+
import spaces
|
7 |
+
else:
|
8 |
+
class spaces:
|
9 |
+
@staticmethod
|
10 |
+
def GPU(func):
|
11 |
+
def wrapper(*args, **kwargs):
|
12 |
+
return func(*args, **kwargs)
|
13 |
+
return wrapper
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def fake_gpu():
|
17 |
+
pass
|
18 |
+
|
19 |
import shutil
|
20 |
os.environ['SPCONV_ALGO'] = 'native'
|
21 |
from typing import *
|
|
|
110 |
return overlaid, visible_mask
|
111 |
|
112 |
|
113 |
+
@spaces.GPU
|
114 |
def image_to_3d(
|
115 |
images: List[tuple],
|
116 |
masks: List[np.ndarray],
|
|
|
147 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
148 |
imageio.mimsave(video_path, video, fps=15)
|
149 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
150 |
+
torch.cuda.empty_cache()
|
151 |
return state, video_path
|
152 |
|
153 |
+
@spaces.GPU(duration=90)
|
154 |
+
def extract_glb(
|
155 |
+
state: dict,
|
156 |
+
mesh_simplify: float,
|
157 |
+
texture_size: int,
|
158 |
+
req: gr.Request,
|
159 |
+
) -> tuple:
|
160 |
+
"""
|
161 |
+
δ»ηζη 3D 樑εδΈζε GLB ζδ»Άγ
|
162 |
+
"""
|
163 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
164 |
+
gs, mesh = unpack_state(state)
|
165 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
166 |
+
glb_path = os.path.join(user_dir, 'sample.glb')
|
167 |
+
glb.export(glb_path)
|
168 |
+
torch.cuda.empty_cache()
|
169 |
+
return glb_path, glb_path
|
170 |
+
|
171 |
+
@spaces.GPU
|
172 |
+
def extract_gaussian(state: dict, req: gr.Request) -> tuple:
|
173 |
+
"""
|
174 |
+
δ»ηζη 3D 樑εδΈζε Gaussian ζδ»Άγ
|
175 |
+
"""
|
176 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
177 |
+
gs, _ = unpack_state(state)
|
178 |
+
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
179 |
+
gs.save_ply(gaussian_path)
|
180 |
+
torch.cuda.empty_cache()
|
181 |
+
return gaussian_path, gaussian_path
|
182 |
|
183 |
|
184 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|