Rahatara commited on
Commit
6f62f26
·
verified ·
1 Parent(s): 4bf36eb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import trimesh
3
+ import numpy as np
4
+ from PIL import Image
5
+ import tempfile
6
+ import os
7
+
8
+ def visualize_texture(section):
9
+ # Load the original mesh
10
+ mesh = trimesh.load('train.glb', force='mesh')
11
+ im = Image.open('rust_steel.png').convert('RGB')
12
+
13
+ # Calculate the bounding box coordinates
14
+ bounds = mesh.bounds
15
+ min_bounds, max_bounds = bounds[0], bounds[1]
16
+ mid_x = (max_bounds[0] + min_bounds[0]) / 2
17
+ mid_y = (max_bounds[1] + min_bounds[1]) / 2
18
+ mid_z = (max_bounds[2] + min_bounds[2]) / 2
19
+
20
+ # Define sections with relevant indices
21
+ sections = {
22
+ 'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
23
+ 'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
24
+ 'middle': np.where((mesh.vertices[:, 0] >= min_bounds[0]) & (mesh.vertices[:, 0] <= max_bounds[0]) &
25
+ (mesh.vertices[:, 1] >= min_bounds[1]) & (mesh.vertices[:, 1] <= max_bounds[1]) &
26
+ (mesh.vertices[:, 2] >= min_bounds[2]) & (mesh.vertices[:, 2] <= max_bounds[2]))[0],
27
+ 'top': np.where(mesh.vertices[:, 1] > mid_y)[0],
28
+ 'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
29
+ }
30
+
31
+ # Get indices for the selected section
32
+ selected_indices = sections[section]
33
+
34
+ # Generate new UV coordinates and maintain existing colors
35
+ uv = np.random.rand(len(mesh.vertices), 2)
36
+ original_colors = mesh.visual.vertex_colors
37
+
38
+ # Apply the new UV coordinates only to the selected section, keeping the original color elsewhere
39
+ new_uv = np.zeros_like(uv)
40
+ new_uv[selected_indices, :] = uv[selected_indices, :]
41
+ updated_vertex_colors = np.copy(original_colors)
42
+
43
+ # Blend or mix the new texture into existing colors (example mix formula)
44
+ blend_factor = 0.5 # Adjust to change blending strength
45
+ new_texture = trimesh.visual.texture.SimpleMaterial(image=im)
46
+ for index in selected_indices:
47
+ updated_vertex_colors[index] = blend_factor * new_texture.image[index % new_texture.image.shape[0], index % new_texture.image.shape[1]] + (1 - blend_factor) * original_colors[index]
48
+
49
+ # Create a texture visual with blended colors and updated UVs
50
+ color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, vertex_colors=updated_vertex_colors, material=new_texture)
51
+
52
+ # Create a new mesh with the visual
53
+ textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
54
+
55
+ # Save the mesh to a temporary file
56
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
57
+ textured_mesh.export(temp_file.name, file_type='glb')
58
+ temp_file.close()
59
+ return temp_file.name
60
+
61
+ with gr.Blocks() as app:
62
+ gr.Markdown("### 3D Model Texture Application")
63
+ original_model = gr.Model3D('train.glb', label="Original Model")
64
+ modified_model = gr.Model3D(label="Textured Model")
65
+
66
+ section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
67
+ button = gr.Button("Visualize Texture")
68
+ button.click(visualize_texture, inputs=section_dropdown, outputs=modified_model)
69
+
70
+ app.launch()