Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
import trimesh
|
|
|
3 |
from PIL import Image
|
4 |
-
import base64
|
5 |
import io
|
6 |
|
7 |
-
def
|
8 |
# Load the GLB file
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
14 |
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
15 |
-
|
16 |
-
# Get the uv coordinates from the mesh
|
17 |
-
uv = scene.visual.uv
|
18 |
-
|
19 |
-
# Create TextureVisuals with the uv coordinates and the texture image
|
20 |
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
|
21 |
-
|
22 |
-
# Apply the
|
23 |
-
mesh.visual =
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import trimesh
|
3 |
+
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
import io
|
6 |
|
7 |
+
def visualize_texture():
|
8 |
# Load the GLB file
|
9 |
+
mesh = trimesh.load('train.glb', file_type='glb', process=False)
|
10 |
+
|
11 |
+
# Load the texture image
|
12 |
+
im = Image.open('defect.jpg')
|
13 |
+
im = im.convert('RGB') # Ensure the image is in RGB format
|
14 |
+
|
15 |
+
# Create random UV coordinates for the mesh
|
16 |
+
uv = np.random.rand(len(mesh.vertices), 2)
|
17 |
+
|
18 |
+
# Create material and apply texture
|
19 |
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
|
|
|
|
|
|
|
|
|
|
20 |
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
|
21 |
+
|
22 |
+
# Apply the texture to the original mesh
|
23 |
+
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
|
24 |
+
|
25 |
+
# Save the original and textured meshes to GLB format in memory
|
26 |
+
original_glb = io.BytesIO()
|
27 |
+
mesh.export(original_glb, file_type='glb')
|
28 |
+
original_glb.seek(0)
|
29 |
+
|
30 |
+
modified_glb = io.BytesIO()
|
31 |
+
textured_mesh.export(modified_glb, file_type='glb')
|
32 |
+
modified_glb.seek(0)
|
33 |
+
|
34 |
+
return original_glb, modified_glb
|
35 |
+
|
36 |
+
with gr.Blocks() as app:
|
37 |
+
gr.Markdown("### 3D Model Texture Application")
|
38 |
+
original_model = gr.Model3D(label="Original Model")
|
39 |
+
modified_model = gr.Model3D(label="Textured Model")
|
40 |
+
button = gr.Button("Visualize Texture")
|
41 |
+
button.click(visualize_texture, outputs=[original_model, modified_model])
|
42 |
+
|
43 |
+
app.launch()
|