cronos3k commited on
Commit
9b222ac
·
verified ·
1 Parent(s): b62fa8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -84,7 +84,7 @@ def image_to_3d(
84
  slat_guidance_strength: float,
85
  slat_sampling_steps: int,
86
  req: gr.Request,
87
- ) -> Tuple[dict, str, str]:
88
  """
89
  Convert an image to a 3D model.
90
  """
@@ -103,20 +103,19 @@ def image_to_3d(
103
  "cfg_strength": slat_guidance_strength,
104
  },
105
  )
106
-
107
- # Generate video preview
108
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
109
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
110
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
111
  trial_id = str(uuid.uuid4())
112
  video_path = os.path.join(user_dir, f"{trial_id}.mp4")
113
  imageio.mimsave(video_path, video, fps=15)
114
-
115
- # Save full quality GLB
116
  glb = postprocessing_utils.to_glb(
117
  outputs['gaussian'][0],
118
  outputs['mesh'][0],
119
- simplify=0.0, # No simplification for full quality
120
  fill_holes=True,
121
  fill_holes_max_size=0.04,
122
  texture_size=2048, # Maximum texture size
@@ -125,45 +124,39 @@ def image_to_3d(
125
  full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
126
  glb.export(full_glb_path)
127
 
128
- # Pack state for potential reduced version
129
  state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
130
-
131
- return state, video_path, full_glb_path
132
 
133
- def extract_reduced_glb(
134
  state: dict,
135
  mesh_simplify: float,
136
  texture_size: int,
137
  req: gr.Request,
138
  ) -> Tuple[str, str]:
139
  """
140
- Extract a reduced quality GLB file.
141
  """
142
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
143
  gs, mesh, trial_id = unpack_state(state)
144
-
145
- # Create reduced quality GLB with user settings
146
  glb = postprocessing_utils.to_glb(
147
- gs,
148
- mesh,
149
  simplify=mesh_simplify,
150
  fill_holes=True,
151
  fill_holes_max_size=0.04,
152
  texture_size=texture_size,
153
  verbose=False
154
  )
155
- reduced_glb_path = os.path.join(user_dir, f"{trial_id}_reduced.glb")
156
- glb.export(reduced_glb_path)
157
-
158
- return reduced_glb_path, reduced_glb_path
159
 
160
  with gr.Blocks(delete_cache=(600, 600)) as demo:
161
  gr.Markdown("""
162
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
163
  * Upload an image and click "Generate" to create a 3D asset
164
- * After generation:
165
- * Download the full quality GLB (no mesh simplification, maximum texture resolution)
166
- * Or create a reduced size version with customizable settings
167
  """)
168
 
169
  with gr.Row():
@@ -184,17 +177,15 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
184
 
185
  generate_btn = gr.Button("Generate")
186
 
187
- with gr.Accordion(label="Reduced GLB Settings", open=False):
188
- mesh_simplify = gr.Slider(0.0, 0.98, label="Mesh Simplification", value=0.95, step=0.01,
189
- info="Higher values = more reduction")
190
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
191
 
192
- extract_reduced_btn = gr.Button("Extract Reduced GLB", interactive=False)
193
 
194
  with gr.Column():
195
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
196
  model_output = LitModel3D(label="3D Model Preview", exposure=20.0, height=300)
197
- gr.Markdown("### Download Options")
198
  with gr.Row():
199
  download_full = gr.DownloadButton(label="Download Full-Quality GLB", interactive=False)
200
  download_reduced = gr.DownloadButton(label="Download Reduced GLB", interactive=False)
@@ -230,4 +221,28 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
230
  inputs=[randomize_seed, seed],
231
  outputs=[seed],
232
  ).then(
233
- image_to_3d,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  slat_guidance_strength: float,
85
  slat_sampling_steps: int,
86
  req: gr.Request,
87
+ ) -> Tuple[dict, str, str, str]:
88
  """
89
  Convert an image to a 3D model.
90
  """
 
103
  "cfg_strength": slat_guidance_strength,
104
  },
105
  )
106
+
 
107
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
108
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
109
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
110
  trial_id = str(uuid.uuid4())
111
  video_path = os.path.join(user_dir, f"{trial_id}.mp4")
112
  imageio.mimsave(video_path, video, fps=15)
113
+
114
+ # Generate full quality GLB
115
  glb = postprocessing_utils.to_glb(
116
  outputs['gaussian'][0],
117
  outputs['mesh'][0],
118
+ simplify=0.0, # No simplification
119
  fill_holes=True,
120
  fill_holes_max_size=0.04,
121
  texture_size=2048, # Maximum texture size
 
124
  full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
125
  glb.export(full_glb_path)
126
 
 
127
  state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
128
+ return state, video_path, model_output, full_glb_path
 
129
 
130
+ def extract_glb(
131
  state: dict,
132
  mesh_simplify: float,
133
  texture_size: int,
134
  req: gr.Request,
135
  ) -> Tuple[str, str]:
136
  """
137
+ Extract a reduced GLB file from the 3D model.
138
  """
139
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
140
  gs, mesh, trial_id = unpack_state(state)
 
 
141
  glb = postprocessing_utils.to_glb(
142
+ gs, mesh,
 
143
  simplify=mesh_simplify,
144
  fill_holes=True,
145
  fill_holes_max_size=0.04,
146
  texture_size=texture_size,
147
  verbose=False
148
  )
149
+ glb_path = os.path.join(user_dir, f"{trial_id}_reduced.glb")
150
+ glb.export(glb_path)
151
+ return glb_path, glb_path
 
152
 
153
  with gr.Blocks(delete_cache=(600, 600)) as demo:
154
  gr.Markdown("""
155
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
156
  * Upload an image and click "Generate" to create a 3D asset
157
+ * After generation, you can:
158
+ * Download the full quality GLB immediately
159
+ * Create a reduced size version with the extraction settings below
160
  """)
161
 
162
  with gr.Row():
 
177
 
178
  generate_btn = gr.Button("Generate")
179
 
180
+ with gr.Accordion(label="GLB Extraction Settings", open=False):
181
+ mesh_simplify = gr.Slider(0.0, 0.98, label="Simplify", value=0.95, step=0.01)
 
182
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
183
 
184
+ extract_glb_btn = gr.Button("Extract Reduced GLB", interactive=False)
185
 
186
  with gr.Column():
187
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
188
  model_output = LitModel3D(label="3D Model Preview", exposure=20.0, height=300)
 
189
  with gr.Row():
190
  download_full = gr.DownloadButton(label="Download Full-Quality GLB", interactive=False)
191
  download_reduced = gr.DownloadButton(label="Download Reduced GLB", interactive=False)
 
221
  inputs=[randomize_seed, seed],
222
  outputs=[seed],
223
  ).then(
224
+ image_to_3d,
225
+ inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
226
+ outputs=[output_buf, video_output, model_output, download_full],
227
+ ).then(
228
+ lambda: [gr.Button(interactive=True), gr.Button(interactive=True), gr.Button(interactive=False)],
229
+ outputs=[download_full, extract_glb_btn, download_reduced],
230
+ )
231
+
232
+ extract_glb_btn.click(
233
+ extract_glb,
234
+ inputs=[output_buf, mesh_simplify, texture_size],
235
+ outputs=[model_output, download_reduced],
236
+ ).then(
237
+ lambda: gr.Button(interactive=True),
238
+ outputs=[download_reduced],
239
+ )
240
+
241
+ if __name__ == "__main__":
242
+ pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
243
+ pipeline.cuda()
244
+ try:
245
+ pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))
246
+ except:
247
+ pass
248
+ demo.launch()