Rahatara commited on
Commit
c2bccfe
·
verified ·
1 Parent(s): a2c5fcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -31
app.py CHANGED
@@ -1,39 +1,43 @@
1
  import gradio as gr
2
  import trimesh
 
3
  from PIL import Image
4
- import base64
5
  import io
6
 
7
- def modify_texture():
8
  # Load the GLB file
9
- scene = trimesh.load_mesh("train.glb")
10
- # Open the texture image
11
- im = Image.open("defect.jpg")
12
-
13
- # Create a material with the texture image
 
 
 
 
 
14
  material = trimesh.visual.texture.SimpleMaterial(image=im)
15
-
16
- # Get the uv coordinates from the mesh
17
- uv = scene.visual.uv
18
-
19
- # Create TextureVisuals with the uv coordinates and the texture image
20
  color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
21
-
22
- # Apply the visuals to the mesh
23
- mesh.visual = color_visuals
24
-
25
- # Export the mesh to a .glb file
26
- mesh.export(file_obj='modified_train.glb')
27
-
28
- # Convert the GLB file to base64 for Gradio Model3D output
29
- with open('modified_train.glb', 'rb') as f:
30
- glb_base64 = base64.b64encode(f.read()).decode()
31
-
32
- return glb_base64
33
-
34
- gr.Interface(
35
- fn=modify_texture,
36
- inputs=[],
37
- outputs=gr.Model3D(label="Download Modified GLB File"),
38
- title="GLB Texture Modifier"
39
- ).launch()
 
 
 
 
 
1
  import gradio as gr
2
  import trimesh
3
+ import numpy as np
4
  from PIL import Image
 
5
  import io
6
 
7
+ def visualize_texture():
8
  # Load the GLB file
9
+ mesh = trimesh.load('train.glb', file_type='glb', process=False)
10
+
11
+ # Load the texture image
12
+ im = Image.open('defect.jpg')
13
+ im = im.convert('RGB') # Ensure the image is in RGB format
14
+
15
+ # Create random UV coordinates for the mesh
16
+ uv = np.random.rand(len(mesh.vertices), 2)
17
+
18
+ # Create material and apply texture
19
  material = trimesh.visual.texture.SimpleMaterial(image=im)
 
 
 
 
 
20
  color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
21
+
22
+ # Apply the texture to the original mesh
23
+ textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=color_visuals, validate=True, process=False)
24
+
25
+ # Save the original and textured meshes to GLB format in memory
26
+ original_glb = io.BytesIO()
27
+ mesh.export(original_glb, file_type='glb')
28
+ original_glb.seek(0)
29
+
30
+ modified_glb = io.BytesIO()
31
+ textured_mesh.export(modified_glb, file_type='glb')
32
+ modified_glb.seek(0)
33
+
34
+ return original_glb, modified_glb
35
+
36
+ with gr.Blocks() as app:
37
+ gr.Markdown("### 3D Model Texture Application")
38
+ original_model = gr.Model3D(label="Original Model")
39
+ modified_model = gr.Model3D(label="Textured Model")
40
+ button = gr.Button("Visualize Texture")
41
+ button.click(visualize_texture, outputs=[original_model, modified_model])
42
+
43
+ app.launch()