cronos3k commited on
Commit
b7adca8
·
verified ·
1 Parent(s): 8a25734

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -100,7 +100,7 @@ def image_to_3d(
100
  slat_guidance_strength: float,
101
  slat_sampling_steps: int,
102
  req: gr.Request,
103
- ) -> Tuple[dict, str, str, str]:
104
  """
105
  Convert an image to a 3D model.
106
  """
@@ -119,28 +119,38 @@ def image_to_3d(
119
  "cfg_strength": slat_guidance_strength,
120
  },
121
  )
122
- video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
123
- video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
124
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
125
  trial_id = str(uuid.uuid4())
126
  video_path = os.path.join(user_dir, f"{trial_id}.mp4")
127
  imageio.mimsave(video_path, video, fps=15)
128
 
129
- # Save full quality GLB
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  glb = postprocessing_utils.to_glb(
131
- outputs['gaussian'][0],
132
- outputs['mesh'][0],
133
  simplify=0.0, # No simplification
134
  fill_holes=True,
135
  fill_holes_max_size=0.04,
136
- texture_size=2048, # Maximum texture size
137
  verbose=False
138
  )
139
- full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
140
- glb.export(full_glb_path)
141
-
142
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
143
- return state, video_path, model_output, full_glb_path
144
 
145
  @spaces.GPU
146
  def extract_glb(
@@ -150,7 +160,7 @@ def extract_glb(
150
  req: gr.Request,
151
  ) -> Tuple[str, str]:
152
  """
153
- Extract a GLB file from the 3D model.
154
  """
155
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
156
  gs, mesh, trial_id = unpack_state(state)
@@ -163,8 +173,9 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
163
  gr.Markdown("""
164
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
165
  * 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.
166
- * The full-quality GLB will be available immediately after generation.
167
- * If you need a reduced size version, use the GLB Extraction Settings below.
 
168
  """)
169
 
170
  with gr.Row():
@@ -184,12 +195,13 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
184
  slat_sampling_steps = gr.Slider(1, 500, label="Sampling Steps", value=12, step=1)
185
 
186
  generate_btn = gr.Button("Generate")
 
187
 
188
  with gr.Accordion(label="GLB Extraction Settings", open=False):
189
  mesh_simplify = gr.Slider(0.0, 0.98, label="Simplify", value=0.95, step=0.01)
190
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
191
 
192
- extract_glb_btn = gr.Button("Extract GLB", interactive=False)
193
 
194
  with gr.Column():
195
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
@@ -231,10 +243,19 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
231
  ).then(
232
  image_to_3d,
233
  inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
234
- outputs=[output_buf, video_output, model_output, download_full],
235
  ).then(
236
- lambda: [gr.Button(interactive=True), gr.Button(interactive=True), gr.Button(interactive=False)],
237
- outputs=[download_full, extract_glb_btn, download_reduced],
 
 
 
 
 
 
 
 
 
238
  )
239
 
240
  extract_glb_btn.click(
 
100
  slat_guidance_strength: float,
101
  slat_sampling_steps: int,
102
  req: gr.Request,
103
+ ) -> Tuple[dict, str]:
104
  """
105
  Convert an image to a 3D model.
106
  """
 
119
  "cfg_strength": slat_guidance_strength,
120
  },
121
  )
122
+ video = render_utils.render_video(outputs['gaussian'][0], num_frames=10)['color']
123
+ video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=10)['normal']
124
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
125
  trial_id = str(uuid.uuid4())
126
  video_path = os.path.join(user_dir, f"{trial_id}.mp4")
127
  imageio.mimsave(video_path, video, fps=15)
128
 
129
+ state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
130
+ return state, video_path
131
+
132
+ @spaces.GPU
133
+ def extract_full_glb(
134
+ state: dict,
135
+ req: gr.Request,
136
+ ) -> Tuple[str, str]:
137
+ """
138
+ Extract a full-quality GLB file from the 3D model.
139
+ """
140
+ user_dir = os.path.join(TMP_DIR, str(req.session_hash))
141
+ gs, mesh, trial_id = unpack_state(state)
142
+
143
  glb = postprocessing_utils.to_glb(
144
+ gs, mesh,
 
145
  simplify=0.0, # No simplification
146
  fill_holes=True,
147
  fill_holes_max_size=0.04,
148
+ texture_size=2048, # Maximum texture resolution
149
  verbose=False
150
  )
151
+ glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
152
+ glb.export(glb_path)
153
+ return glb_path, glb_path
 
 
154
 
155
  @spaces.GPU
156
  def extract_glb(
 
160
  req: gr.Request,
161
  ) -> Tuple[str, str]:
162
  """
163
+ Extract a reduced-quality GLB file from the 3D model.
164
  """
165
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
166
  gs, mesh, trial_id = unpack_state(state)
 
173
  gr.Markdown("""
174
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
175
  * 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.
176
+ * After generation:
177
+ * Click "Extract Full GLB" for maximum quality (no mesh reduction)
178
+ * Or use the GLB Extraction Settings for a reduced size version
179
  """)
180
 
181
  with gr.Row():
 
195
  slat_sampling_steps = gr.Slider(1, 500, label="Sampling Steps", value=12, step=1)
196
 
197
  generate_btn = gr.Button("Generate")
198
+ extract_full_btn = gr.Button("Extract Full GLB", interactive=False)
199
 
200
  with gr.Accordion(label="GLB Extraction Settings", open=False):
201
  mesh_simplify = gr.Slider(0.0, 0.98, label="Simplify", value=0.95, step=0.01)
202
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
203
 
204
+ extract_glb_btn = gr.Button("Extract Reduced GLB", interactive=False)
205
 
206
  with gr.Column():
207
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
 
243
  ).then(
244
  image_to_3d,
245
  inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
246
+ outputs=[output_buf, video_output],
247
  ).then(
248
+ lambda: [gr.Button(interactive=True), gr.Button(interactive=True)],
249
+ outputs=[extract_full_btn, extract_glb_btn],
250
+ )
251
+
252
+ extract_full_btn.click(
253
+ extract_full_glb,
254
+ inputs=[output_buf],
255
+ outputs=[model_output, download_full],
256
+ ).then(
257
+ lambda: gr.Button(interactive=True),
258
+ outputs=[download_full],
259
  )
260
 
261
  extract_glb_btn.click(