Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243444546476 unmodified lines5455565758596061626364653 unmodified linesfrom PIL import Imageimport tempfiledef apply_masked_visuals(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 indicessections = {'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],'middle': np.where((mesh.vertices[:, 0] >= min_bounds[0]) & (mesh.vertices[:, 0] <= max_bounds[0]) &(mesh.vertices[:, 1] >= min_bounds[1]) & (mesh.vertices[:, 1] <= max_bounds[1]) &(mesh.vertices[:, 2] >= min_bounds[2]) & (mesh.vertices[:, 2] <= max_bounds[2]))[0],'top': np.where(mesh.vertices[:, 1] > mid_y)[0],'right': np.where(mesh.vertices[:, 0] > mid_x)[0]}# Select the indices for the specified sectionselected_indices = sections[section]# Ensure that all indices are validvalid_indices = selected_indices[selected_indices < len(mesh.vertices)]# Create or re-initialize the UV arrayuv = np.random.rand(len(mesh.vertices), 2)# Generate new UV coordinates only for the selected sectionnew_uv = np.copy(uv)new_uv[valid_indices, :] = np.random.rand(len(valid_indices), 2)# Create a new material with the image texturematerial = trimesh.visual.texture.SimpleMaterial(image=im)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)# Create a new mesh with the updated visuals6 unmodified linesreturn temp_file.namewith gr.Blocks() as app:gr.Markdown("### 3D Model Texture Application")original_model = gr.Model3D('train.glb', label="Original Model")modified_model = gr.Model3D(label="Textured Model")section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")button = gr.Button("Apply Texture to Section")button.click(apply_masked_visuals, inputs=section_dropdown, outputs=modified_model)app.launch()3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243446 unmodified lines5152535455565758596061623 unmodified linesfrom PIL import Imageimport tempfiledef apply_rust_texture(section):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')rust_texture = Image.open('rust_steel.png').convert('RGB')# Train body selection logicbounds = mesh.boundsmin_bounds, max_bounds = bounds[0], bounds[1]body_height = max_bounds[2] - min_bounds[2]body_midpoint = (max_bounds + min_bounds) / 2# Define more sophisticated criteria for the body selectionif section == 'body':# Assuming the body is within certain height and centered horizontallyselected_indices = np.where((mesh.vertices[:, 2] >= min_bounds[2]) &(mesh.vertices[:, 2] <= min_bounds[2] + 0.75 * body_height) &(mesh.vertices[:, 0] >= min_bounds[0]) &(mesh.vertices[:, 0] <= max_bounds[0]))[0]elif section == 'roof':# Roof is the top quarter of the heightselected_indices = np.where(mesh.vertices[:, 2] > min_bounds[2] + 0.75 * body_height)[0]# Ensure indices are within boundsvalid_indices = selected_indices[selected_indices < len(mesh.vertices)]# Create or initialize UV mappinguv = np.random.rand(len(mesh.vertices), 2)# Apply the rust texture only to the selected indicesnew_uv = np.copy(uv)new_uv[valid_indices, :] = np.random.rand(len(valid_indices), 2)# Create a new material with the rust texture imagematerial = trimesh.visual.texture.SimpleMaterial(image=rust_texture)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)# Create a new mesh with the updated visuals6 unmodified linesreturn temp_file.namewith gr.Blocks() as app:gr.Markdown("### Apply Rust Texture to Train Body Section")original_model = gr.Model3D('train.glb', label="Original Model")modified_model = gr.Model3D(label="Textured Model")section_dropdown = gr.Dropdown(choices=['body', 'roof'], label="Select Section")button = gr.Button("Apply Rust Texture to Section")button.click(apply_rust_texture, inputs=section_dropdown, outputs=modified_model)app.launch()
app.py+27-30