Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,50 +2,58 @@ 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', force='mesh')
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
uv = np.random.rand(len(mesh.vertices), 2)
|
|
|
|
|
17 |
|
18 |
-
#
|
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 |
-
|
31 |
-
return
|
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
app.launch()
|
|
|
2 |
import trimesh
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
+
def visualize_texture(section):
|
|
|
7 |
mesh = trimesh.load('train.glb', force='mesh')
|
8 |
+
im = Image.open('defect.jpg').convert('RGB')
|
9 |
+
|
10 |
+
# Calculate bounds
|
11 |
+
bounds = mesh.bounds
|
12 |
+
min_bounds, max_bounds = bounds[0], bounds[1]
|
13 |
+
mid_x = (max_bounds[0] + min_bounds[0]) / 2
|
14 |
+
mid_y = (max_bounds[1] + min_bounds[1]) / 2
|
15 |
+
mid_z = (max_bounds[2] + min_bounds[2]) / 2
|
16 |
+
|
17 |
+
# Define sections
|
18 |
+
sections = {
|
19 |
+
'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
|
20 |
+
'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
|
21 |
+
'middle': np.where((mesh.vertices[:, 0] > min_bounds[0]) & (mesh.vertices[:, 0] < max_bounds[0]) &
|
22 |
+
(mesh.vertices[:, 1] > min_bounds[1]) & (mesh.vertices[:, 1] < max_bounds[1]) &
|
23 |
+
(mesh.vertices[:, 2] > min_bounds[2]) & (mesh.vertices[:, 2] < max_bounds[2]))[0],
|
24 |
+
'top': np.where(mesh.vertices[:, 1] > mid_y)[0],
|
25 |
+
'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
|
26 |
+
}
|
27 |
+
|
28 |
+
# Get indices for selected section
|
29 |
+
selected_indices = sections[section]
|
30 |
+
|
31 |
+
# Create UV coordinates
|
32 |
uv = np.random.rand(len(mesh.vertices), 2)
|
33 |
+
new_uv = np.zeros_like(uv)
|
34 |
+
new_uv[selected_indices, :] = uv[selected_indices, :]
|
35 |
|
36 |
+
# Create material and apply texture
|
|
|
|
|
|
|
37 |
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
|
|
|
|
|
|
38 |
color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=im, material=material)
|
|
|
|
|
39 |
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
|
40 |
+
|
41 |
+
return textured_mesh.export(file_type='glb')
|
42 |
|
43 |
with gr.Blocks() as app:
|
44 |
gr.Markdown("### 3D Model Texture Application")
|
45 |
original_model = gr.Model3D('train.glb', label="Original Model")
|
46 |
modified_model = gr.Model3D(label="Textured Model")
|
|
|
|
|
|
|
|
|
47 |
with gr.Row():
|
48 |
+
button_upper = gr.Button("Texture Upper Part")
|
49 |
+
button_lower = gr.Button("Texture Lower Part")
|
50 |
+
button_middle = gr.Button("Texture Middle")
|
51 |
+
button_top = gr.Button("Texture Top")
|
52 |
+
button_right = gr.Button("Texture Right")
|
53 |
+
button_upper.click(visualize_texture, inputs='upper', outputs=modified_model)
|
54 |
+
button_lower.click(visualize_texture, inputs='lower', outputs=modified_model)
|
55 |
+
button_middle.click(visualize_texture, inputs='middle', outputs=modified_model)
|
56 |
+
button_top.click(visualize_texture, inputs='top', outputs=modified_model)
|
57 |
+
button_right.click(visualize_texture, inputs='right', outputs=modified_model)
|
58 |
|
59 |
app.launch()
|