Rahatara commited on
Commit
3865c72
·
verified ·
1 Parent(s): 3d4bb70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -40
app.py CHANGED
@@ -3,60 +3,56 @@ import trimesh
3
  import numpy as np
4
  from PIL import Image
5
  import tempfile
 
6
 
7
- def apply_rust_texture(section):
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
- # Train body selection logic
13
  bounds = mesh.bounds
14
  min_bounds, max_bounds = bounds[0], bounds[1]
15
- body_height = max_bounds[2] - min_bounds[2]
16
- body_midpoint = (max_bounds + min_bounds) / 2
17
-
18
- # Define more sophisticated criteria for the body selection
19
- if section == 'body':
20
- # Assuming the body is within certain height and centered horizontally
21
- selected_indices = np.where((mesh.vertices[:, 2] >= min_bounds[2]) &
22
- (mesh.vertices[:, 2] <= min_bounds[2] + 0.75 * body_height) &
23
- (mesh.vertices[:, 0] >= min_bounds[0]) &
24
- (mesh.vertices[:, 0] <= max_bounds[0]))[0]
25
-
26
- elif section == 'roof':
27
- # Roof is the top quarter of the height
28
- selected_indices = np.where(mesh.vertices[:, 2] > min_bounds[2] + 0.75 * body_height)[0]
29
-
30
- # Ensure indices are within bounds
31
- valid_indices = selected_indices[selected_indices < len(mesh.vertices)]
32
-
33
- # Create or initialize UV mapping
34
  uv = np.random.rand(len(mesh.vertices), 2)
 
 
35
 
36
- # Apply the rust texture only to the selected indices
37
- new_uv = np.copy(uv)
38
- new_uv[valid_indices, :] = np.random.rand(len(valid_indices), 2)
 
39
 
40
- # Create a new material with the rust texture image
41
- material = trimesh.visual.texture.SimpleMaterial(image=rust_texture)
42
- new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
43
-
44
- # Create a new mesh with the updated visuals
45
- textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=new_visuals, validate=True, process=False)
46
-
47
- # Save the modified mesh to a temporary file for Gradio visualization
48
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
49
  textured_mesh.export(temp_file.name, file_type='glb')
50
- temp_file.close()
51
  return temp_file.name
52
 
53
  with gr.Blocks() as app:
54
- gr.Markdown("### Apply Rust Texture to Train Body Section")
55
  original_model = gr.Model3D('train.glb', label="Original Model")
56
  modified_model = gr.Model3D(label="Textured Model")
57
-
58
- section_dropdown = gr.Dropdown(choices=['body', 'roof'], label="Select Section")
59
- button = gr.Button("Apply Rust Texture to Section")
60
- button.click(apply_rust_texture, inputs=section_dropdown, outputs=modified_model)
61
 
62
  app.launch()
 
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=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
55
+ button = gr.Button("Visualize Texture")
56
+ button.click(visualize_texture, inputs=section_dropdown, outputs=modified_model)
57
 
58
  app.launch()