Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- utils/depth_utils.py +25 -0
- utils/pointcloud_utils.py +25 -0
- utils/video_utils.py +24 -0
utils/depth_utils.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 5 |
+
|
| 6 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 7 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 8 |
+
model.eval()
|
| 9 |
+
|
| 10 |
+
def estimate_depth(frame_paths):
|
| 11 |
+
depth_maps = []
|
| 12 |
+
|
| 13 |
+
for path in frame_paths:
|
| 14 |
+
image = cv2.imread(path)
|
| 15 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 16 |
+
|
| 17 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
depth = outputs.predicted_depth[0].cpu().numpy()
|
| 21 |
+
|
| 22 |
+
depth = cv2.resize(depth, (image.shape[1], image.shape[0]))
|
| 23 |
+
depth_maps.append(depth)
|
| 24 |
+
|
| 25 |
+
return depth_maps
|
utils/pointcloud_utils.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import open3d as o3d
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def depth_to_pointcloud(depth_maps, frame_paths, out_dir):
|
| 7 |
+
points = []
|
| 8 |
+
|
| 9 |
+
for depth, frame_path in zip(depth_maps, frame_paths):
|
| 10 |
+
img = cv2.imread(frame_path)
|
| 11 |
+
h, w = depth.shape
|
| 12 |
+
|
| 13 |
+
for y in range(0, h, 10):
|
| 14 |
+
for x in range(0, w, 10):
|
| 15 |
+
z = depth[y, x]
|
| 16 |
+
points.append([x, y, z])
|
| 17 |
+
|
| 18 |
+
points = np.array(points)
|
| 19 |
+
pcd = o3d.geometry.PointCloud()
|
| 20 |
+
pcd.points = o3d.utility.Vector3dVector(points)
|
| 21 |
+
|
| 22 |
+
out_path = os.path.join(out_dir, "model.ply")
|
| 23 |
+
o3d.io.write_point_cloud(out_path, pcd)
|
| 24 |
+
|
| 25 |
+
return out_path
|
utils/video_utils.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def extract_frames(video_path, out_dir, every_n=15):
|
| 5 |
+
cap = cv2.VideoCapture(video_path)
|
| 6 |
+
frames = []
|
| 7 |
+
count = 0
|
| 8 |
+
idx = 0
|
| 9 |
+
|
| 10 |
+
while cap.isOpened():
|
| 11 |
+
ret, frame = cap.read()
|
| 12 |
+
if not ret:
|
| 13 |
+
break
|
| 14 |
+
|
| 15 |
+
if count % every_n == 0:
|
| 16 |
+
frame_path = os.path.join(out_dir, f"frame_{idx}.jpg")
|
| 17 |
+
cv2.imwrite(frame_path, frame)
|
| 18 |
+
frames.append(frame_path)
|
| 19 |
+
idx += 1
|
| 20 |
+
|
| 21 |
+
count += 1
|
| 22 |
+
|
| 23 |
+
cap.release()
|
| 24 |
+
return frames
|