Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
import open3d as o3d
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Streamlit title and description
|
| 5 |
st.title("Jewelry 3D Model Viewer")
|
|
@@ -9,28 +11,44 @@ st.write("Upload an OBJ file to visualize the 3D model of jewelry.")
|
|
| 9 |
uploaded_file = st.file_uploader("Choose an OBJ file", type="obj")
|
| 10 |
|
| 11 |
if uploaded_file is not None:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
else:
|
| 36 |
st.write("Please upload an OBJ file to visualize the jewelry model.")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import trimesh
|
| 3 |
+
from pythreejs import *
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
# Streamlit title and description
|
| 7 |
st.title("Jewelry 3D Model Viewer")
|
|
|
|
| 11 |
uploaded_file = st.file_uploader("Choose an OBJ file", type="obj")
|
| 12 |
|
| 13 |
if uploaded_file is not None:
|
| 14 |
+
try:
|
| 15 |
+
# Load the mesh using trimesh
|
| 16 |
+
mesh = trimesh.load(uploaded_file, force='mesh')
|
| 17 |
+
|
| 18 |
+
# Check if mesh is valid
|
| 19 |
+
if mesh.is_empty:
|
| 20 |
+
st.error("The OBJ file does not contain valid geometry. Please upload a different file.")
|
| 21 |
+
else:
|
| 22 |
+
# Get vertices and faces from the mesh
|
| 23 |
+
vertices = mesh.vertices
|
| 24 |
+
faces = mesh.faces
|
| 25 |
+
|
| 26 |
+
# Create geometry and material for the 3D model
|
| 27 |
+
geometry = BufferGeometry(
|
| 28 |
+
attributes={
|
| 29 |
+
'position': BufferAttribute(vertices, normalized=False),
|
| 30 |
+
'index': BufferAttribute(faces.flatten(), normalized=False)
|
| 31 |
+
}
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
material = MeshStandardMaterial(color='gold', roughness=0.5, metalness=0.8)
|
| 35 |
+
model = Mesh(geometry, material)
|
| 36 |
+
|
| 37 |
+
# Setup the scene
|
| 38 |
+
camera = PerspectiveCamera(position=[3, 3, 3], fov=75)
|
| 39 |
+
scene = Scene(children=[model, camera, AmbientLight(color='#777777')])
|
| 40 |
+
renderer = Renderer(camera=camera, scene=scene, controls=[OrbitControls(controlling=camera)],
|
| 41 |
+
width=800, height=600)
|
| 42 |
+
|
| 43 |
+
st.write("### 3D Model Preview")
|
| 44 |
+
st.write(renderer)
|
| 45 |
+
|
| 46 |
+
# Print success message
|
| 47 |
+
st.success("The OBJ file was successfully loaded and visualized.")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
# Print error message
|
| 51 |
+
st.error(f"An error occurred while loading the OBJ file: {str(e)}")
|
| 52 |
|
| 53 |
else:
|
| 54 |
st.write("Please upload an OBJ file to visualize the jewelry model.")
|