Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061622 unmodified linesimport numpy as npfrom 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 visualstextured_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')textured_mesh.export(temp_file.name, file_type='glb')temp_file.close()return 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()2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657582 unmodified linesimport numpy as npfrom PIL import Imageimport tempfileimport osdef visualize_texture(section):mesh = trimesh.load('train.glb', force='mesh')im = Image.open('rust_steel.png').convert('RGB')# Calculate boundsbounds = 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 sectionssections = {'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]}# Get indices for selected sectionselected_indices = sections[section]# Create UV coordinatesuv = np.random.rand(len(mesh.vertices), 2)new_uv = np.zeros_like(uv)new_uv[selected_indices, :] = uv[selected_indices, :]# Create material and apply texturematerial = trimesh.visual.texture.SimpleMaterial(image=im)color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=im, material=material)textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)# Save the mesh to a temporary filetemp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')textured_mesh.export(temp_file.name, file_type='glb')temp_file.close() # Close the file so it can be read by Gradioreturn 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("Visualize Texture")button.click(visualize_texture, inputs=section_dropdown, outputs=modified_model)app.launch()
app.py+36-40