Spaces:
Runtime error
Runtime error
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,15 @@ from mpl_toolkits import mplot3d
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import tempfile
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Page configuration
|
| 9 |
st.set_page_config(page_title="AR CAD Viewer", layout="wide")
|
| 10 |
|
| 11 |
# Title
|
| 12 |
st.title("Augmented Reality CAD Viewer")
|
| 13 |
-
st.write("Upload your CAD models and
|
| 14 |
|
| 15 |
# File uploader for CAD files
|
| 16 |
uploaded_file = st.file_uploader("Upload a CAD file (.stl format)", type=["stl"])
|
|
@@ -20,29 +22,51 @@ if uploaded_file is not None:
|
|
| 20 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:
|
| 21 |
temp_file.write(uploaded_file.read())
|
| 22 |
temp_filepath = temp_file.name
|
| 23 |
-
|
| 24 |
# Load the STL file
|
| 25 |
stl_mesh = mesh.Mesh.from_file(temp_filepath)
|
| 26 |
-
|
| 27 |
-
#
|
|
|
|
| 28 |
fig = plt.figure(figsize=(10, 10))
|
| 29 |
ax = fig.add_subplot(111, projection="3d")
|
| 30 |
-
|
| 31 |
-
# Plot the CAD model
|
| 32 |
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors, alpha=0.7, color="cyan"))
|
| 33 |
ax.auto_scale_xyz(stl_mesh.points.flatten(), stl_mesh.points.flatten(), stl_mesh.points.flatten())
|
| 34 |
-
|
| 35 |
-
# Display the 3D plot in Streamlit
|
| 36 |
st.pyplot(fig)
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
st.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
import trimesh
|
| 8 |
+
import pyvista as pv
|
| 9 |
|
| 10 |
# Page configuration
|
| 11 |
st.set_page_config(page_title="AR CAD Viewer", layout="wide")
|
| 12 |
|
| 13 |
# Title
|
| 14 |
st.title("Augmented Reality CAD Viewer")
|
| 15 |
+
st.write("Upload your CAD models, preview them, and convert them to AR-compatible formats.")
|
| 16 |
|
| 17 |
# File uploader for CAD files
|
| 18 |
uploaded_file = st.file_uploader("Upload a CAD file (.stl format)", type=["stl"])
|
|
|
|
| 22 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:
|
| 23 |
temp_file.write(uploaded_file.read())
|
| 24 |
temp_filepath = temp_file.name
|
| 25 |
+
|
| 26 |
# Load the STL file
|
| 27 |
stl_mesh = mesh.Mesh.from_file(temp_filepath)
|
| 28 |
+
|
| 29 |
+
# Preview the model in 3D
|
| 30 |
+
st.subheader("3D Model Preview")
|
| 31 |
fig = plt.figure(figsize=(10, 10))
|
| 32 |
ax = fig.add_subplot(111, projection="3d")
|
|
|
|
|
|
|
| 33 |
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors, alpha=0.7, color="cyan"))
|
| 34 |
ax.auto_scale_xyz(stl_mesh.points.flatten(), stl_mesh.points.flatten(), stl_mesh.points.flatten())
|
|
|
|
|
|
|
| 35 |
st.pyplot(fig)
|
| 36 |
+
|
| 37 |
+
# Convert to glTF for AR
|
| 38 |
+
st.subheader("Convert to AR Format")
|
| 39 |
+
st.write("Click below to convert the STL file into an AR-compatible `.glTF` format.")
|
| 40 |
+
|
| 41 |
+
if st.button("Convert to glTF"):
|
| 42 |
+
# Use Trimesh for conversion
|
| 43 |
+
trimesh_mesh = trimesh.load(temp_filepath)
|
| 44 |
+
gltf_filepath = temp_filepath.replace(".stl", ".gltf")
|
| 45 |
+
trimesh.exchange.gltf.export_gltf(trimesh_mesh, gltf_filepath)
|
| 46 |
+
|
| 47 |
+
# Display download link for glTF
|
| 48 |
+
with open(gltf_filepath, "rb") as gltf_file:
|
| 49 |
+
st.download_button(
|
| 50 |
+
label="Download glTF Model",
|
| 51 |
+
data=gltf_file,
|
| 52 |
+
file_name="model.gltf",
|
| 53 |
+
mime="model/gltf+json"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Clean up temporary files
|
| 57 |
+
os.remove(gltf_filepath)
|
| 58 |
+
os.remove(temp_filepath)
|
| 59 |
+
|
| 60 |
+
# AR Hosting Info
|
| 61 |
+
st.subheader("Host in AR")
|
| 62 |
+
st.markdown(
|
| 63 |
+
"""
|
| 64 |
+
Upload your `.glTF` file to [GitHub Pages](https://pages.github.com/) or use a service like [Google Poly](https://poly.google.com/) (now integrated into other platforms).
|
| 65 |
+
|
| 66 |
+
For AR.js:
|
| 67 |
+
- Host the `.glTF` file on a web server.
|
| 68 |
+
- Create an AR marker or use markerless AR for viewing.
|
| 69 |
+
|
| 70 |
+
Example: `<a-scene><a-entity gltf-model="model.gltf"></a-entity></a-scene>`
|
| 71 |
+
"""
|
| 72 |
+
)
|