Spaces:
Sleeping
Sleeping
open3d -> trimesh 대체
Browse files
utils.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
-
|
| 6 |
|
| 7 |
# 키프레임 추출 함수 (영상에서 특정 프레임을 추출)
|
| 8 |
def extract_keyframes(video_path, frame_interval=30):
|
|
@@ -31,55 +31,29 @@ def extract_keyframes(video_path, frame_interval=30):
|
|
| 31 |
cap.release()
|
| 32 |
return keyframes
|
| 33 |
|
| 34 |
-
#
|
| 35 |
def reconstruct_3d(keyframes):
|
| 36 |
"""
|
| 37 |
-
Reconstruct 3D point cloud from keyframes using simple feature matching.
|
| 38 |
|
| 39 |
:param keyframes: List of keyframes (as numpy arrays)
|
| 40 |
-
:return: 3D
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
# Assuming keyframes are pairs to compute sparse depth
|
| 49 |
-
for frame in keyframes:
|
| 50 |
-
# Convert the frame to grayscale
|
| 51 |
-
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 52 |
-
|
| 53 |
-
# Detect ORB features
|
| 54 |
-
orb = cv2.ORB_create()
|
| 55 |
-
keypoints, descriptors = orb.detectAndCompute(gray_frame, None)
|
| 56 |
-
|
| 57 |
-
# For simplicity, assume point cloud generation based on features (placeholder)
|
| 58 |
-
# This part would typically include feature matching and triangulation
|
| 59 |
-
points = np.random.rand(100, 3) # Placeholder for actual 3D reconstruction logic
|
| 60 |
-
|
| 61 |
-
# Add points to the point cloud
|
| 62 |
-
pcd.points = o3d.utility.Vector3dVector(points)
|
| 63 |
-
|
| 64 |
-
return pcd
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
def save_3d_to_webgl(
|
| 68 |
"""
|
| 69 |
-
Save a 3D
|
| 70 |
|
| 71 |
-
:param
|
| 72 |
:param output_path: Path to save the HTML file
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Save the point cloud as an HTML file
|
| 78 |
-
# WebGL export logic can be added here if required (simplified for now)
|
| 79 |
-
vis = o3d.visualization.Visualizer()
|
| 80 |
-
vis.create_window()
|
| 81 |
-
vis.add_geometry(pcd)
|
| 82 |
-
vis.capture_screen_image(output_path)
|
| 83 |
-
vis.destroy_window()
|
| 84 |
-
|
| 85 |
print(f"3D model saved to {output_path}")
|
|
|
|
| 1 |
+
import trimesh
|
| 2 |
import numpy as np
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
+
import cv2
|
| 6 |
|
| 7 |
# 키프레임 추출 함수 (영상에서 특정 프레임을 추출)
|
| 8 |
def extract_keyframes(video_path, frame_interval=30):
|
|
|
|
| 31 |
cap.release()
|
| 32 |
return keyframes
|
| 33 |
|
| 34 |
+
# 3D Reconstruction (using trimesh to create simple 3D model)
|
| 35 |
def reconstruct_3d(keyframes):
|
| 36 |
"""
|
| 37 |
+
Reconstruct 3D point cloud or mesh from keyframes using simple feature matching.
|
| 38 |
|
| 39 |
:param keyframes: List of keyframes (as numpy arrays)
|
| 40 |
+
:return: 3D model (trimesh object)
|
| 41 |
"""
|
| 42 |
+
# Example: Create a simple mesh using trimesh (placeholder logic)
|
| 43 |
+
# For simplicity, we generate a cube mesh. In practice, 3D reconstruction would use feature matching
|
| 44 |
+
# and triangulation for actual 3D modeling.
|
| 45 |
+
mesh = trimesh.creation.box(extents=[1, 1, 1])
|
| 46 |
|
| 47 |
+
return mesh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Save 3D mesh to WebGL HTML file
|
| 50 |
+
def save_3d_to_webgl(mesh, output_path="output_model.html"):
|
| 51 |
"""
|
| 52 |
+
Save a 3D mesh to a WebGL-compatible HTML file using trimesh's built-in method.
|
| 53 |
|
| 54 |
+
:param mesh: 3D mesh object (trimesh)
|
| 55 |
:param output_path: Path to save the HTML file
|
| 56 |
"""
|
| 57 |
+
# Export mesh as WebGL (HTML format)
|
| 58 |
+
mesh.export(output_path, file_type='html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
print(f"3D model saved to {output_path}")
|