Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,19 @@ import numpy as np
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
|
7 |
-
def
|
8 |
# Load the original mesh
|
9 |
mesh = trimesh.load('train.glb', force='mesh')
|
10 |
im = Image.open('rust_steel.png').convert('RGB')
|
11 |
|
12 |
-
# Calculate bounding box coordinates
|
13 |
bounds = mesh.bounds
|
14 |
min_bounds, max_bounds = bounds[0], bounds[1]
|
15 |
mid_x = (max_bounds[0] + min_bounds[0]) / 2
|
16 |
mid_y = (max_bounds[1] + min_bounds[1]) / 2
|
17 |
mid_z = (max_bounds[2] + min_bounds[2]) / 2
|
18 |
|
19 |
-
# Define sections with indices
|
20 |
sections = {
|
21 |
'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
|
22 |
'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
|
@@ -30,22 +30,26 @@ def apply_texture_to_selected(section):
|
|
30 |
# Select the indices for the specified section
|
31 |
selected_indices = sections[section]
|
32 |
|
33 |
-
# Initialize
|
34 |
-
if mesh.visual.kind == 'texture':
|
35 |
original_uv = mesh.visual.uv
|
36 |
else:
|
37 |
original_uv = np.random.rand(len(mesh.vertices), 2)
|
38 |
|
39 |
-
#
|
40 |
new_uv = np.copy(original_uv)
|
41 |
new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)
|
42 |
|
43 |
-
#
|
|
|
|
|
|
|
44 |
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
45 |
new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
|
46 |
|
47 |
-
# Create a new mesh with
|
48 |
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)
|
|
|
49 |
|
50 |
# Save the modified mesh to a temporary file for Gradio visualization
|
51 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
|
@@ -60,6 +64,6 @@ with gr.Blocks() as app:
|
|
60 |
|
61 |
section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
|
62 |
button = gr.Button("Apply Texture to Section")
|
63 |
-
button.click(
|
64 |
|
65 |
app.launch()
|
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
|
7 |
+
def apply_masked_texture(section):
|
8 |
# Load the original mesh
|
9 |
mesh = trimesh.load('train.glb', force='mesh')
|
10 |
im = Image.open('rust_steel.png').convert('RGB')
|
11 |
|
12 |
+
# Calculate the bounding box coordinates
|
13 |
bounds = mesh.bounds
|
14 |
min_bounds, max_bounds = bounds[0], bounds[1]
|
15 |
mid_x = (max_bounds[0] + min_bounds[0]) / 2
|
16 |
mid_y = (max_bounds[1] + min_bounds[1]) / 2
|
17 |
mid_z = (max_bounds[2] + min_bounds[2]) / 2
|
18 |
|
19 |
+
# Define sections with relevant indices
|
20 |
sections = {
|
21 |
'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
|
22 |
'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
|
|
|
30 |
# Select the indices for the specified section
|
31 |
selected_indices = sections[section]
|
32 |
|
33 |
+
# Initialize UV mapping and retain original visuals for unselected vertices
|
34 |
+
if mesh.visual.kind == 'texture' and mesh.visual.uv is not None:
|
35 |
original_uv = mesh.visual.uv
|
36 |
else:
|
37 |
original_uv = np.random.rand(len(mesh.vertices), 2)
|
38 |
|
39 |
+
# Create new UV coordinates only for the selected region
|
40 |
new_uv = np.copy(original_uv)
|
41 |
new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)
|
42 |
|
43 |
+
# Preserve the original colors and visuals for non-selected sections
|
44 |
+
original_colors = mesh.visual.vertex_colors if mesh.visual.vertex_colors is not None else np.ones((len(mesh.vertices), 4)) * 255
|
45 |
+
|
46 |
+
# Create a new material with the texture image
|
47 |
material = trimesh.visual.texture.SimpleMaterial(image=im)
|
48 |
new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
|
49 |
|
50 |
+
# Create a new mesh with both original and updated visuals
|
51 |
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)
|
52 |
+
textured_mesh.visual.vertex_colors = original_colors # Retain original vertex colors
|
53 |
|
54 |
# Save the modified mesh to a temporary file for Gradio visualization
|
55 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
|
|
|
64 |
|
65 |
section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
|
66 |
button = gr.Button("Apply Texture to Section")
|
67 |
+
button.click(apply_masked_texture, inputs=section_dropdown, outputs=modified_model)
|
68 |
|
69 |
app.launch()
|