Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 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=['right compartments', 'left compartments', 'freight_body' 'side_body'], label="Select Section")button = gr.Button("Visualize Texture")button.click(visualize_texture, inputs=section_dropdown, outputs=modified_model)app.launch()2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960612 unmodified linesimport numpy as npfrom PIL import Imageimport tempfiledef visualize_dynamic_texture(x_min, x_max, y_min, y_max, z_min, z_max):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')rust_texture = Image.open('rust_steel.png').convert('RGB')# Identify vertices within the given bounding boxselected_indices = np.where((mesh.vertices[:, 0] >= x_min) & (mesh.vertices[:, 0] <= x_max) &(mesh.vertices[:, 1] >= y_min) & (mesh.vertices[:, 1] <= y_max) &(mesh.vertices[:, 2] >= z_min) & (mesh.vertices[:, 2] <= z_max))[0]# Initialize 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=rust_texture)color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=rust_texture, 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()return temp_file.name# Get bounding box values from the original mesh to define slider rangesmesh = trimesh.load('train.glb', force='mesh')bounds = mesh.boundsx_min_range, x_max_range = bounds[0][0], bounds[1][0]y_min_range, y_max_range = bounds[0][1], bounds[1][1]z_min_range, z_max_range = bounds[0][2], bounds[1][2]with gr.Blocks() as app:gr.Markdown("### Select Model Portion with Sliders")original_model = gr.Model3D('train.glb', label="Original Model")modified_model = gr.Model3D(label="Textured Model")# Add sliders for bounding box selectionwith gr.Row():x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)x_max_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Max", value=x_max_range)with gr.Row():y_min_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Min", value=y_min_range)y_max_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Max", value=y_max_range)with gr.Row():z_min_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Min", value=z_min_range)z_max_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Max", value=z_max_range)button = gr.Button("Apply Texture")button.click(visualize_dynamic_texture, inputs=[x_min_slider, x_max_slider, y_min_slider, y_max_slider, z_min_slider, z_max_slider], outputs=modified_model)app.launch()
app.py+37-34