Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
8 |
-
# Load the original mesh
|
9 |
mesh = trimesh.load('train.glb', force='mesh')
|
10 |
-
|
11 |
|
12 |
-
#
|
13 |
bounds = mesh.bounds
|
14 |
min_bounds, max_bounds = bounds[0], bounds[1]
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
# Create
|
34 |
uv = np.random.rand(len(mesh.vertices), 2)
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
#
|
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("###
|
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=['
|
59 |
-
button = gr.Button("
|
60 |
-
button.click(
|
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()
|