Rahatara commited on
Commit
07730ad
·
verified ·
1 Parent(s): 03a3a4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -4,22 +4,33 @@ 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)
@@ -30,32 +41,34 @@ def visualize_dynamic_texture(x_min, x_max, y_min, y_max, z_min, z_max):
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()
 
 
 
4
  from PIL import Image
5
  import tempfile
6
 
7
+ def visualize_dynamic_texture(predefined_section, 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('/mnt/data/A_texture_image_that_combines_rust_and_yellow_patt.png').convert('RGB')
11
 
12
+ # Predefined sections
13
+ if predefined_section == 'right compartments':
14
+ selected_indices = np.where(mesh.vertices[:, 0] > (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
15
+ elif predefined_section == 'left compartments':
16
+ selected_indices = np.where(mesh.vertices[:, 0] <= (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
17
+ elif predefined_section == 'freight_body':
18
+ selected_indices = np.where((mesh.vertices[:, 0] >= train_bounds[0][0]) & (mesh.vertices[:, 0] <= train_bounds[1][0]) &
19
+ (mesh.vertices[:, 2] <= (train_bounds[0][2] + train_bounds[1][2]) / 2))[0]
20
+ elif predefined_section == 'custom':
21
+ # Use custom sliders for custom section
22
+ selected_indices = np.where((mesh.vertices[:, 0] >= x_min) & (mesh.vertices[:, 0] <= x_max) &
23
+ (mesh.vertices[:, 1] >= y_min) & (mesh.vertices[:, 1] <= y_max) &
24
+ (mesh.vertices[:, 2] >= z_min) & (mesh.vertices[:, 2] <= z_max))[0]
25
+ else:
26
+ selected_indices = np.array([])
27
 
28
+ # Initialize UV mapping
29
  uv = np.random.rand(len(mesh.vertices), 2)
30
  new_uv = np.zeros_like(uv)
31
  new_uv[selected_indices, :] = uv[selected_indices, :]
32
 
33
+ # Create material and apply the new texture
34
  material = trimesh.visual.texture.SimpleMaterial(image=rust_texture)
35
  color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=rust_texture, material=material)
36
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
 
41
  temp_file.close()
42
  return temp_file.name
43
 
44
+ # Load the train model to get its bounds
45
+ train_model = trimesh.load('train.glb', force='mesh')
46
+ train_bounds = train_model.bounds
 
 
 
47
 
48
+ # Get bounds for slider ranges
49
+ x_min_range, x_max_range = train_bounds[0][0], train_bounds[1][0]
50
+ y_min_range, y_max_range = train_bounds[0][1], train_bounds[1][1]
51
+ z_min_range, z_max_range = train_bounds[0][2], train_bounds[1][2]
52
+
53
+ # Create Gradio UI with predefined and custom selection options
54
  with gr.Blocks() as app:
55
+ gr.Markdown("### 3D Model Texture Application with Real-Time Visualization")
56
  original_model = gr.Model3D('train.glb', label="Original Model")
57
  modified_model = gr.Model3D(label="Textured Model")
58
 
59
+ section_dropdown = gr.Dropdown(choices=['right compartments', 'left compartments', 'freight_body', 'custom'], label="Select Section", value='custom')
60
+
61
+ # Add sliders for bounding box selection, visible only when 'custom' is selected
62
+ with gr.Row(visible=True) as custom_controls:
63
  x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)
64
  x_max_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Max", value=x_max_range)
65
 
 
66
  y_min_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Min", value=y_min_range)
67
  y_max_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Max", value=y_max_range)
68
 
69
+ z_min_slider = gr.Slider(minimum=z_min_range, maximum z_max_range, step=0.01, label="Z Min", value=z_min_range)
70
+ z_max_slider = gr.Slider(minimum=z_min_range, maximum z_max_range, step=0.01, label="Z Max", value z_max_range)
 
 
 
 
71
 
72
+ # Toggle visibility of custom controls
73
+ def toggle_custom_controls(predefined_section):
74
+ return gr.update(visible=(predefined_section == 'custom'))