Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import trimesh
|
|
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
|
| 6 |
def modify_texture():
|
| 7 |
# Load the GLB file
|
| 8 |
-
mesh = trimesh.
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
gr.Interface(
|
| 26 |
fn=modify_texture,
|
| 27 |
inputs=[],
|
| 28 |
-
outputs=gr.
|
| 29 |
title="GLB Texture Modifier"
|
| 30 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import trimesh
|
| 3 |
+
from PIL import Image
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
|
| 7 |
def modify_texture():
|
| 8 |
# Load the GLB file
|
| 9 |
+
mesh = trimesh.load_mesh("train.glb", process=False)
|
| 10 |
+
|
| 11 |
+
# Open the texture image
|
| 12 |
+
im = Image.open("defect.jpg")
|
| 13 |
+
|
| 14 |
+
# Create a material with the texture image
|
| 15 |
+
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
| 16 |
+
|
| 17 |
+
# Get the uv coordinates from the mesh
|
| 18 |
+
uv = mesh.visual.uv
|
| 19 |
+
|
| 20 |
+
# Create TextureVisuals with the uv coordinates and the texture image
|
| 21 |
+
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
|
| 22 |
+
|
| 23 |
+
# Apply the visuals to the mesh
|
| 24 |
+
mesh.visual = color_visuals
|
| 25 |
+
|
| 26 |
+
# Export the mesh to a .glb file
|
| 27 |
+
mesh.export(file_obj='modified_train.glb')
|
| 28 |
+
|
| 29 |
+
# Convert the GLB file to base64 for Gradio Model3D output
|
| 30 |
+
with open('modified_train.glb', 'rb') as f:
|
| 31 |
+
glb_base64 = base64.b64encode(f.read()).decode()
|
| 32 |
+
|
| 33 |
+
return glb_base64
|
| 34 |
|
| 35 |
gr.Interface(
|
| 36 |
fn=modify_texture,
|
| 37 |
inputs=[],
|
| 38 |
+
outputs=gr.Model3D(label="Download Modified GLB File"),
|
| 39 |
title="GLB Texture Modifier"
|
| 40 |
).launch()
|