Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(x_min, x_max, y_min, y_max, z_min, z_max):
|
8 |
+
# Load the GLB file
|
9 |
+
mesh = trimesh.load('train.glb', force='mesh')
|
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 UV coordinates for the mesh
|
16 |
+
uv = np.random.rand(len(mesh.vertices), 2)
|
17 |
+
|
18 |
+
# Determine vertices within the specified bounds
|
19 |
+
yellow_indices = [i for i, vertex in enumerate(mesh.vertices) if x_min <= vertex[0] <= x_max and y_min <= vertex[1] <= y_max and z_min <= vertex[2] <= z_max]
|
20 |
+
|
21 |
+
# Update UV coordinates for identified vertices
|
22 |
+
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
23 |
+
new_uv = np.zeros_like(uv)
|
24 |
+
new_uv[yellow_indices, :] = uv[yellow_indices, :]
|
25 |
+
|
26 |
+
color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=im, material=material)
|
27 |
+
|
28 |
+
# Apply the texture to the original mesh
|
29 |
+
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
|
30 |
+
modified_model = textured_mesh.export('trainm.glb')
|
31 |
+
return modified_model
|
32 |
+
|
33 |
+
with gr.Blocks() as app:
|
34 |
+
gr.Markdown("### 3D Model Texture Application")
|
35 |
+
original_model = gr.Model3D('train.glb', label="Original Model")
|
36 |
+
modified_model = gr.Model3D(label="Textured Model")
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
x_min = gr.Slider(minimum=-100, maximum=100, label="X Min")
|
40 |
+
x_max = gr.Slider(minimum=-100, maximum=100, label="X Max")
|
41 |
+
with gr.Row():
|
42 |
+
y_min = gr.Slider(minimum=-100, maximum=100, label="Y Min")
|
43 |
+
y_max = gr.Slider(minimum=-100, maximum=100, label="Y Max")
|
44 |
+
with gr.Row():
|
45 |
+
z_min = gr.Slider(minimum=-100, maximum=100, label="Z Min")
|
46 |
+
z_max = gr.Slider(minimum=-100, maximum=100, label="Z Max")
|
47 |
+
|
48 |
+
button = gr.Button("Visualize Texture")
|
49 |
+
button.click(visualize_texture, inputs=[x_min, x_max, y_min, y_max, z_min, z_max], outputs=[modified_model])
|
50 |
+
|
51 |
+
app.launch()
|