cavargas10 commited on
Commit
793ef80
verified
1 Parent(s): c74cd86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -91
app.py CHANGED
@@ -13,7 +13,6 @@ from PIL import Image
13
  from trellis.pipelines import TrellisImageTo3DPipeline
14
  from trellis.representations import Gaussian, MeshExtractResult
15
  from trellis.utils import render_utils, postprocessing_utils
16
-
17
  MAX_SEED = np.iinfo(np.int32).max
18
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
19
  os.makedirs(TMP_DIR, exist_ok=True)
@@ -28,9 +27,6 @@ def end_session(req: gr.Request):
28
  shutil.rmtree(user_dir)
29
 
30
  def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
31
- """
32
- Preprocesa una lista de im谩genes.
33
- """
34
  images = [image[0] for image in images]
35
  processed_images = [pipeline.preprocess_image(image) for image in images]
36
  return processed_images
@@ -72,9 +68,6 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict]:
72
  return gs, mesh
73
 
74
  def get_seed(randomize_seed: bool, seed: int) -> int:
75
- """
76
- Obtiene una semilla aleatoria.
77
- """
78
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
79
 
80
  @spaces.GPU
@@ -88,9 +81,6 @@ def image_to_3d(
88
  multiimage_algo: Literal["multidiffusion", "stochastic"],
89
  req: gr.Request,
90
  ) -> Tuple[dict, str]:
91
- """
92
- Convierte m煤ltiples im谩genes en un modelo 3D.
93
- """
94
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
95
  outputs = pipeline.run_multi_image(
96
  [image[0] for image in multiimages],
@@ -123,9 +113,6 @@ def extract_glb(
123
  texture_size: int,
124
  req: gr.Request,
125
  ) -> Tuple[str, str]:
126
- """
127
- Extrae un archivo GLB del modelo 3D.
128
- """
129
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
130
  gs, mesh = unpack_state(state)
131
  glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
@@ -134,41 +121,14 @@ def extract_glb(
134
  torch.cuda.empty_cache()
135
  return glb_path, glb_path
136
 
137
- @spaces.GPU
138
- def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
139
- """
140
- Extrae un archivo Gaussiano del modelo 3D.
141
- """
142
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
143
- gs, _ = unpack_state(state)
144
- gaussian_path = os.path.join(user_dir, 'sample.ply')
145
- gs.save_ply(gaussian_path)
146
- torch.cuda.empty_cache()
147
- return gaussian_path, gaussian_path
148
-
149
- def prepare_multi_example() -> List[Tuple[str, str]]:
150
- """
151
- Prepara ejemplos de m煤ltiples im谩genes para la galer铆a.
152
- """
153
- multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
154
- examples = []
155
- for case in multi_case:
156
- case_images = []
157
- for i in range(1, 4): # Suponemos 3 vistas por caso
158
- img_path = f'assets/example_multi_image/{case}_{i}.png'
159
- if os.path.exists(img_path): # Asegurarse de que la imagen existe
160
- case_images.append((img_path, f"View {i}"))
161
- if case_images: # Solo a帽adir casos con im谩genes v谩lidas
162
- examples.append(case_images)
163
- return examples
164
-
165
  # Interfaz Gradio
166
  with gr.Blocks(delete_cache=(600, 600)) as demo:
167
  gr.Markdown("""
168
- ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
169
- * Upload multiple images of an object from different views and click "Generate" to create a 3D asset.
170
- * If you find the generated 3D asset satisfactory, click "Extract GLB" to extract the GLB file and download it.
171
- 鉁∟ew: Experimental multi-image support and Gaussian file extraction.
 
172
  """)
173
 
174
  with gr.Row():
@@ -176,11 +136,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
176
  with gr.Tabs() as input_tabs:
177
  with gr.Tab(label="Multiple Images", id=1) as multiimage_input_tab:
178
  multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)
179
- gr.Markdown("""
180
- Input different views of the object in separate images.
181
- NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
182
- """)
183
-
184
  with gr.Accordion(label="Generation Settings", open=False):
185
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
186
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
@@ -200,45 +156,25 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
200
  mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
201
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
202
 
203
- with gr.Row():
204
- extract_glb_btn = gr.Button("Extract GLB", interactive=False)
205
- extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
206
-
207
- gr.Markdown("""
208
- NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
209
- """)
210
 
211
  with gr.Column():
212
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
213
- model_output = LitModel3D(label="Extracted GLB/Gaussian", exposure=10.0, height=300)
214
-
215
- with gr.Row():
216
- download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
217
- download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
218
 
219
  output_buf = gr.State()
220
 
221
- # Ejemplos de im谩genes m煤ltiples
222
- with gr.Row(visible=True) as multiimage_example:
223
- examples_multi = gr.Examples(
224
- examples=prepare_multi_example(),
225
- inputs=[multiimage_prompt],
226
- fn=lambda x: x, # Pasar la entrada directamente (sin preprocesamiento adicional)
227
- outputs=[multiimage_prompt],
228
- run_on_click=True,
229
- examples_per_page=8,
230
- )
231
-
232
  # Manejadores
233
  demo.load(start_session)
234
  demo.unload(end_session)
235
-
236
  multiimage_prompt.upload(
237
  preprocess_images,
238
  inputs=[multiimage_prompt],
239
  outputs=[multiimage_prompt],
240
  )
241
-
242
  generate_btn.click(
243
  get_seed,
244
  inputs=[randomize_seed, seed],
@@ -248,15 +184,15 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
248
  inputs=[multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo],
249
  outputs=[output_buf, video_output],
250
  ).then(
251
- lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
252
- outputs=[extract_glb_btn, extract_gs_btn],
253
  )
254
-
255
  video_output.clear(
256
- lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
257
- outputs=[extract_glb_btn, extract_gs_btn],
258
  )
259
-
260
  extract_glb_btn.click(
261
  extract_glb,
262
  inputs=[output_buf, mesh_simplify, texture_size],
@@ -265,16 +201,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
265
  lambda: gr.Button(interactive=True),
266
  outputs=[download_glb],
267
  )
268
-
269
- extract_gs_btn.click(
270
- extract_gaussian,
271
- inputs=[output_buf],
272
- outputs=[model_output, download_gs],
273
- ).then(
274
- lambda: gr.Button(interactive=True),
275
- outputs=[download_gs],
276
- )
277
-
278
  model_output.clear(
279
  lambda: gr.Button(interactive=False),
280
  outputs=[download_glb],
 
13
  from trellis.pipelines import TrellisImageTo3DPipeline
14
  from trellis.representations import Gaussian, MeshExtractResult
15
  from trellis.utils import render_utils, postprocessing_utils
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
18
  os.makedirs(TMP_DIR, exist_ok=True)
 
27
  shutil.rmtree(user_dir)
28
 
29
  def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
 
 
 
30
  images = [image[0] for image in images]
31
  processed_images = [pipeline.preprocess_image(image) for image in images]
32
  return processed_images
 
68
  return gs, mesh
69
 
70
  def get_seed(randomize_seed: bool, seed: int) -> int:
 
 
 
71
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
72
 
73
  @spaces.GPU
 
81
  multiimage_algo: Literal["multidiffusion", "stochastic"],
82
  req: gr.Request,
83
  ) -> Tuple[dict, str]:
 
 
 
84
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
85
  outputs = pipeline.run_multi_image(
86
  [image[0] for image in multiimages],
 
113
  texture_size: int,
114
  req: gr.Request,
115
  ) -> Tuple[str, str]:
 
 
 
116
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
117
  gs, mesh = unpack_state(state)
118
  glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
 
121
  torch.cuda.empty_cache()
122
  return glb_path, glb_path
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # Interfaz Gradio
125
  with gr.Blocks(delete_cache=(600, 600)) as demo:
126
  gr.Markdown("""
127
+ # UTPL - Conversi贸n de Multiples Im谩genes a objetos 3D usando IA
128
+ ### Tesis: *"Objetos tridimensionales creados por IA: Innovaci贸n en entornos virtuales"*
129
+ **Autor:** Carlos Vargas
130
+ **Base t茅cnica:** Adaptaci贸n de [TRELLIS](https://trellis3d.github.io/) (herramienta de c贸digo abierto para generaci贸n 3D)
131
+ **Prop贸sito educativo:** Demostraciones acad茅micas e Investigaci贸n en modelado 3D autom谩tico
132
  """)
133
 
134
  with gr.Row():
 
136
  with gr.Tabs() as input_tabs:
137
  with gr.Tab(label="Multiple Images", id=1) as multiimage_input_tab:
138
  multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)
139
+
 
 
 
 
140
  with gr.Accordion(label="Generation Settings", open=False):
141
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
142
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
 
156
  mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
157
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
158
 
159
+ extract_glb_btn = gr.Button("Extract GLB", interactive=False)
 
 
 
 
 
 
160
 
161
  with gr.Column():
162
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
163
+ model_output = LitModel3D(label="Extracted GLB", exposure=10.0, height=300)
164
+ download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
 
 
 
165
 
166
  output_buf = gr.State()
167
 
 
 
 
 
 
 
 
 
 
 
 
168
  # Manejadores
169
  demo.load(start_session)
170
  demo.unload(end_session)
171
+
172
  multiimage_prompt.upload(
173
  preprocess_images,
174
  inputs=[multiimage_prompt],
175
  outputs=[multiimage_prompt],
176
  )
177
+
178
  generate_btn.click(
179
  get_seed,
180
  inputs=[randomize_seed, seed],
 
184
  inputs=[multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo],
185
  outputs=[output_buf, video_output],
186
  ).then(
187
+ lambda: gr.Button(interactive=True),
188
+ outputs=[extract_glb_btn],
189
  )
190
+
191
  video_output.clear(
192
+ lambda: gr.Button(interactive=False),
193
+ outputs=[extract_glb_btn],
194
  )
195
+
196
  extract_glb_btn.click(
197
  extract_glb,
198
  inputs=[output_buf, mesh_simplify, texture_size],
 
201
  lambda: gr.Button(interactive=True),
202
  outputs=[download_glb],
203
  )
204
+
 
 
 
 
 
 
 
 
 
205
  model_output.clear(
206
  lambda: gr.Button(interactive=False),
207
  outputs=[download_glb],