Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
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}")
|