Sm0kyWu commited on
Commit
a723dd3
Β·
verified Β·
1 Parent(s): 154113b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
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
- # def extract_glb(
139
- # state: dict,
140
- # mesh_simplify: float,
141
- # texture_size: int,
142
- # req: gr.Request,
143
- # ) -> tuple:
144
- # """
145
- # δ»Žη”Ÿζˆηš„ 3D ζ¨‘εž‹δΈ­ζε– GLB 文仢。
146
- # """
147
- # user_dir = os.path.join(TMP_DIR, str(req.session_hash))
148
- # gs, mesh = unpack_state(state)
149
- # glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
150
- # glb_path = os.path.join(user_dir, 'sample.glb')
151
- # glb.export(glb_path)
152
- # return glb_path, glb_path
153
-
154
- # def extract_gaussian(state: dict, req: gr.Request) -> tuple:
155
- # """
156
- # δ»Žη”Ÿζˆηš„ 3D ζ¨‘εž‹δΈ­ζε– Gaussian 文仢。
157
- # """
158
- # user_dir = os.path.join(TMP_DIR, str(req.session_hash))
159
- # gs, _ = unpack_state(state)
160
- # gaussian_path = os.path.join(user_dir, 'sample.ply')
161
- # gs.save_ply(gaussian_path)
162
- # return gaussian_path, gaussian_path
 
 
 
 
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: