Mathdesenvnonimate commited on
Commit
6c6b13d
·
verified ·
1 Parent(s): 8c621cb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -99,24 +99,34 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
99
 
100
 
101
  @spaces.GPU
102
- def generate(image, mc_resolution, formats=["obj", "glb"]):
 
103
  scene_codes = model(image, device=device)
104
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
105
  mesh = to_gradio_3d_orientation(mesh)
106
 
107
- mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f".glb", delete=False)
108
- mesh.export(mesh_path_glb.name)
 
 
 
 
 
 
 
 
 
109
 
110
  mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
111
  mesh.apply_scale([-1, 1, 1]) # Otherwise the visualized .obj will be flipped
112
  mesh.export(mesh_path_obj.name)
113
 
114
- return mesh_path_obj.name, mesh_path_glb.name
115
 
116
  def run_example(image_pil):
117
  preprocessed = preprocess(image_pil, False, 0.9)
118
- mesh_name_obj, mesh_name_glb = generate(preprocessed, 256, ["obj", "glb"])
119
- return preprocessed, mesh_name_obj, mesh_name_glb
120
 
121
  with gr.Blocks() as demo:
122
  gr.Markdown(HEADER)
@@ -158,10 +168,10 @@ with gr.Blocks() as demo:
158
  label="Output Model (OBJ Format)",
159
  interactive=False,
160
  )
161
- gr.Markdown("Note: Downloaded object will be flipped in case of .obj export. Export .glb instead or manually flip it before usage.")
162
- with gr.Tab("GLB"):
163
- output_model_glb = gr.Model3D(
164
- label="Output Model (GLB Format)",
165
  interactive=False,
166
  )
167
  gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")
@@ -171,7 +181,7 @@ with gr.Blocks() as demo:
171
  os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
172
  ],
173
  inputs=[input_image],
174
- outputs=[processed_image, output_model_obj, output_model_glb],
175
  cache_examples=True,
176
  fn=partial(run_example),
177
  label="Examples",
@@ -184,7 +194,7 @@ with gr.Blocks() as demo:
184
  ).success(
185
  fn=generate,
186
  inputs=[processed_image, mc_resolution],
187
- outputs=[output_model_obj, output_model_glb],
188
  )
189
 
190
  demo.queue(max_size=10)
 
99
 
100
 
101
  @spaces.GPU
102
+ def generate(image, mc_resolution, formats=["obj", "stl"]):
103
+ #Substituir stl por stl
104
  scene_codes = model(image, device=device)
105
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
106
  mesh = to_gradio_3d_orientation(mesh)
107
 
108
+ mesh_path_stl = tempfile.NamedTemporaryFile(suffix=f".stl", delete=False)
109
+ # mesh_path_stl = tempfile.NamedTemporaryFile(suffix=f".stl", delete=False)
110
+ if try_import("trimesh"):
111
+ import trimesh as tm
112
+
113
+ # Converter malha para formato trimesh
114
+ mesh_tri = tm.Trimesh(vertices=mesh.vertices, faces=mesh.faces)
115
+
116
+ # Salvar malha como STL
117
+ mesh_tri.export(mesh_path_stl.name)
118
+ # mesh.export(mesh_path_stl.name)
119
 
120
  mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
121
  mesh.apply_scale([-1, 1, 1]) # Otherwise the visualized .obj will be flipped
122
  mesh.export(mesh_path_obj.name)
123
 
124
+ return mesh_path_obj.name, mesh_path_stl.name
125
 
126
  def run_example(image_pil):
127
  preprocessed = preprocess(image_pil, False, 0.9)
128
+ mesh_name_obj, mesh_name_stl = generate(preprocessed, 256, ["obj", "stl"])
129
+ return preprocessed, mesh_name_obj, mesh_name_stl
130
 
131
  with gr.Blocks() as demo:
132
  gr.Markdown(HEADER)
 
168
  label="Output Model (OBJ Format)",
169
  interactive=False,
170
  )
171
+ gr.Markdown("Note: Downloaded object will be flipped in case of .obj export. Export .stl instead or manually flip it before usage.")
172
+ with gr.Tab("stl"):
173
+ output_model_stl = gr.Model3D(
174
+ label="Output Model (stl Format)",
175
  interactive=False,
176
  )
177
  gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")
 
181
  os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
182
  ],
183
  inputs=[input_image],
184
+ outputs=[processed_image, output_model_obj, output_model_stl],
185
  cache_examples=True,
186
  fn=partial(run_example),
187
  label="Examples",
 
194
  ).success(
195
  fn=generate,
196
  inputs=[processed_image, mc_resolution],
197
+ outputs=[output_model_obj, output_model_stl],
198
  )
199
 
200
  demo.queue(max_size=10)