shin0624 commited on
Commit
eb7db0b
ยท
verified ยท
1 Parent(s): 01c1f02

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +85 -0
utils.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
9
+ """
10
+ Extract keyframes from a video at a given interval.
11
+
12
+ :param video_path: Path to the input video file
13
+ :param frame_interval: Interval (in frames) between keyframes
14
+ :return: List of keyframes (as numpy arrays)
15
+ """
16
+ # Open the video file
17
+ cap = cv2.VideoCapture(video_path)
18
+
19
+ # Get the total number of frames
20
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
21
+
22
+ keyframes = []
23
+
24
+ # Extract frames at the given interval
25
+ for i in range(0, total_frames, frame_interval):
26
+ cap.set(cv2.CAP_PROP_POS_FRAMES, i)
27
+ ret, frame = cap.read()
28
+ if ret:
29
+ keyframes.append(frame)
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}")