JeffreyXiang commited on
Commit
a898014
1 Parent(s): 2e7f188
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -17,9 +17,10 @@ from trellis.utils import render_utils, postprocessing_utils
17
 
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
 
20
 
21
 
22
- def preprocess_image(image: Image.Image) -> Tuple[dict, Image.Image]:
23
  """
24
  Preprocess the input image.
25
 
@@ -27,14 +28,16 @@ def preprocess_image(image: Image.Image) -> Tuple[dict, Image.Image]:
27
  image (Image.Image): The input image.
28
 
29
  Returns:
30
- np.array: The preprocessed image.
31
  Image.Image: The preprocessed image.
32
  """
 
33
  processed_image = pipeline.preprocess_image(image)
34
- return {'image': np.array(processed_image)}, processed_image
 
35
 
36
 
37
- def pack_state(gs: Gaussian, mesh: MeshExtractResult, model_id: str) -> dict:
38
  return {
39
  'gaussian': {
40
  **gs.init_params,
@@ -48,7 +51,7 @@ def pack_state(gs: Gaussian, mesh: MeshExtractResult, model_id: str) -> dict:
48
  'vertices': mesh.vertices.cpu().numpy(),
49
  'faces': mesh.faces.cpu().numpy(),
50
  },
51
- 'model_id': model_id,
52
  }
53
 
54
 
@@ -72,16 +75,16 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
72
  faces=torch.tensor(state['mesh']['faces'], device='cuda'),
73
  )
74
 
75
- return gs, mesh, state['model_id']
76
 
77
 
78
  @spaces.GPU
79
- def image_to_3d(image: dict, seed: int, randomize_seed: bool, ss_guidance_strength: float, ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int) -> Tuple[dict, str]:
80
  """
81
  Convert an image to a 3D model.
82
 
83
  Args:
84
- image (dict): The input image.
85
  seed (int): The random seed.
86
  randomize_seed (bool): Whether to randomize the seed.
87
  ss_guidance_strength (float): The guidance strength for sparse structure generation.
@@ -96,7 +99,7 @@ def image_to_3d(image: dict, seed: int, randomize_seed: bool, ss_guidance_streng
96
  if randomize_seed:
97
  seed = np.random.randint(0, MAX_SEED)
98
  outputs = pipeline.run(
99
- Image.fromarray(image['image']),
100
  seed=seed,
101
  formats=["gaussian", "mesh"],
102
  preprocess_image=False,
@@ -112,11 +115,11 @@ def image_to_3d(image: dict, seed: int, randomize_seed: bool, ss_guidance_streng
112
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
113
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
114
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
115
- model_id = uuid.uuid4()
116
- video_path = f"/tmp/Trellis-demo/{model_id}.mp4"
117
  os.makedirs(os.path.dirname(video_path), exist_ok=True)
118
  imageio.mimsave(video_path, video, fps=15)
119
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], model_id)
120
  return state, video_path
121
 
122
 
@@ -133,9 +136,9 @@ def extract_glb(state: dict, mesh_simplify: float, texture_size: int) -> Tuple[s
133
  Returns:
134
  str: The path to the extracted GLB file.
135
  """
136
- gs, mesh, model_id = unpack_state(state)
137
  glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
138
- glb_path = f"/tmp/Trellis-demo/{model_id}.glb"
139
  glb.export(glb_path)
140
  return glb_path, glb_path
141
 
@@ -184,7 +187,7 @@ with gr.Blocks() as demo:
184
  model_output = LitModel3D(label="Extracted GLB", exposure=20.0, height=300)
185
  download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
186
 
187
- image_buf = gr.State()
188
  output_buf = gr.State()
189
 
190
  # Example images at the bottom of the page
@@ -196,7 +199,7 @@ with gr.Blocks() as demo:
196
  ],
197
  inputs=[image_prompt],
198
  fn=preprocess_image,
199
- outputs=[image_buf, image_prompt],
200
  run_on_click=True,
201
  examples_per_page=64,
202
  )
