Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines456789101112131415161718192021227 unmodified lines303132333435363738394041424344454647484950518 unmodified lines6061626364653 unmodified linesfrom PIL import Imageimport tempfiledef apply_texture_to_selected(section):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')im = Image.open('rust_steel.png').convert('RGB')# Calculate bounding box coordinatesbounds = mesh.boundsmin_bounds, max_bounds = bounds[0], bounds[1]mid_x = (max_bounds[0] + min_bounds[0]) / 2mid_y = (max_bounds[1] + min_bounds[1]) / 2mid_z = (max_bounds[2] + min_bounds[2]) / 2# Define sections with indicessections = {'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],7 unmodified lines# Select the indices for the specified sectionselected_indices = sections[section]# Initialize the UV mapping with default or existing valuesif mesh.visual.kind == 'texture':original_uv = mesh.visual.uvelse:original_uv = np.random.rand(len(mesh.vertices), 2)# Generate new UV coordinates for the selected region onlynew_uv = np.copy(original_uv)new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)# Create a new material using the texture imagematerial = trimesh.visual.texture.SimpleMaterial(image=im)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)# Create a new mesh with the updated visual datatextured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)# Save the modified mesh to a temporary file for Gradio visualizationtemp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')8 unmodified linessection_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")button = gr.Button("Apply Texture to Section")button.click(apply_texture_to_selected, inputs=section_dropdown, outputs=modified_model)app.launch()3 unmodified lines456789101112131415161718192021227 unmodified lines30313233343536373839404142434445464748495051525354558 unmodified lines6465666768693 unmodified linesfrom PIL import Imageimport tempfiledef apply_masked_texture(section):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')im = Image.open('rust_steel.png').convert('RGB')# Calculate the bounding box coordinatesbounds = mesh.boundsmin_bounds, max_bounds = bounds[0], bounds[1]mid_x = (max_bounds[0] + min_bounds[0]) / 2mid_y = (max_bounds[1] + min_bounds[1]) / 2mid_z = (max_bounds[2] + min_bounds[2]) / 2# Define sections with relevant indicessections = {'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],7 unmodified lines# Select the indices for the specified sectionselected_indices = sections[section]# Initialize UV mapping and retain original visuals for unselected verticesif mesh.visual.kind == 'texture' and mesh.visual.uv is not None:original_uv = mesh.visual.uvelse:original_uv = np.random.rand(len(mesh.vertices), 2)# Create new UV coordinates only for the selected regionnew_uv = np.copy(original_uv)new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)# Preserve the original colors and visuals for non-selected sectionsoriginal_colors = mesh.visual.vertex_colors if mesh.visual.vertex_colors is not None else np.ones((len(mesh.vertices), 4)) * 255# Create a new material with the texture imagematerial = trimesh.visual.texture.SimpleMaterial(image=im)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)# Create a new mesh with both original and updated visualstextured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)textured_mesh.visual.vertex_colors = original_colors # Retain original vertex colors# Save the modified mesh to a temporary file for Gradio visualizationtemp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')8 unmodified linessection_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")button = gr.Button("Apply Texture to Section")button.click(apply_masked_texture, inputs=section_dropdown, outputs=modified_model)app.launch()
app.py+13-9