Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,9 @@ 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 |
-
|
| 11 |
-
# Load the new texture image
|
| 12 |
im = Image.open('rust_steel.png').convert('RGB')
|
| 13 |
|
| 14 |
# Calculate bounding box coordinates
|
|
@@ -18,7 +16,7 @@ def apply_masked_texture(section):
|
|
| 18 |
mid_y = (max_bounds[1] + min_bounds[1]) / 2
|
| 19 |
mid_z = (max_bounds[2] + min_bounds[2]) / 2
|
| 20 |
|
| 21 |
-
# Define sections
|
| 22 |
sections = {
|
| 23 |
'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
|
| 24 |
'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
|
|
@@ -29,23 +27,27 @@ def apply_masked_texture(section):
|
|
| 29 |
'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
|
| 30 |
}
|
| 31 |
|
| 32 |
-
#
|
| 33 |
selected_indices = sections[section]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
if mesh.visual.vertex_colors is not None:
|
| 37 |
-
|
| 38 |
else:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
new_vertex_colors[idx, :] = [255, 255, 0, 255] # Example yellow RGBA color
|
| 44 |
|
| 45 |
-
# Apply the new visual
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
# Save to a temporary file for Gradio visualization
|
| 49 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
|
| 50 |
textured_mesh.export(temp_file.name, file_type='glb')
|
| 51 |
temp_file.close()
|
|
@@ -58,6 +60,6 @@ with gr.Blocks() as app:
|
|
| 58 |
|
| 59 |
section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
|
| 60 |
button = gr.Button("Apply Texture to Section")
|
| 61 |
-
button.click(
|
| 62 |
|
| 63 |
app.launch()
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
+
def apply_texture_with_mask(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 bounding box coordinates
|
|
|
|
| 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],
|
|
|
|
| 27 |
'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
|
| 28 |
}
|
| 29 |
|
| 30 |
+
# Select the indices for the specified section
|
| 31 |
selected_indices = sections[section]
|
| 32 |
|
| 33 |
+
# Preserve the original visual appearance
|
| 34 |
if mesh.visual.vertex_colors is not None:
|
| 35 |
+
vertex_colors = np.copy(mesh.visual.vertex_colors)
|
| 36 |
else:
|
| 37 |
+
vertex_colors = np.ones((len(mesh.vertices), 4)) * 255 # Assuming the default color is white
|
| 38 |
+
|
| 39 |
+
# Apply the new texture only to the selected section
|
| 40 |
+
uv = np.random.rand(len(mesh.vertices), 2) # Generate new UV coordinates
|
| 41 |
+
uv_colors = trimesh.visual.uv_to_color(uv[selected_indices], im) # Map UV coordinates to colors in the image
|
| 42 |
|
| 43 |
+
for i, idx in enumerate(selected_indices):
|
| 44 |
+
vertex_colors[idx] = uv_colors[i] # Apply the new texture color to selected indices
|
|
|
|
| 45 |
|
| 46 |
+
# Apply the new visual colors to the mesh
|
| 47 |
+
color_visuals = trimesh.visual.ColorVisuals(vertex_colors=vertex_colors)
|
| 48 |
+
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_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')
|
| 52 |
textured_mesh.export(temp_file.name, file_type='glb')
|
| 53 |
temp_file.close()
|
|
|
|
| 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(apply_texture_with_mask, inputs=section_dropdown, outputs=modified_model)
|
| 64 |
|
| 65 |
app.launch()
|