shin0624 commited on
Commit
d5ac586
·
verified ·
1 Parent(s): eb7db0b

open3d -> trimesh 대체

Browse files
Files changed (1) hide show
  1. utils.py +16 -42
utils.py CHANGED
@@ -1,8 +1,8 @@
1
- import cv2
2
  import numpy as np
3
  import os
4
  import tempfile
5
- from pathlib import Path
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
- # Open3D 기반의 3D 재구성 (간단한 포인트 클라우드 생성)
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 point cloud (as Open3D point cloud object)
41
  """
42
- import open3d as o3d
43
-
44
- # Initialize Open3D point cloud
45
- pcd = o3d.geometry.PointCloud()
46
 
47
- # Example for feature extraction and matching between keyframes
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
- # WebGL 용 3D 모델 저장 (간단한 포인트 클라우드 예시)
67
- def save_3d_to_webgl(pcd, output_path="output_model.html"):
68
  """
69
- Save a 3D point cloud to a WebGL-compatible HTML file.
70
 
71
- :param pcd: 3D point cloud (as Open3D point cloud object)
72
  :param output_path: Path to save the HTML file
73
  """
74
- import open3d as o3d
75
- o3d.visualization.draw_geometries([pcd], window_name="3D Reconstruction")
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}")