Rahatara commited on
Commit
f795533
·
verified ·
1 Parent(s): fc41a87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -4,19 +4,19 @@ 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
  im = Image.open('rust_steel.png').convert('RGB')
11
 
12
- # Calculate the bounding box coordinates
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 with relevant indices
20
  sections = {
21
  'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
22
  'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
@@ -30,26 +30,24 @@ def apply_masked_texture(section):
30
  # Select the indices for the specified section
31
  selected_indices = sections[section]
32
 
33
- # Initialize UV mapping and retain original visuals for unselected vertices
34
- if mesh.visual.kind == 'texture' and mesh.visual.uv is not None:
35
- original_uv = mesh.visual.uv
 
36
  else:
37
- original_uv = np.random.rand(len(mesh.vertices), 2)
38
 
39
- # Create new UV coordinates only for the selected region
40
- new_uv = np.copy(original_uv)
 
41
  new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)
42
 
43
- # Preserve the original colors and visuals for non-selected sections
44
- original_colors = mesh.visual.vertex_colors if mesh.visual.vertex_colors is not None else np.ones((len(mesh.vertices), 4)) * 255
45
-
46
- # Create a new material with the texture image
47
  material = trimesh.visual.texture.SimpleMaterial(image=im)
48
  new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
49
 
50
- # Create a new mesh with both original and updated visuals
51
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)
52
- textured_mesh.visual.vertex_colors = original_colors # Retain original vertex colors
53
 
54
  # Save the modified mesh to a temporary file for Gradio visualization
55
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
@@ -64,6 +62,6 @@ with gr.Blocks() as app:
64
 
65
  section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
66
  button = gr.Button("Apply Texture to Section")
67
- button.click(apply_masked_texture, inputs=section_dropdown, outputs=modified_model)
68
 
69
  app.launch()
 
4
  from PIL import Image
5
  import tempfile
6
 
7
+ def apply_masked_visuals(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
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 with indices
20
  sections = {
21
  'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
22
  'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
 
30
  # Select the indices for the specified section
31
  selected_indices = sections[section]
32
 
33
+ # Check if the mesh has existing visuals
34
+ if mesh.visual.kind == 'texture':
35
+ visual = mesh.visual.copy()
36
+ visual.update_vertices(mask=np.isin(range(len(mesh.vertices)), selected_indices))
37
  else:
38
+ visual = trimesh.visual.ColorVisuals(vertex_colors=np.ones((len(mesh.vertices), 4)) * 255)
39
 
40
+ # Prepare the new visual properties (UV or color updates)
41
+ uv = np.copy(visual.uv) if visual.kind == 'texture' else np.random.rand(len(mesh.vertices), 2)
42
+ new_uv = np.copy(uv)
43
  new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)
44
 
45
+ # Create a new material with the image texture
 
 
 
46
  material = trimesh.visual.texture.SimpleMaterial(image=im)
47
  new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
48
 
49
+ # Create a new mesh with the updated visuals
50
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)
 
51
 
52
  # Save the modified mesh to a temporary file for Gradio visualization
53
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
 
62
 
63
  section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
64
  button = gr.Button("Apply Texture to Section")
65
+ button.click(apply_masked_visuals, inputs=section_dropdown, outputs=modified_model)
66
 
67
  app.launch()