dschandra commited on
Commit
7d02d9d
·
verified ·
1 Parent(s): afe1c50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
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
- # Save the uploaded file to a temporary location
13
- with open("uploaded_model.obj", "wb") as f:
14
- f.write(uploaded_file.getbuffer())
15
-
16
- # Load the mesh using Open3D
17
- mesh = o3d.io.read_triangle_mesh("uploaded_model.obj")
18
- mesh.compute_vertex_normals()
19
-
20
- # Convert the mesh to a visualizable format for Streamlit
21
- vertices = mesh.vertices
22
- triangles = mesh.triangles
23
- vertex_colors = mesh.vertex_colors
24
-
25
- st.write("### 3D Model Preview")
26
- st.write("Mesh Vertices: ", len(vertices))
27
- st.write("Mesh Triangles: ", len(triangles))
28
-
29
- # Display the 3D mesh using Streamlit's 3D visualization support
30
- st.write("You can download Open3D and use it to visualize the mesh locally.")
31
-
32
- # Optionally, display the mesh information
33
- st.write(mesh)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")