Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import trimesh
|
| 3 |
from instant_texture import Converter
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
converter = Converter()
|
| 6 |
|
| 7 |
-
|
| 8 |
def extract_texture(mesh_path: str):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def convert(input_mesh_path: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if not input_mesh_path.endswith(".obj"):
|
| 17 |
-
raise
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
demo = gr.Interface(
|
| 25 |
-
convert,
|
| 26 |
title="Instant Texture",
|
| 27 |
description="Convert a vertex-colored mesh (.obj) to a uv-mapped, textured mesh (.glb).",
|
| 28 |
inputs=gr.Model3D(label="Vertex-colored mesh (.obj)"),
|
| 29 |
outputs=[
|
| 30 |
gr.Model3D(label="Output uv-mapped, textured mesh (.glb)"),
|
| 31 |
-
gr.Image(label="Output texture"),
|
| 32 |
],
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import trimesh
|
| 3 |
from instant_texture import Converter
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
+
# Inicializa o conversor
|
| 9 |
converter = Converter()
|
| 10 |
|
|
|
|
| 11 |
def extract_texture(mesh_path: str):
|
| 12 |
+
"""Tenta extrair a textura base do arquivo GLB gerado."""
|
| 13 |
+
try:
|
| 14 |
+
# Carrega a cena GLB
|
| 15 |
+
scene = trimesh.load(mesh_path)
|
| 16 |
+
|
| 17 |
+
# GLBs são carregados como 'Scenes'. Precisamos pegar a primeira malha (geometry) da cena.
|
| 18 |
+
# Se a cena estiver vazia, isso vai gerar um erro que o 'except' pega.
|
| 19 |
+
if not scene.geometry:
|
| 20 |
+
print("Erro: Nenhuma geometria encontrada no GLB gerado.")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
mesh = next(iter(scene.geometry.values()))
|
| 24 |
+
|
| 25 |
+
# Verifica se a malha tem material e se o material tem a textura de cor base
|
| 26 |
+
if hasattr(mesh.visual, 'material') and hasattr(mesh.visual.material, 'baseColorTexture'):
|
| 27 |
+
texture = mesh.visual.material.baseColorTexture
|
| 28 |
+
# Verifica se a textura é uma imagem PIL válida antes de retornar
|
| 29 |
+
if isinstance(texture, Image.Image):
|
| 30 |
+
return texture
|
| 31 |
+
|
| 32 |
+
print("Aviso: O modelo GLB foi gerado, mas nenhuma textura baseColorTexture foi encontrada.")
|
| 33 |
+
return None
|
| 34 |
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Erro ao extrair textura: {e}")
|
| 37 |
+
return None
|
| 38 |
|
| 39 |
def convert(input_mesh_path: str):
|
| 40 |
+
# Validação de entrada
|
| 41 |
+
if input_mesh_path is None:
|
| 42 |
+
raise gr.Error("Por favor, envie um arquivo .obj.")
|
| 43 |
+
|
| 44 |
if not input_mesh_path.endswith(".obj"):
|
| 45 |
+
raise gr.Error("Apenas arquivos .obj são suportados.")
|
| 46 |
+
|
| 47 |
+
# --- MUDANÇA CRÍTICA 1: Arquivos Temporários Únicos ---
|
| 48 |
+
# Em um Space, múltiplos usuários podem usar o app ao mesmo tempo.
|
| 49 |
+
# Usar sempre "/tmp/output.glb" faria um usuário sobrescrever o arquivo do outro.
|
| 50 |
+
# Usamos UUID para criar um nome de arquivo único para cada execução.
|
| 51 |
+
unique_filename = str(uuid.uuid4())
|
| 52 |
+
output_path = os.path.join("/tmp", f"output_{unique_filename}.glb")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# Executa a conversão (assumindo que esta biblioteca funciona corretamente)
|
| 56 |
+
converter.convert(input_mesh_path, output_path)
|
| 57 |
+
|
| 58 |
+
# Verifica se o arquivo de saída foi realmente criado
|
| 59 |
+
if not os.path.exists(output_path):
|
| 60 |
+
raise gr.Error("Falha na conversão: O arquivo de saída não foi gerado.")
|
| 61 |
+
|
| 62 |
+
# Extrai a textura do novo arquivo GLB
|
| 63 |
+
texture = extract_texture(output_path)
|
| 64 |
+
|
| 65 |
+
if texture is None:
|
| 66 |
+
# Se não conseguiu extrair a textura, retorna o modelo, mas avisa no log
|
| 67 |
+
print("Retornando modelo sem imagem de textura separada.")
|
| 68 |
+
return output_path, None
|
| 69 |
|
| 70 |
+
return output_path, texture
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
# Captura erros da biblioteca instant_texture e mostra pro usuário
|
| 74 |
+
raise gr.Error(f"Erro durante o processo de conversão: {str(e)}")
|
| 75 |
|
| 76 |
+
# Configuração da Interface Gradio
|
| 77 |
demo = gr.Interface(
|
| 78 |
+
fn=convert,
|
| 79 |
title="Instant Texture",
|
| 80 |
description="Convert a vertex-colored mesh (.obj) to a uv-mapped, textured mesh (.glb).",
|
| 81 |
inputs=gr.Model3D(label="Vertex-colored mesh (.obj)"),
|
| 82 |
outputs=[
|
| 83 |
gr.Model3D(label="Output uv-mapped, textured mesh (.glb)"),
|
| 84 |
+
gr.Image(label="Output texture", type="pil"), # Força o tipo PIL para garantir compatibilidade
|
| 85 |
],
|
| 86 |
+
# Verifique se o caminho do exemplo está correto no seu repositório
|
| 87 |
+
# examples=[["examples/chair.obj"]],
|
| 88 |
+
cache_examples=False, # Desativado temporariamente para evitar erros se o exemplo não existir
|
| 89 |
)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
demo.queue().launch()
|