Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,269 +1,241 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import trimesh
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
-
|
| 8 |
-
|
| 9 |
import tempfile
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
# Function to visualize texture based on selection criteria
|
| 100 |
-
def visualize_dynamic_texture(
|
| 101 |
# Load the original mesh
|
| 102 |
mesh = trimesh.load('train.glb', force='mesh')
|
| 103 |
-
|
| 104 |
|
| 105 |
# Predefined sections
|
| 106 |
if predefined_section == 'right compartments':
|
| 107 |
-
selected_indices = np.where(mesh.vertices[:, 0] > (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
|
| 108 |
-
elif predefined_section == 'left compartments':
|
| 109 |
-
selected_indices = np.where(mesh.vertices[:, 0] <= (train_bounds[0][0] + train_bounds[1][0]) / 2)[0]
|
| 110 |
-
elif predefined_section == 'freight_body':
|
| 111 |
-
selected_indices = np.where((mesh.vertices[:, 0] >= train_bounds[0][0]) & (mesh.vertices[:, 0] <= train_bounds[1][0]) &
|
| 112 |
-
(mesh.vertices[:, 2] <= (train_bounds[0][2] + train_bounds[1][2]) / 2))[0]
|
| 113 |
-
elif predefined_section == 'custom':
|
| 114 |
-
# Use custom sliders for custom section
|
| 115 |
-
selected_indices = np.where((mesh.vertices[:, 0] >= x_min) & (mesh.vertices[:, 0] <= x_max) &
|
| 116 |
-
(mesh.vertices[:, 1] >= y_min) & (mesh.vertices[:, 1] <= y_max) &
|
| 117 |
-
(mesh.vertices[:, 2] >= z_min) & (mesh.vertices[:, 2] <= z_max))[0]
|
| 118 |
-
else:
|
| 119 |
-
selected_indices = np.array([])
|
| 120 |
-
|
| 121 |
-
# Initialize UV mapping
|
| 122 |
-
uv = np.random.rand(len(mesh.vertices), 2)
|
| 123 |
-
new_uv = np.zeros_like(uv)
|
| 124 |
new_uv[selected_indices, :] = uv[selected_indices, :]
|
| 125 |
|
| 126 |
# Create material and apply the new texture
|
| 127 |
-
material = trimesh.visual.texture.SimpleMaterial(image=
|
| 128 |
-
color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=
|
| 129 |
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
|
| 130 |
|
| 131 |
# Save the mesh to a temporary file
|
| 132 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
|
| 133 |
-
textured_mesh.export(temp_file.name, file_type='glb')
|
| 134 |
-
temp_file.close()
|
| 135 |
-
return temp_file.name
|
| 136 |
-
|
| 137 |
-
# Load train model to establish bounding box ranges
|
| 138 |
-
train_model = trimesh.load('train.glb', force='mesh')
|
| 139 |
-
train_bounds = train_model.bounds
|
| 140 |
-
|
| 141 |
-
# Get slider ranges based on train model bounds
|
| 142 |
-
x_min_range, x_max_range = train_bounds[0][0], train_bounds[1][0]
|
| 143 |
y_min_range, y_max_range = train_bounds[0][1], train_bounds[1][1]
|
| 144 |
z_min_range, z_max_range = train_bounds[0][2], train_bounds[1][2]
|
| 145 |
|
| 146 |
-
# Gradio
|
| 147 |
with gr.Blocks() as app:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import trimesh
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
+
|
| 8 |
+
|
| 9 |
import tempfile
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
|
| 99 |
# Function to visualize texture based on selection criteria
|
| 100 |
+
def visualize_dynamic_texture(predefined_section, x_min, x_max, y_min, y_max, z_min, z_max):
|
| 101 |
# Load the original mesh
|
| 102 |
mesh = trimesh.load('train.glb', force='mesh')
|
| 103 |
+
rust_texture = Image.open('rust_steel.png').convert('RGB')
|
| 104 |
|
| 105 |
# Predefined sections
|
| 106 |
if predefined_section == 'right compartments':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
new_uv[selected_indices, :] = uv[selected_indices, :]
|
| 108 |
|
| 109 |
# Create material and apply the new texture
|
| 110 |
+
material = trimesh.visual.texture.SimpleMaterial(image=rust_texture)
|
| 111 |
+
color_visuals = trimesh.visual.TextureVisuals(uv=new_uv, image=rust_texture, material=material)
|
| 112 |
textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
|
| 113 |
|
| 114 |
# Save the mesh to a temporary file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
y_min_range, y_max_range = train_bounds[0][1], train_bounds[1][1]
|
| 116 |
z_min_range, z_max_range = train_bounds[0][2], train_bounds[1][2]
|
| 117 |
|
| 118 |
+
# Gradio UI with a single window, dynamic updates, and real-time changes
|
| 119 |
with gr.Blocks() as app:
|
| 120 |
+
gr.Markdown("### 3D Model Texture Application with Predefined and Custom Sections")
|
| 121 |
+
original_model = gr.Model3D('train.glb', label="Original Model")
|
| 122 |
+
modified_model = gr.Model3D(label="Model with Applied Texture")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# Dropdown for predefined and custom selection
|
| 190 |
+
section_dropdown = gr.Radio(choices=['right compartments', 'left compartments', 'freight_body', 'custom'], label="Select Section", value='custom')
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
# Custom sliders for the bounding box selection
|
| 197 |
+
with gr.Row(visible=True) as custom_controls:
|
| 198 |
+
x_min_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Min", value=x_min_range)
|
| 199 |
+
x_max_slider = gr.Slider(minimum=x_min_range, maximum=x_max_range, step=0.01, label="X Max", value=x_max_range)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
y_min_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Min", value=y_min_range)
|
| 206 |
+
y_max_slider = gr.Slider(minimum=y_min_range, maximum=y_max_range, step=0.01, label="Y Max", value=y_max_range)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
z_min_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Min", value=z_min_range)
|
| 215 |
+
z_max_slider = gr.Slider(minimum=z_min_range, maximum=z_max_range, step=0.01, label="Z Max", value=z_max_range)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
# Toggle visibility of custom controls
|
| 219 |
+
def toggle_custom_controls(predefined_section):
|
| 220 |
+
return gr.update(visible=(predefined_section == 'custom'))
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
section_dropdown.change(fn=toggle_custom_controls, inputs=section_dropdown, outputs=custom_controls)
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
# Update model dynamically
|
| 232 |
+
def update_model(predefined_section, x_min, x_max, y_min, y_max, z_min, z_max):
|
| 233 |
+
return visualize_dynamic_texture(predefined_section, x_min, x_max, y_min, y_max, z_min, z_max)
|
| 234 |
+
|
| 235 |
+
# Add event listeners for real-time updates when sliders or dropdown change
|
| 236 |
+
inputs = [section_dropdown, x_min_slider, x_max_slider, y_min_slider, y_max_slider, z_min_slider, z_max_slider]
|
| 237 |
+
for input_component in inputs:
|
| 238 |
+
input_component.change(fn=update_model, inputs=inputs, outputs=modified_model)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
app.launch()
|