i will gave you two point cloud generator code. first one is for metric3d second one is zoe. i want to implement methods of metric3d code to zoe. the main difference is just usage of different model. give me the whole code
import cv2
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
import open3d as o3d
# Load camera parameters from NPZ file
if calibration_path:
self.load_camera_parameters(calibration_path)
else:
self.set_default_camera_parameters()
# Rotation matrix (keeping this as it was in the original code)
self.R = np.eye(3)
if transformation is None:
self.transformation = np.eye(4)
else:
self.transformation = transformation
self.voxel_size = voxel_size
def set_default_camera_parameters(self):
self.FX = 350.0 # Default focal length
self.FY = 350.0
self.CX = 160.0 # Default principal point
self.CY = 120.0
self.dist_coeffs = np.zeros(5)
print("Using default camera parameters")
def load_camera_parameters(self, calibration_path):
try:
calibration_data = np.load(calibration_path)
if 'Camera_matrix' in calibration_data:
camera_matrix = calibration_data['Camera_matrix']
self.FX = camera_matrix[0, 0]
self.FY = camera_matrix[1, 1]
self.CX = camera_matrix[0, 2]
self.CY = camera_matrix[1, 2]
print(f"Camera parameters loaded: FX: {self.FX}, FY: {self.FY}, CX: {self.CX}, CY: {self.CY}")
else:
raise KeyError("Camera matrix not found in calibration file")
if 'distCoeff' in calibration_data:
self.dist_coeffs = calibration_data['distCoeff'].ravel()
print(f"Distortion coefficients loaded: {self.dist_coeffs}")
else:
print("Distortion coefficients not found, assuming no distortion")
self.dist_coeffs = np.zeros(5)
except Exception as e:
print(f"Error loading camera parameters: {e}")
self.set_default_camera_parameters()
def process_frame_with_depth(self, frame):
# Undistort the frame
h, w = frame.shape[:2]
new_camera_matrix, _ = cv2.getOptimalNewCameraMatrix(
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs, (w, h), 1, (w, h)
)
undistorted_frame = cv2.undistort(frame,
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs,
None,
new_camera_matrix)
# RGB to Depth
color_image = cv2.cvtColor(undistorted_frame, cv2.COLOR_BGR2RGB)
image_tensor = transforms.ToTensor()(color_image).unsqueeze(0).to(self.device)
with torch.no_grad():
pred_depth, confidence, _ = self.metric3d_model.inference({'input': image_tensor})
depth_map = pred_depth.squeeze().cpu().numpy()
# Ensure depth_map matches the original frame size
depth_map = cv2.resize(depth_map, (w, h), interpolation=cv2.INTER_LINEAR)
# Scale depth map to real-world units (e.g., meters)
depth_scale = 1.0 # Adjust this value based on your depth model's output scale
scaled_depth_map = depth_map * depth_scale
# Create 3D points
rows, cols = scaled_depth_map.shape
c, r = np.meshgrid(np.arange(cols), np.arange(rows))
points = np.zeros((rows * cols, 3), dtype=np.float32)
points[:, 0] = (c.flatten() - self.CX) * scaled_depth_map.flatten() / self.FX
points[:, 1] = (r.flatten() - self.CY) * scaled_depth_map.flatten() / self.FY
points[:, 2] = scaled_depth_map.flatten()
# Improved filtering
min_depth = 2.0 # Adjust based on your scene
max_depth = 10.0 # Adjust based on your scene
min_brightness = 10 # Adjust to filter out very dark points
mask = (scaled_depth_map.flatten() > min_depth) & \
(scaled_depth_map.flatten() < max_depth) & \
(np.mean(color_image.reshape(-1, 3), axis=1) > min_brightness)
valid_points = points[mask]
valid_colors = color_image.reshape(-1, 3)[mask]
# Apply transformations
transformed_points = np.dot(valid_points, self.R.T)
homogeneous_points = np.hstack((valid_points, np.ones((valid_points.shape[0], 1))))
transformed_points = np.dot(homogeneous_points, self.transformation.T)[:, :3]
# Downsample
if self.voxel_size > 0:
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(transformed_points)
pcd.colors = o3d.utility.Vector3dVector(valid_colors.astype(np.float64) / 255.0)
downsampled_pcd = pcd.voxel_down_sample(voxel_size=self.voxel_size)
transformed_points = np.asarray(downsampled_pcd.points)
valid_colors = np.asarray(downsampled_pcd.colors) * 255
# Combine points and colors for final point cloud
point_cloud = np.hstack((transformed_points, valid_colors.astype(np.uint8)))
return point_cloud, depth_map
def save_point_cloud_txt(self, point_cloud, filename):
np.savetxt(filename, point_cloud, fmt='%.6f %.6f %.6f %d %d %d')
def save_point_cloud_pcd(point_cloud, filename):
with open(filename, 'w') as f:
f.write("# .PCD v0.7 - Point Cloud Data file format\n")
f.write("VERSION 0.7\n")
f.write("FIELDS x y z rgb\n")
f.write("SIZE 4 4 4 4\n")
f.write("TYPE F F F U\n")
f.write("COUNT 1 1 1 1\n")
f.write(f"WIDTH {len(point_cloud)}\n")
f.write("HEIGHT 1\n")
f.write("VIEWPOINT 0 0 0 1 0 0 0 0\n")
f.write(f"POINTS {len(point_cloud)}\n")
f.write("DATA ascii\n")
for point in point_cloud:
x, y, z, r, g, b = point
rgb = int(r) << 16 | int(g) << 8 | int(b)
f.write(f"{x} {y} {z} {rgb}\n")
import cv2
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
from zoe.zoedepth.models.builder import build_model
from zoe.zoedepth.utils.config import get_config
class PointCloudGenerator:
def init(self, model_name='zoedepth', calibration_path='samsung_cam_cal.npz', transformation=None, voxel_size=0.02):
# ZoeDepth setup
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pretrained_resource = 'local::zoe/checkpoints/depth_anything_metric_depth_indoor.pt'
config = get_config(model_name, "eval", 'nyu') # or 'kitti' for outdoor
config.pretrained_resource = pretrained_resource
self.zoedepth_model = build_model(config).to(self.device)
self.zoedepth_model.eval()
code
# Load camera parameters from NPZ file
if calibration_path:
self.load_camera_parameters(calibration_path)
else:
self.set_default_camera_parameters()
# Compute Q matrix
self.compute_Q_matrix()
# Rotation matrix
self.R = np.array([
[0, 0, -1],
[0, 1, 0],
[1, 0, 0]
])
# Transformation matrix
if transformation is None:
self.transformation = np.array([
[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, 1]
])
else:
self.transformation = transformation
self.voxel_size = voxel_size
def set_default_camera_parameters(self):
self.FX = 350.0 # Default focal length
self.FY = 350.0
self.CX = 160.0 # Default principal point
self.CY = 120.0
print("Using default camera parameters")
def load_camera_parameters(self, calibration_path):
try:
calibration_data = np.load(calibration_path)
print(f"Available keys in calibration file: {calibration_data.files}")
if 'mtx' in calibration_data:
camera_matrix = calibration_data['mtx']
self.FX = camera_matrix[0, 0]
self.FY = camera_matrix[1, 1]
self.CX = camera_matrix[0, 2]
self.CY = camera_matrix[1, 2]
print(f"Camera parameters loaded from {calibration_path}")
print(f"FX: {self.FX}, FY: {self.FY}, CX: {self.CX}, CY: {self.CY}")
else:
print(f"Camera matrix not found in {calibration_path}")
self.set_default_camera_parameters()
if 'dist' in calibration_data:
self.dist_coeffs = calibration_data['dist'].ravel()
print(f"Distortion coefficients loaded: {self.dist_coeffs}")
else:
print("Distortion coefficients not found, assuming no distortion")
self.dist_coeffs = np.zeros(5)
except Exception as e:
print(f"Error loading camera parameters: {e}")
self.set_default_camera_parameters()
def compute_Q_matrix(self):
self.Q = np.array([[1.0, 0.0, 0.0, -self.CX],
[0.0, 1.0, 0.0, -self.CY],
[0.0, 0.0, 0.0, self.FX],
[0.0, 0.0, 1.0/self.FY, 0.0]], dtype=np.float32)
def process_frame_with_depth(self, frame):
"""
Processes a frame and returns both the point cloud and the depth map.
"""
# Undistort the frame if distortion coefficients are available
if hasattr(self, 'dist_coeffs'):
h, w = frame.shape[:2]
new_camera_matrix, _ = cv2.getOptimalNewCameraMatrix(
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs, (w, h), 1, (w, h)
)
frame = cv2.undistort(frame,
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs,
None,
new_camera_matrix)
# RGB to Depth
color_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
original_height, original_width = color_image.shape[:2]
print(f"Original image shape: {color_image.shape}")
# Convert to PIL Image for ZoeDepth
pil_image = Image.fromarray(color_image)
image_tensor = transforms.ToTensor()(pil_image).unsqueeze(0).to(self.device)
with torch.no_grad():
pred = self.zoedepth_model(image_tensor, dataset='nyu') # or 'kitti' for outdoor
if isinstance(pred, dict):
pred = pred.get('metric_depth', pred.get('out'))
elif isinstance(pred, (list, tuple)):
pred = pred[-1]
depth_map = pred.squeeze().cpu().numpy()
print(f"Depth map shape before resize: {depth_map.shape}")
# Resize depth map to match original image size
depth_map = cv2.resize(depth_map, (original_width, original_height), interpolation=cv2.INTER_LINEAR)
print(f"Depth map shape after resize: {depth_map.shape}")
# Depth to Point Cloud
points_3D = cv2.reprojectImageTo3D(depth_map, self.Q, handleMissingValues=False)
mask_map = depth_map > 0.4 # Threshold to filter out invalid points
print(f"Mask map shape: {mask_map.shape}")
output_points = points_3D[mask_map]
output_colors = color_image[mask_map]
print(f"Output points shape: {output_points.shape}")
print(f"Output colors shape: {output_colors.shape}")
# Downsample
downsample_rate = int(1 / self.voxel_size)
if downsample_rate > 1:
downsampled_indices = np.arange(0, output_points.shape[0], downsample_rate)
output_points = output_points[downsampled_indices]
output_colors = output_colors[downsampled_indices]
# Apply transformations
output_points = np.dot(output_points, self.R.T)
homogeneous_points = np.hstack((output_points, np.ones((output_points.shape[0], 1))))
transformed_points = np.dot(homogeneous_points, self.transformation.T)[:, :3]
# Combine points and colors
point_cloud = np.hstack((transformed_points, output_colors))
return point_cloud, depth_map
def save_point_cloud_txt(self, point_cloud, filename):
np.savetxt(filename, point_cloud, fmt='%.6f %.6f %.6f %d %d %d')
Function to save point cloud to pcd file (if needed)
def save_point_cloud_pcd(point_cloud, filename):
with open(filename, 'w') as f:
f.write("# .PCD v0.7 - Point Cloud Data file format\n")
f.write("VERSION 0.7\n")
f.write("FIELDS x y z rgb\n")
f.write("SIZE 4 4 4 4\n")
f.write("TYPE F F F U\n")
f.write("COUNT 1 1 1 1\n")
f.write(f"WIDTH {len(point_cloud)}\n")
f.write("HEIGHT 1\n")
f.write("VIEWPOINT 0 0 0 1 0 0 0 0\n")
f.write(f"POINTS {len(point_cloud)}\n")
f.write("DATA ascii\n")
for point in point_cloud:
x, y, z, r, g, b = point
rgb = int(r) << 16 | int(g) << 8 | int(b)
f.write(f"{x} {y} {z} {rgb}\n")
Here's the combined code that implements the methods from the Metric3D code into the ZoeDepth code, using the ZoeDepth model:
python
import cv2
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
import open3d as o3d
# ZoeDepth imports
from zoe.zoedepth.models.builder import build_model
from zoe.zoedepth.utils.config import get_config
class PointCloudGenerator:
def __init__(self, model_name='zoedepth_nk', calibration_path='zoe/CalibrationMatrix_college_cpt.npz', transformation=None, voxel_size=0.02):
# ZoeDepth setup
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pretrained_resource = 'local::zoe/checkpoints/depth_anything_metric_depth_indoor.pt'
config = get_config(model_name, 'eval', 'nyu') # choose dataset 'nyu' or 'kitti' depending on indoor/outdoor
config.pretrained_resource = pretrained_resource
self.zoedepth_model = build_model(config).to(self.device)
self.zoedepth_model.eval()
# Load camera parameters
if calibration_path:
self.load_camera_parameters(calibration_path)
else:
self.set_default_camera_parameters()
# Rotation matrix (keeping this as it was in the original code)
self.R = np.eye(3)
if transformation is None:
self.transformation = np.eye(4)
else:
self.transformation = transformation
self.voxel_size = voxel_size
def set_default_camera_parameters(self):
self.FX = 350.0 # Default focal length
self.FY = 350.0
self.CX = 160.0 # Default principal point
self.CY = 120.0
self.dist_coeffs = np.zeros(5)
print("Using default camera parameters")
def load_camera_parameters(self, calibration_path):
try:
calibration_data = np.load(calibration_path)
if 'Camera_matrix' in calibration_data:
camera_matrix = calibration_data['Camera_matrix']
self.FX = camera_matrix[0, 0]
self.FY = camera_matrix[1, 1]
self.CX = camera_matrix[0, 2]
self.CY = camera_matrix[1, 2]
print(f"Camera parameters loaded: FX: {self.FX}, FY: {self.FY}, CX: {self.CX}, CY: {self.CY}")
else:
raise KeyError("Camera matrix not found in calibration file")
if 'distCoeff' in calibration_data:
self.dist_coeffs = calibration_data['distCoeff'].ravel()
print(f"Distortion coefficients loaded: {self.dist_coeffs}")
else:
print("Distortion coefficients not found, assuming no distortion")
self.dist_coeffs = np.zeros(5)
except Exception as e:
print(f"Error loading camera parameters: {e}")
self.set_default_camera_parameters()
def process_frame_with_depth(self, frame):
# Undistort the frame
h, w = frame.shape[:2]
new_camera_matrix, _ = cv2.getOptimalNewCameraMatrix(
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs, (w, h), 1, (w, h)
)
undistorted_frame = cv2.undistort(frame,
np.array([[self.FX, 0, self.CX], [0, self.FY, self.CY], [0, 0, 1]]),
self.dist_coeffs,
None,
new_camera_matrix)
# RGB to Depth
color_image = cv2.cvtColor(undistorted_frame, cv2.COLOR_BGR2RGB)
image_tensor = transforms.ToTensor()(color_image).unsqueeze(0).to(self.device)
# Use ZoeDepth model to get depth map
with torch.no_grad():
pred = self.zoedepth_model(image_tensor, dataset='nyu') # or 'kitti' for outdoor scenes
if isinstance(pred, dict):
pred = pred.get('metric_depth', pred.get('out'))
elif isinstance(pred, (list, tuple)):
pred = pred[-1]
depth_map = pred.squeeze().cpu().numpy()
# Ensure depth_map matches the original frame size
depth_map = cv2.resize(depth_map, (w, h), interpolation=cv2.INTER_LINEAR)
# Scale depth map to real-world units (e.g., meters)
depth_scale = 1.0 # Adjust this value based on your depth model's output scale
scaled_depth_map = depth_map * depth_scale
# Create 3D points
rows, cols = scaled_depth_map.shape
c, r = np.meshgrid(np.arange(cols), np.arange(rows))
points = np.zeros((rows * cols, 3), dtype=np.float32)
points[:, 0] = (c.flatten() - self.CX) * scaled_depth_map.flatten() / self.FX
points[:, 1] = (r.flatten() - self.CY) * scaled_depth_map.flatten() / self.FY
points[:, 2] = scaled_depth_map.flatten()
# Improved filtering
min_depth = 0.4 # Adjust based on your scene
max_depth = 10.0 # Adjust based on your scene
min_brightness = 10 # Adjust to filter out very dark points
brightness = np.mean(color_image.reshape(-1, 3), axis=1)
mask = (scaled_depth_map.flatten() > min_depth) & \
(scaled_depth_map.flatten() < max_depth) & \
(brightness > min_brightness)
valid_points = points[mask]
valid_colors = color_image.reshape(-1, 3)[mask]
# Apply transformations
transformed_points = np.dot(valid_points, self.R.T)
homogeneous_points = np.hstack((valid_points, np.ones((valid_points.shape[0], 1))))
transformed_points = np.dot(homogeneous_points, self.transformation.T)[:, :3]
# Downsample
if self.voxel_size > 0:
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(transformed_points)
pcd.colors = o3d.utility.Vector3dVector(valid_colors.astype(np.float64) / 255.0)
downsampled_pcd = pcd.voxel_down_sample(voxel_size=self.voxel_size)
transformed_points = np.asarray(downsampled_pcd.points)
valid_colors = np.asarray(downsampled_pcd.colors) * 255
# Combine points and colors for final point cloud
point_cloud = np.hstack((transformed_points, valid_colors.astype(np.uint8)))
return point_cloud, depth_map
def save_point_cloud_txt(self, point_cloud, filename):
np.savetxt(filename, point_cloud, fmt='%.6f %.6f %.6f %d %d %d')
def save_point_cloud_pcd(point_cloud, filename):
with open(filename, 'w') as f:
f.write("# .PCD v0.7 - Point Cloud Data file format\n")
f.write("VERSION 0.7\n")
f.write("FIELDS x y z rgb\n")
f.write("SIZE 4 4 4 4\n")
f.write("TYPE F F F U\n")
f.write("COUNT 1 1 1 1\n")
f.write(f"WIDTH {len(point_cloud)}\n")
f.write("HEIGHT 1\n")
f.write("VIEWPOINT 0 0 0 1 0 0 0 0\n")
f.write(f"POINTS {len(point_cloud)}\n")
f.write("DATA ascii\n")
for point in point_cloud:
x, y, z, r, g, b = point
rgb = int(r) << 16 | int(g) << 8 | int(b)
f.write(f"{x} {y} {z} {rgb}\n")
Explanation:
Model Loading: The __init__ method now initializes the ZoeDepth model instead of the Metric3D model. It uses the same setup as in your ZoeDepth code.
Camera Parameters: The methods set_default_camera_parameters and load_camera_parameters are kept the same to load and set the camera intrinsics.
Processing Frames: The process_frame_with_depth method is taken from the Metric3D code but modified to use the ZoeDepth model for depth estimation. It processes the frame, undistorts it, gets the depth map from ZoeDepth, and then computes the 3D points.
Depth Map Scaling: The depth map is scaled appropriately. You may need to adjust the depth_scale variable depending on the ZoeDepth model's output units.
3D Point Creation: The code computes the 3D points using the intrinsic camera parameters, similar to how it was done in the Metric3D code.
Filtering and Downsampling: The same filtering based on depth range and brightness is applied. Downsampling is performed using Open3D's voxel downsampling.
Transformation: The points are transformed using the rotation matrix R and the transformation matrix self.transformation.
Saving Point Cloud: The method save_point_cloud_txt allows you to save the point cloud to a text file. There's also a function save_point_cloud_pcd to save in PCD format if needed.
Note:
Make sure all necessary modules and dependencies are correctly installed in your environment, including Open3D and the ZoeDepth dependencies.
Adjust the min_depth, max_depth, and min_brightness parameters in the filtering step based on your specific scenario and the output range of the ZoeDepth model.
Ensure that the ZoeDepth model files and configuration are correctly set up according to your local paths and setup.