Spaces:
Runtime error
Runtime error
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files3 unmodified lines456789101112131415163 unmodified lines20212223242526272829303132333435363738394041424344454647483 unmodified linesimport matplotlib.pyplot as pltimport tempfileimport os# Page configurationst.set_page_config(page_title="AR CAD Viewer", layout="wide")# Titlest.title("Augmented Reality CAD Viewer")st.write("Upload your CAD models and view them in a 3D interactive environment.")# File uploader for CAD filesuploaded_file = st.file_uploader("Upload a CAD file (.stl format)", type=["stl"])3 unmodified lineswith tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:temp_file.write(uploaded_file.read())temp_filepath = temp_file.name# Load the STL filestl_mesh = mesh.Mesh.from_file(temp_filepath)# Create a new figure for plottingfig = plt.figure(figsize=(10, 10))ax = fig.add_subplot(111, projection="3d")# Plot the CAD modelax.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors, alpha=0.7, color="cyan"))ax.auto_scale_xyz(stl_mesh.points.flatten(), stl_mesh.points.flatten(), stl_mesh.points.flatten())# Display the 3D plot in Streamlitst.pyplot(fig)# Clean up the temporary fileos.remove(temp_filepath)# AR.js linkst.markdown("""## AR FeatureUse [AR.js](https://ar-js-org.github.io/AR.js/) to host your model online for AR visualization.Upload this model to a compatible AR hosting service or modify the file to be AR.js-ready.""")3 unmodified lines4567891011121314151617183 unmodified lines2223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071723 unmodified linesimport matplotlib.pyplot as pltimport tempfileimport osimport trimeshimport pyvista as pv# Page configurationst.set_page_config(page_title="AR CAD Viewer", layout="wide")# Titlest.title("Augmented Reality CAD Viewer")st.write("Upload your CAD models, preview them, and convert them to AR-compatible formats.")# File uploader for CAD filesuploaded_file = st.file_uploader("Upload a CAD file (.stl format)", type=["stl"])3 unmodified lineswith tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:temp_file.write(uploaded_file.read())temp_filepath = temp_file.name# Load the STL filestl_mesh = mesh.Mesh.from_file(temp_filepath)# Preview the model in 3Dst.subheader("3D Model Preview")fig = plt.figure(figsize=(10, 10))ax = fig.add_subplot(111, projection="3d")ax.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors, alpha=0.7, color="cyan"))ax.auto_scale_xyz(stl_mesh.points.flatten(), stl_mesh.points.flatten(), stl_mesh.points.flatten())st.pyplot(fig)# Convert to glTF for ARst.subheader("Convert to AR Format")st.write("Click below to convert the STL file into an AR-compatible `.glTF` format.")if st.button("Convert to glTF"):# Use Trimesh for conversiontrimesh_mesh = trimesh.load(temp_filepath)gltf_filepath = temp_filepath.replace(".stl", ".gltf")trimesh.exchange.gltf.export_gltf(trimesh_mesh, gltf_filepath)# Display download link for glTFwith open(gltf_filepath, "rb") as gltf_file:st.download_button(label="Download glTF Model",data=gltf_file,file_name="model.gltf",mime="model/gltf+json")# Clean up temporary filesos.remove(gltf_filepath)os.remove(temp_filepath)# AR Hosting Infost.subheader("Host in AR")st.markdown("""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).For AR.js:- Host the `.glTF` file on a web server.- Create an AR marker or use markerless AR for viewing.Example: `<a-scene><a-entity gltf-model="model.gltf"></a-entity></a-scene>`""")
app.py+44-20