Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,83 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import trimesh
|
|
|
|
|
|
|
| 3 |
import io
|
| 4 |
import base64
|
| 5 |
from PIL import Image
|
| 6 |
-
from matplotlib import pyplot as plt
|
| 7 |
|
| 8 |
def generate_cad_model(base_length, base_width, base_height):
|
| 9 |
"""
|
| 10 |
-
Generate a CAD model as a 3D mesh
|
| 11 |
"""
|
| 12 |
# Create a 3D box (CAD model representation)
|
| 13 |
mesh = trimesh.creation.box(extents=(base_length, base_width, base_height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
projection = mesh.section(plane_origin=(0, 0, 0), plane_normal=(0, 0, 1))
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Gradio Interface
|
| 33 |
-
def
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
demo = gr.Interface(
|
| 39 |
-
fn=
|
| 40 |
inputs=[
|
| 41 |
gr.Number(label="Base Length (mm)", value=10),
|
| 42 |
gr.Number(label="Base Width (mm)", value=10),
|
| 43 |
gr.Number(label="Base Height (mm)", value=5),
|
| 44 |
],
|
| 45 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 46 |
live=True,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import trimesh
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
def generate_cad_model(base_length, base_width, base_height):
|
| 9 |
"""
|
| 10 |
+
Generate a CAD model as a 3D mesh.
|
| 11 |
"""
|
| 12 |
# Create a 3D box (CAD model representation)
|
| 13 |
mesh = trimesh.creation.box(extents=(base_length, base_width, base_height))
|
| 14 |
+
return mesh
|
| 15 |
+
|
| 16 |
+
def render_3d_cad_model(base_length, base_width, base_height):
|
| 17 |
+
"""
|
| 18 |
+
Render a 3D CAD model for interactive viewing.
|
| 19 |
+
"""
|
| 20 |
+
# Generate the 3D CAD model
|
| 21 |
+
mesh = generate_cad_model(base_length, base_width, base_height)
|
| 22 |
|
| 23 |
+
# Convert the mesh to GLB format for 3D visualization
|
| 24 |
+
glb_data = io.BytesIO()
|
| 25 |
+
mesh.export(glb_data, file_type="glb")
|
| 26 |
+
glb_data.seek(0)
|
| 27 |
+
return glb_data
|
| 28 |
+
|
| 29 |
+
def render_2d_cad_projection(base_length, base_width, base_height):
|
| 30 |
+
"""
|
| 31 |
+
Generate a 2D projection of the CAD model and return it as an image.
|
| 32 |
+
"""
|
| 33 |
+
# Generate the 3D CAD model
|
| 34 |
+
mesh = generate_cad_model(base_length, base_width, base_height)
|
| 35 |
|
| 36 |
+
# Create a 2D projection of the model
|
| 37 |
projection = mesh.section(plane_origin=(0, 0, 0), plane_normal=(0, 0, 1))
|
| 38 |
+
if projection is not None:
|
| 39 |
+
projection_img = trimesh.util.grid_2d(projection.to_image(), tile=(1, 1))
|
| 40 |
+
|
| 41 |
+
# Save the projection as an image
|
| 42 |
+
buffer = io.BytesIO()
|
| 43 |
+
plt.imshow(projection_img, cmap="gray")
|
| 44 |
+
plt.axis("off")
|
| 45 |
+
plt.savefig(buffer, format="png", bbox_inches="tight")
|
| 46 |
+
buffer.seek(0)
|
| 47 |
+
|
| 48 |
+
# Convert to Base64 for Gradio display
|
| 49 |
+
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 50 |
+
return f"data:image/png;base64,{encoded_image}"
|
| 51 |
+
else:
|
| 52 |
+
return None
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
+
def display_cad(base_length, base_width, base_height):
|
| 56 |
+
"""
|
| 57 |
+
Render both 3D and 2D CAD outputs.
|
| 58 |
+
"""
|
| 59 |
+
# Render 3D model
|
| 60 |
+
glb_data = render_3d_cad_model(base_length, base_width, base_height)
|
| 61 |
+
|
| 62 |
+
# Render 2D projection
|
| 63 |
+
cad_image = render_2d_cad_projection(base_length, base_width, base_height)
|
| 64 |
+
|
| 65 |
+
return glb_data, cad_image
|
| 66 |
|
| 67 |
+
# Gradio interface for 3D and 2D CAD visualization
|
| 68 |
demo = gr.Interface(
|
| 69 |
+
fn=display_cad,
|
| 70 |
inputs=[
|
| 71 |
gr.Number(label="Base Length (mm)", value=10),
|
| 72 |
gr.Number(label="Base Width (mm)", value=10),
|
| 73 |
gr.Number(label="Base Height (mm)", value=5),
|
| 74 |
],
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.Model3D(label="3D CAD Model"), # Interactive 3D viewer
|
| 77 |
+
gr.Image(label="2D CAD Projection"), # Static 2D projection
|
| 78 |
+
],
|
| 79 |
live=True,
|
| 80 |
)
|
| 81 |
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|