Asrar990 commited on
Commit
84ff87e
Β·
verified Β·
1 Parent(s): 1064f31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import open3d as o3d
4
+ import numpy as np
5
+ import tempfile
6
+ import os
7
+
8
+ # Title of the App
9
+ st.title("3D Reconstruction Tool from Video πŸ“Ή β†’ πŸ› οΈ β†’ 🧊")
10
+
11
+ # Sidebar: Information
12
+ st.sidebar.write("""
13
+ ## About the App
14
+ Upload a video file, extract frames, reconstruct a 3D point cloud using Structure from Motion (SfM), and visualize or download the 3D mesh.
15
+ """)
16
+
17
+ # Step 1: Upload Video File
18
+ uploaded_file = st.file_uploader("Upload a Video File (MP4, AVI)", type=["mp4", "avi"])
19
+
20
+ # Function to extract frames from video
21
+ def extract_frames(video_path, frame_rate=10):
22
+ cap = cv2.VideoCapture(video_path)
23
+ frames = []
24
+ count = 0
25
+ while cap.isOpened():
26
+ ret, frame = cap.read()
27
+ if not ret:
28
+ break
29
+ if count % frame_rate == 0:
30
+ frames.append(frame)
31
+ count += 1
32
+ cap.release()
33
+ return frames
34
+
35
+ # Function to save frames as images
36
+ def save_frames_as_images(frames, output_dir):
37
+ os.makedirs(output_dir, exist_ok=True)
38
+ for i, frame in enumerate(frames):
39
+ filename = os.path.join(output_dir, f"frame_{i:04d}.png")
40
+ cv2.imwrite(filename, frame)
41
+
42
+ # Step 2: Process Uploaded Video
43
+ if uploaded_file:
44
+ st.video(uploaded_file)
45
+ st.write("Extracting frames...")
46
+
47
+ # Save the uploaded video temporarily
48
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
49
+ tmp_video.write(uploaded_file.read())
50
+ video_path = tmp_video.name
51
+
52
+ # Extract frames
53
+ frames = extract_frames(video_path, frame_rate=10)
54
+ st.write(f"βœ… Extracted {len(frames)} frames from the video.")
55
+
56
+ # Save extracted frames
57
+ frames_dir = tempfile.mkdtemp()
58
+ save_frames_as_images(frames, frames_dir)
59
+ st.write(f"Frames saved temporarily at `{frames_dir}`.")
60
+
61
+ # Step 3: Structure from Motion (3D Reconstruction)
62
+ st.write("πŸ”„ Reconstructing 3D point cloud using Structure from Motion...")
63
+
64
+ # Create Open3D Point Cloud
65
+ pcd = o3d.geometry.PointCloud()
66
+ for image_file in sorted(os.listdir(frames_dir)):
67
+ img_path = os.path.join(frames_dir, image_file)
68
+ frame = cv2.imread(img_path)
69
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
70
+
71
+ # Dummy point cloud generation for simplicity
72
+ height, width = gray.shape
73
+ x, y = np.meshgrid(np.arange(width), np.arange(height))
74
+ z = gray / 255.0 # Use gray intensity as a pseudo depth
75
+ points = np.stack((x.flatten(), y.flatten(), z.flatten()), axis=1)
76
+ pcd.points.extend(o3d.utility.Vector3dVector(points))
77
+
78
+ # Step 4: Surface Reconstruction
79
+ st.write("πŸ› οΈ Generating mesh using Poisson Reconstruction...")
80
+ pcd.estimate_normals()
81
+ mesh, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=8)
82
+
83
+ # Step 5: Visualization
84
+ st.write("βœ… Reconstruction Complete! Visualizing the 3D model:")
85
+ o3d.io.write_triangle_mesh("reconstructed_mesh.stl", mesh)
86
+
87
+ # Use Plotly for visualization
88
+ import plotly.graph_objects as go
89
+ vertices = np.asarray(mesh.vertices)
90
+ triangles = np.asarray(mesh.triangles)
91
+ fig = go.Figure(data=[go.Mesh3d(
92
+ x=vertices[:, 0],
93
+ y=vertices[:, 1],
94
+ z=vertices[:, 2],
95
+ i=triangles[:, 0],
96
+ j=triangles[:, 1],
97
+ k=triangles[:, 2],
98
+ color='lightblue',
99
+ opacity=0.50
100
+ )])
101
+ st.plotly_chart(fig)
102
+
103
+ # Step 6: Download the Optimized Mesh
104
+ st.write("πŸ“₯ Download the reconstructed 3D model:")
105
+ with open("reconstructed_mesh.stl", "rb") as f:
106
+ st.download_button("Download 3D Mesh (STL)", f, file_name="reconstructed_mesh.stl")