@@ -205,12 +208,16 @@ with gr.Blocks() as demo:
205
  image_prompt.upload(
206
  preprocess_image,
207
  inputs=[image_prompt],
208
- outputs=[image_buf, image_prompt],
 
 
 
 
209
  )
210
 
211
  generate_btn.click(
212
  image_to_3d,
213
- inputs=[image_buf, seed, randomize_seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
214
  outputs=[output_buf, video_output],
215
  ).then(
216
  activate_button,
 
17
 
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
+ TMP_DIR = "/tmp/Trellis-demo"
21
 
22
 
23
+ def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
24
  """
25
  Preprocess the input image.
26
 
 
28
  image (Image.Image): The input image.
29
 
30
  Returns:
31
+ str: uuid of the trial.
32
  Image.Image: The preprocessed image.
33
  """
34
+ trial_id = str(uuid.uuid4())
35
  processed_image = pipeline.preprocess_image(image)
36
+ processed_image.save(f"{TMP_DIR}/{trial_id}.png")
37
+ return trial_id, processed_image
38
 
39
 
40
+ def pack_state(gs: Gaussian, mesh: MeshExtractResult, trial_id: str) -> dict:
41
  return {
42
  'gaussian': {
43
  **gs.init_params,
 
51
  'vertices': mesh.vertices.cpu().numpy(),
52
  'faces': mesh.faces.cpu().numpy(),
53
  },
54
+ 'trial_id': trial_id,
55
  }
56
 
57
 
 
75
  faces=torch.tensor(state['mesh']['faces'], device='cuda'),
76
  )
77
 
78
+ return gs, mesh, state['trial_id']
79
 
80
 
81
  @spaces.GPU
82
+ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float, ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int) -> Tuple[dict, str]:
83
  """
84
  Convert an image to a 3D model.
85
 
86
  Args:
87
+ trial_id (str): The uuid of the trial.
88
  seed (int): The random seed.
89
  randomize_seed (bool): Whether to randomize the seed.
90
  ss_guidance_strength (float): The guidance strength for sparse structure generation.
 
99
  if randomize_seed:
100
  seed = np.random.randint(0, MAX_SEED)
101
  outputs = pipeline.run(
102
+ Image.open(f"{TMP_DIR}/{trial_id}.png"),
103
  seed=seed,
104
  formats=["gaussian", "mesh"],
105
  preprocess_image=False,
 
115
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
116
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
117
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
118
+ trial_id = uuid.uuid4()
119
+ video_path = f"{TMP_DIR}/{trial_id}.mp4"
120
  os.makedirs(os.path.dirname(video_path), exist_ok=True)
121
  imageio.mimsave(video_path, video, fps=15)
122
+ state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
123
  return state, video_path
124
 
125
 
 
136
  Returns:
137
  str: The path to the extracted GLB file.
138
  """
139
+ gs, mesh, trial_id = unpack_state(state)
140
  glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
141
+ glb_path = f"{TMP_DIR}/{trial_id}.glb"
142
  glb.export(glb_path)
143
  return glb_path, glb_path
144
 
 
187
  model_output = LitModel3D(label="Extracted GLB", exposure=20.0, height=300)
188
  download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
189
 
190
+ trial_id = gr.Textbox(visible=False)
191
  output_buf = gr.State()
192
 
193
  # Example images at the bottom of the page
 
199
  ],
200
  inputs=[image_prompt],
201
  fn=preprocess_image,
202
+ outputs=[trial_id, image_prompt],
203
  run_on_click=True,
204
  examples_per_page=64,
205
  )
 
208
  image_prompt.upload(
209
  preprocess_image,
210
  inputs=[image_prompt],
211
+ outputs=[trial_id, image_prompt],
212
+ )
213
+ image_prompt.clear(
214
+ lambda: '',
215
+ outputs=[trial_id],
216
  )
217
 
218
  generate_btn.click(
219
  image_to_3d,
220
+ inputs=[trial_id, seed, randomize_seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
221
  outputs=[output_buf, video_output],
222
  ).then(
223
  activate_button,