Rahatara commited on
Commit
5adc72d
·
verified ·
1 Parent(s): 43b23b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -4
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  import trimesh
3
  import numpy as np
4
  from PIL import Image
@@ -11,7 +11,24 @@ def visualize_dynamic_texture(predefined_section, x_min, x_max, y_min, y_max, z_
11
  rust_texture = Image.open('rust_steel.png').convert('RGB')
12
 
13
  # Predefined sections
14
- if predefined_section == 'right compartments':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  new_uv[selected_indices, :] = uv[selected_indices, :]
16
 
17
  # Create material and apply the new texture
@@ -20,6 +37,17 @@ if predefined_section == 'right compartments':
20
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
21
 
22
  # Save the mesh to a temporary file
 
 
 
 
 
 
 
 
 
 
 
23
  y_min_range, y_max_range = train_bounds[0][1], train_bounds[1][1]
24
  z_min_range, z_max_range = train_bounds[0][2], train_bounds[1][2]
25
 
@@ -28,8 +56,10 @@ with gr.Blocks() as app:
28
  gr.Markdown("### 3D Model Texture Application with Predefined and Custom Sections")
29
  original_model = gr.Model3D('train.glb', label="Original Model")
30
  modified_model = gr.Model3D(label="Model with Applied Texture")
 
31
  # Dropdown for predefined and custom selection
32
  section_dropdown = gr.Radio(choices=['right compartments', 'left compartments', 'freight_body', 'custom'], label="Select Section", value='custom')
 
33
  # Custom sliders for the bounding box selection
34
  with gr.Row(visible=True) as custom_controls:
35
  x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)
@@ -41,10 +71,10 @@ with gr.Blocks() as app:
41
  z_min_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Min", value=z_min_range)
42
  z_max_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Max", value=z_max_range)
43
 
44
-
45
  # Toggle visibility of custom controls
46
  def toggle_custom_controls(predefined_section):
47
  return gr.update(visible=(predefined_section == 'custom'))
 
48
  section_dropdown.change(fn=toggle_custom_controls, inputs=section_dropdown, outputs=custom_controls)
49
 
50
  # Update model dynamically
@@ -56,5 +86,4 @@ with gr.Blocks() as app:
56
  for input_component in inputs:
57
  input_component.change(fn=update_model, inputs=inputs, outputs=modified_model)
58
 
59
-
60
  app.launch()
 
1
+ -import gradio as gr
2
  import trimesh
3
  import numpy as np
4
  from PIL import Image
 
11
  rust_texture = Image.open('rust_steel.png').convert('RGB')
12
 
13
  # Predefined sections
14
+ if predefined_section == 'right compartments':
15
+ selected_indices = np.where(mesh.vertices[:, 0] > (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
16
+ elif predefined_section == 'left compartments':
17
+ selected_indices = np.where(mesh.vertices[:, 0] <= (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
18
+ elif predefined_section == 'freight_body':
19
+ selected_indices = np.where((mesh.vertices[:, 0] >= train_bounds[0][0]) & (mesh.vertices[:, 0] <= train_bounds[1][0]) &
20
+ (mesh.vertices[:, 2] <= (train_bounds[0][2] + train_bounds[1][2]) / 2))[0]
21
+ elif predefined_section == 'custom':
22
+ # Use custom sliders for custom section
23
+ selected_indices = np.where((mesh.vertices[:, 0] >= x_min) & (mesh.vertices[:, 0] <= x_max) &
24
+ (mesh.vertices[:, 1] >= y_min) & (mesh.vertices[:, 1] <= y_max) &
25
+ (mesh.vertices[:, 2] >= z_min) & (mesh.vertices[:, 2] <= z_max))[0]
26
+ else:
27
+ selected_indices = np.array([])
28
+
29
+ # Initialize UV mapping
30
+ uv = np.random.rand(len(mesh.vertices), 2)
31
+ new_uv = np.zeros_like(uv)
32
  new_uv[selected_indices, :] = uv[selected_indices, :]
33
 
34
  # Create material and apply the new texture
 
37
  textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
38
 
39
  # Save the mesh to a temporary file
40
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
41
+ textured_mesh.export(temp_file.name, file_type='glb')
42
+ temp_file.close()
43
+ return temp_file.name
44
+
45
+ # Load train model to establish bounding box ranges
46
+ train_model = trimesh.load('train.glb', force='mesh')
47
+ train_bounds = train_model.bounds
48
+
49
+ # Get slider ranges based on train model bounds
50
+ x_min_range, x_max_range = train_bounds[0][0], train_bounds[1][0]
51
  y_min_range, y_max_range = train_bounds[0][1], train_bounds[1][1]
52
  z_min_range, z_max_range = train_bounds[0][2], train_bounds[1][2]
53
 
 
56
  gr.Markdown("### 3D Model Texture Application with Predefined and Custom Sections")
57
  original_model = gr.Model3D('train.glb', label="Original Model")
58
  modified_model = gr.Model3D(label="Model with Applied Texture")
59
+
60
  # Dropdown for predefined and custom selection
61
  section_dropdown = gr.Radio(choices=['right compartments', 'left compartments', 'freight_body', 'custom'], label="Select Section", value='custom')
62
+
63
  # Custom sliders for the bounding box selection
64
  with gr.Row(visible=True) as custom_controls:
65
  x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)
 
71
  z_min_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Min", value=z_min_range)
72
  z_max_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Max", value=z_max_range)
73
 
 
74
  # Toggle visibility of custom controls
75
  def toggle_custom_controls(predefined_section):
76
  return gr.update(visible=(predefined_section == 'custom'))
77
+
78
  section_dropdown.change(fn=toggle_custom_controls, inputs=section_dropdown, outputs=custom_controls)
79
 
80
  # Update model dynamically
 
86
  for input_component in inputs:
87
  input_component.change(fn=update_model, inputs=inputs, outputs=modified_model)
88
 
 
89
  app.launch()