Rahatara commited on
Commit
03a3a4b
·
verified ·
1 Parent(s): 22cdf7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -3,56 +3,59 @@ 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
  mesh = trimesh.load('train.glb', force='mesh')
10
- im = Image.open('rust_steel.png').convert('RGB')
11
-
12
- # Calculate bounds
13
- bounds = mesh.bounds
14
- min_bounds, max_bounds = bounds[0], bounds[1]
15
- mid_x = (max_bounds[0] + min_bounds[0]) / 2
16
- mid_y = (max_bounds[1] + min_bounds[1]) / 2
17
- mid_z = (max_bounds[2] + min_bounds[2]) / 2
18
-
19
- # Define sections
20
- sections = {
21
- 'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
22
- 'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
23
- 'middle': np.where((mesh.vertices[:, 0] >= min_bounds[0]) & (mesh.vertices[:, 0] <= max_bounds[0]) &
24
- (mesh.vertices[:, 1] >= min_bounds[1]) & (mesh.vertices[:, 1] <= max_bounds[1]) &
25
- (mesh.vertices[:, 2] >= min_bounds[2]) & (mesh.vertices[:, 2] <= max_bounds[2]))[0],
26
- 'top': np.where(mesh.vertices[:, 1] > mid_y)[0],
27
- 'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
28
- }
29
-
30
- # Get indices for selected section
31
- selected_indices = sections[section]
32
-
33
- # Create UV coordinates
34
  uv = np.random.rand(len(mesh.vertices), 2)
35
  new_uv = np.zeros_like(uv)
36
  new_uv[selected_indices, :] = uv[selected_indices, :]
37
 
38
  # Create material and apply texture
39
- material = trimesh.visual.texture.SimpleMaterial(image=im)
40
- color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=im, material=material)
41
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
42
 
43
  # Save the mesh to a temporary file
44
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
45
  textured_mesh.export(temp_file.name, file_type='glb')
46
- temp_file.close() # Close the file so it can be read by Gradio
47
  return temp_file.name
48
 
 
 
 
 
 
 
 
49
  with gr.Blocks() as app:
50
- gr.Markdown("### 3D Model Texture Application")
51
  original_model = gr.Model3D('train.glb', label="Original Model")
52
  modified_model = gr.Model3D(label="Textured Model")
53
-
54
- section_dropdown = gr.Dropdown(choices=['right compartments', 'left compartments', 'freight_body' 'side_body'], label="Select Section")
55
- button = gr.Button("Visualize Texture")
56
- button.click(visualize_texture, inputs=section_dropdown, outputs=modified_model)
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  app.launch()
 
3
  import numpy as np
4
  from PIL import Image
5
  import tempfile
 
6
 
7
+ def visualize_dynamic_texture(x_min, x_max, y_min, y_max, z_min, z_max):
8
+ # Load the original mesh
9
  mesh = trimesh.load('train.glb', force='mesh')
10
+ rust_texture = Image.open('rust_steel.png').convert('RGB')
11
+
12
+ # Identify vertices within the given bounding box
13
+ selected_indices = np.where((mesh.vertices[:, 0] >= x_min) & (mesh.vertices[:, 0] <= x_max) &
14
+ (mesh.vertices[:, 1] >= y_min) & (mesh.vertices[:, 1] <= y_max) &
15
+ (mesh.vertices[:, 2] >= z_min) & (mesh.vertices[:, 2] <= z_max))[0]
16
+
17
+ # Initialize UV coordinates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  uv = np.random.rand(len(mesh.vertices), 2)
19
  new_uv = np.zeros_like(uv)
20
  new_uv[selected_indices, :] = uv[selected_indices, :]
21
 
22
  # Create material and apply texture
23
+ material = trimesh.visual.texture.SimpleMaterial(image=rust_texture)
24
+ color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=rust_texture, material=material)
25
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
26
 
27
  # Save the mesh to a temporary file
28
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
29
  textured_mesh.export(temp_file.name, file_type='glb')
30
+ temp_file.close()
31
  return temp_file.name
32
 
33
+ # Get bounding box values from the original mesh to define slider ranges
34
+ mesh = trimesh.load('train.glb', force='mesh')
35
+ bounds = mesh.bounds
36
+ x_min_range, x_max_range = bounds[0][0], bounds[1][0]
37
+ y_min_range, y_max_range = bounds[0][1], bounds[1][1]
38
+ z_min_range, z_max_range = bounds[0][2], bounds[1][2]
39
+
40
  with gr.Blocks() as app:
41
+ gr.Markdown("### Select Model Portion with Sliders")
42
  original_model = gr.Model3D('train.glb', label="Original Model")
43
  modified_model = gr.Model3D(label="Textured Model")
44
+
45
+ # Add sliders for bounding box selection
46
+ with gr.Row():
47
+ x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)
48
+ x_max_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Max", value=x_max_range)
49
+
50
+ with gr.Row():
51
+ y_min_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Min", value=y_min_range)
52
+ y_max_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Max", value=y_max_range)
53
+
54
+ with gr.Row():
55
+ z_min_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Min", value=z_min_range)
56
+ z_max_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Max", value=z_max_range)
57
+
58
+ button = gr.Button("Apply Texture")
59
+ 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)
60
 
61
  app.launch()