lionelgarnier commited on
Commit
f04b403
·
1 Parent(s): 346d9f6

uncomment 2

Browse files
Files changed (1) hide show
  1. app.py +68 -68
app.py CHANGED
@@ -291,77 +291,77 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
291
  return gs, mesh
292
 
293
 
294
- # @spaces.GPU
295
- # def image_to_3d(
296
- # image: Image.Image,
297
- # seed: int,
298
- # ss_guidance_strength: float,
299
- # ss_sampling_steps: int,
300
- # slat_guidance_strength: float,
301
- # slat_sampling_steps: int,
302
- # ) -> Tuple[dict, str]:
303
- # try:
304
- # # Use a fixed temp directory instead of user-specific
305
- # temp_dir = os.path.join(TMP_DIR, "temp_output")
306
- # os.makedirs(temp_dir, exist_ok=True)
307
 
308
- # # Get the pipeline using the getter function
309
- # pipeline = get_trellis_pipeline()
310
- # if pipeline is None:
311
- # return None, "Trellis pipeline is unavailable."
312
 
313
- # outputs = pipeline.run(
314
- # image,
315
- # seed=seed,
316
- # formats=["gaussian", "mesh"],
317
- # preprocess_image=False,
318
- # sparse_structure_sampler_params={
319
- # "steps": ss_sampling_steps,
320
- # "cfg_strength": ss_guidance_strength,
321
- # },
322
- # slat_sampler_params={
323
- # "steps": slat_sampling_steps,
324
- # "cfg_strength": slat_guidance_strength,
325
- # },
326
- # )
327
-
328
- # video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
329
- # video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
330
- # video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
331
- # video_path = os.path.join(temp_dir, 'sample.mp4')
332
- # imageio.mimsave(video_path, video, fps=15)
333
- # state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
334
- # torch.cuda.empty_cache()
335
- # return state, video_path
336
- # except Exception as e:
337
- # print(f"Error in image_to_3d: {str(e)}")
338
- # return None, f"Error generating 3D model: {str(e)}"
339
-
340
-
341
- # @spaces.GPU(duration=90)
342
- # def extract_glb(
343
- # state: dict,
344
- # mesh_simplify: float,
345
- # texture_size: int,
346
- # ) -> Tuple[str, str]:
347
- # """
348
- # Extract a GLB file from the 3D model.
349
-
350
- # Args:
351
- # state (dict): The state of the generated 3D model.
352
- # mesh_simplify (float): The mesh simplification factor.
353
- # texture_size (int): The texture resolution.
354
 
355
- # Returns:
356
- # str: The path to the extracted GLB file.
357
- # """
358
- # temp_dir = os.path.join(TMP_DIR, "temp_output")
359
- # gs, mesh = unpack_state(state)
360
- # glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
361
- # glb_path = os.path.join(temp_dir, 'sample.glb')
362
- # glb.export(glb_path)
363
- # torch.cuda.empty_cache()
364
- # return glb_path, glb_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365
 
366
 
367
  # @spaces.GPU
 
291
  return gs, mesh
292
 
293
 
294
+ @spaces.GPU
295
+ def image_to_3d(
296
+ image: Image.Image,
297
+ seed: int,
298
+ ss_guidance_strength: float,
299
+ ss_sampling_steps: int,
300
+ slat_guidance_strength: float,
301
+ slat_sampling_steps: int,
302
+ ) -> Tuple[dict, str]:
303
+ try:
304
+ # Use a fixed temp directory instead of user-specific
305
+ temp_dir = os.path.join(TMP_DIR, "temp_output")
306
+ os.makedirs(temp_dir, exist_ok=True)
307
 
308
+ # Get the pipeline using the getter function
309
+ pipeline = get_trellis_pipeline()
310
+ if pipeline is None:
311
+ return None, "Trellis pipeline is unavailable."
312
 
313
+ outputs = pipeline.run(
314
+ image,
315
+ seed=seed,
316
+ formats=["gaussian", "mesh"],
317
+ preprocess_image=False,
318
+ sparse_structure_sampler_params={
319
+ "steps": ss_sampling_steps,
320
+ "cfg_strength": ss_guidance_strength,
321
+ },
322
+ slat_sampler_params={
323
+ "steps": slat_sampling_steps,
324
+ "cfg_strength": slat_guidance_strength,
325
+ },
326
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
+ video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
329
+ video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
330
+ video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
331
+ video_path = os.path.join(temp_dir, 'sample.mp4')
332
+ imageio.mimsave(video_path, video, fps=15)
333
+ state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
334
+ torch.cuda.empty_cache()
335
+ return state, video_path
336
+ except Exception as e:
337
+ print(f"Error in image_to_3d: {str(e)}")
338
+ return None, f"Error generating 3D model: {str(e)}"
339
+
340
+
341
+ @spaces.GPU(duration=90)
342
+ def extract_glb(
343
+ state: dict,
344
+ mesh_simplify: float,
345
+ texture_size: int,
346
+ ) -> Tuple[str, str]:
347
+ """
348
+ Extract a GLB file from the 3D model.
349
+
350
+ Args:
351
+ state (dict): The state of the generated 3D model.
352
+ mesh_simplify (float): The mesh simplification factor.
353
+ texture_size (int): The texture resolution.
354
+
355
+ Returns:
356
+ str: The path to the extracted GLB file.
357
+ """
358
+ temp_dir = os.path.join(TMP_DIR, "temp_output")
359
+ gs, mesh = unpack_state(state)
360
+ glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
361
+ glb_path = os.path.join(temp_dir, 'sample.glb')
362
+ glb.export(glb_path)
363
+ torch.cuda.empty_cache()
364
+ return glb_path, glb_path
365
 
366
 
367
  # @spaces.GPU