Rahatara commited on
Commit
9b2ee46
·
verified ·
1 Parent(s): 516de62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -4,11 +4,9 @@ import numpy as np
4
  from PIL import Image
5
  import tempfile
6
 
7
- def apply_masked_texture(section):
8
  # Load the original mesh
9
  mesh = trimesh.load('train.glb', force='mesh')
10
-
11
- # Load the new texture image
12
  im = Image.open('rust_steel.png').convert('RGB')
13
 
14
  # Calculate bounding box coordinates
@@ -18,7 +16,7 @@ def apply_masked_texture(section):
18
  mid_y = (max_bounds[1] + min_bounds[1]) / 2
19
  mid_z = (max_bounds[2] + min_bounds[2]) / 2
20
 
21
- # Define sections
22
  sections = {
23
  'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
24
  'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
@@ -29,23 +27,27 @@ def apply_masked_texture(section):
29
  'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
30
  }
31
 
32
- # Get indices for the selected section
33
  selected_indices = sections[section]
34
 
35
- # Copy the original visual properties (colors, UVs)
36
  if mesh.visual.vertex_colors is not None:
37
- new_vertex_colors = np.copy(mesh.visual.vertex_colors)
38
  else:
39
- new_vertex_colors = np.ones((len(mesh.vertices), 4)) * 255 # Assuming original is white or neutral
 
 
 
 
40
 
41
- # Apply the new texture to only the selected section
42
- for idx in selected_indices:
43
- new_vertex_colors[idx, :] = [255, 255, 0, 255] # Example yellow RGBA color
44
 
45
- # Apply the new visual properties
46
- textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=trimesh.visual.ColorVisuals(vertex_colors=new_vertex_colors), validate=True, process=False)
 
47
 
48
- # Save to a temporary file for Gradio visualization
49
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
50
  textured_mesh.export(temp_file.name, file_type='glb')
51
  temp_file.close()
@@ -58,6 +60,6 @@ with gr.Blocks() as app:
58
 
59
  section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
60
  button = gr.Button("Apply Texture to Section")
61
- button.click(apply_masked_texture, inputs=section_dropdown, outputs=modified_model)
62
 
63
  app.launch()
 
4
  from PIL import Image
5
  import tempfile
6
 
7
+ def apply_texture_with_mask(section):
8
  # Load the original mesh
9
  mesh = trimesh.load('train.glb', force='mesh')
 
 
10
  im = Image.open('rust_steel.png').convert('RGB')
11
 
12
  # Calculate bounding box coordinates
 
16
  mid_y = (max_bounds[1] + min_bounds[1]) / 2
17
  mid_z = (max_bounds[2] + min_bounds[2]) / 2
18
 
19
+ # Define sections with indices
20
  sections = {
21
  'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
22
  'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
 
27
  'right': np.where(mesh.vertices[:, 0] > mid_x)[0]
28
  }
29
 
30
+ # Select the indices for the specified section
31
  selected_indices = sections[section]
32
 
33
+ # Preserve the original visual appearance
34
  if mesh.visual.vertex_colors is not None:
35
+ vertex_colors = np.copy(mesh.visual.vertex_colors)
36
  else:
37
+ vertex_colors = np.ones((len(mesh.vertices), 4)) * 255 # Assuming the default color is white
38
+
39
+ # Apply the new texture only to the selected section
40
+ uv = np.random.rand(len(mesh.vertices), 2) # Generate new UV coordinates
41
+ uv_colors = trimesh.visual.uv_to_color(uv[selected_indices], im) # Map UV coordinates to colors in the image
42
 
43
+ for i, idx in enumerate(selected_indices):
44
+ vertex_colors[idx] = uv_colors[i] # Apply the new texture color to selected indices
 
45
 
46
+ # Apply the new visual colors to the mesh
47
+ color_visuals = trimesh.visual.ColorVisuals(vertex_colors=vertex_colors)
48
+ textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
49
 
50
+ # Save the modified mesh to a temporary file for Gradio visualization
51
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
52
  textured_mesh.export(temp_file.name, file_type='glb')
53
  temp_file.close()
 
60
 
61
  section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
62
  button = gr.Button("Apply Texture to Section")
63
+ button.click(apply_texture_with_mask, inputs=section_dropdown, outputs=modified_model)
64
 
65
  app.launch()