import torch import torchvision.transforms as transforms from PIL import Image import numpy as np # Example: Using a pretrained depth estimation model # (MiDaS is a popular choice for turning 2D images into depth maps) midas = torch.hub.load("intel-isl/MiDaS", "DPT_Hybrid") midas.eval() transform = torch.hub.load("intel-isl/MiDaS", "transforms").dpt_transform def estimate_depth(image_path): img = Image.open(image_path) input_batch = transform(img).unsqueeze(0) with torch.no_grad(): prediction = midas(input_batch) depth_map = torch.nn.functional.interpolate( prediction.unsqueeze(1), size=img.size[::-1], mode="bicubic", align_corners=False, ).squeeze() return depth_map.cpu().numpy() # Example usage depth = estimate_depth("your_picture.jpg") # Convert depth map into a 3D point cloud import open3d as o3d def depth_to_pointcloud(depth_map, scale=1.0): h, w = depth_map.shape xx, yy = np.meshgrid(np.arange(w), np.arange(h)) points = np.stack((xx, yy, depth_map * scale), axis=-1).reshape(-1, 3) pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) return pcd pcd = depth_to_pointcloud(depth) o3d.visualization.draw_geometries([pcd])