Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines678910111213141516171819201 unmodified line2223242526271 unmodified line293031323334357 unmodified lines43444546474849505152535455565758596061626364656667686920 unmodified lines9091925 unmodified linesimport open3d as o3dfrom pathlib import Pathfeature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")def process_image(image_path):image_path = Path(image_path)image_raw = Image.open(image_path)image = image_raw.resize((800, int(800 * image_raw.size[1] / image_raw.size[0])),Image.Resampling.LANCZOS)encoding = feature_extractor(image, return_tensors="pt")1 unmodified lineoutputs = model(**encoding)predicted_depth = outputs.predicted_depthprediction = torch.nn.functional.interpolate(predicted_depth.unsqueeze(1),size=image.size[::-1],1 unmodified linealign_corners=False,).squeeze()output = prediction.cpu().numpy()depth_image = (output * 255 / np.max(output)).astype('uint8')try:gltf_path = create_3d_obj(np.array(image), depth_image, image_path)7 unmodified linesimage_o3d = o3d.geometry.Image(rgb_image)rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(image_o3d, depth_o3d, convert_rgb_to_intensity=False)w, h = depth_image.shape[1], depth_image.shape[0]camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=depth, width=0, scale=1.1, linear_fit=True)voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)bbox = pcd.get_axis_aligned_bounding_box()mesh_crop = mesh.crop(bbox)gltf_path = f'./{image_path.stem}.gltf'o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)return gltf_pathtitle = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"20 unmodified lines5 unmodified lines67891011121314151617181920212223242526271 unmodified line293031323334351 unmodified line37383940414243444546477 unmodified lines555657585960616263646566676869707172737475767778798081828384858620 unmodified lines1071081091105 unmodified linesimport open3d as o3dfrom pathlib import Path# Load model and feature extractorfeature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")def process_image(image_path):image_path = Path(image_path) if isinstance(image_path, str) else image_pathtry:image_raw = Image.open(image_path).convert("RGB")except Exception as e:return f"Error loading image: {e}"# Resize while maintaining aspect ratioimage = image_raw.resize((800, int(800 * image_raw.size[1] / image_raw.size[0])),Image.Resampling.LANCZOS)encoding = feature_extractor(image, return_tensors="pt")1 unmodified lineoutputs = model(**encoding)predicted_depth = outputs.predicted_depth# Normalize depth imageprediction = torch.nn.functional.interpolate(predicted_depth.unsqueeze(1),size=image.size[::-1],1 unmodified linealign_corners=False,).squeeze()output = prediction.cpu().numpy()if np.max(output) > 0:depth_image = (output * 255 / np.max(output)).astype('uint8')else:depth_image = np.zeros_like(output, dtype='uint8') # Handle empty outputtry:gltf_path = create_3d_obj(np.array(image), depth_image, image_path)7 unmodified linesimage_o3d = o3d.geometry.Image(rgb_image)rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(image_o3d, depth_o3d, convert_rgb_to_intensity=False)w, h = depth_image.shape[1], depth_image.shape[0]camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=depth, width=0, scale=1.1, linear_fit=True)voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)bbox = pcd.get_axis_aligned_bounding_box()mesh_crop = mesh.crop(bbox)gltf_path = f'./{image_path.stem}.gltf'o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)return gltf_pathtitle = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"20 unmodified lines
app.py+26-8