import gradio as gr import trimesh import numpy as np from PIL import Image import io def visualize_texture(x_min, x_max, y_min, y_max, z_min, z_max): # Load the GLB file mesh = trimesh.load('train.glb', force='mesh') # Load the texture image im = Image.open('defect.jpg') im = im.convert('RGB') # Ensure the image is in RGB format # Create UV coordinates for the mesh uv = np.random.rand(len(mesh.vertices), 2) # Determine vertices within the specified bounds yellow_indices = [i for i, vertex in enumerate(mesh.vertices) if x_min <= vertex[0] <= x_max and y_min <= vertex[1] <= y_max and z_min <= vertex[2] <= z_max] # Update UV coordinates for identified vertices material = trimesh.visual.texture.SimpleMaterial(image=im) new_uv = np.zeros_like(uv) new_uv[yellow_indices, :] = uv[yellow_indices, :] color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=im, material=material) # Apply the texture to the original mesh textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False) modified_model = textured_mesh.export('trainm.glb') return modified_model with 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") with gr.Row(): x_min = gr.Slider(minimum=-100, maximum=100, label="X Min") x_max = gr.Slider(minimum=-100, maximum=100, label="X Max") with gr.Row(): y_min = gr.Slider(minimum=-100, maximum=100, label="Y Min") y_max = gr.Slider(minimum=-100, maximum=100, label="Y Max") with gr.Row(): z_min = gr.Slider(minimum=-100, maximum=100, label="Z Min") z_max = gr.Slider(minimum=-100, maximum=100, label="Z Max") button = gr.Button("Visualize Texture") button.click(visualize_texture, inputs=[x_min, x_max, y_min, y_max, z_min, z_max], outputs=[modified_model]) app.launch()