Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,22 @@ from PIL import Image
|
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
|
|
| 9 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 10 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 11 |
|
| 12 |
def process_image(image_path):
|
| 13 |
-
image_path = Path(image_path)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
image = image_raw.resize(
|
| 16 |
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
| 17 |
-
Image.Resampling.LANCZOS
|
|
|
|
| 18 |
|
| 19 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 20 |
|
|
@@ -22,6 +29,7 @@ def process_image(image_path):
|
|
| 22 |
outputs = model(**encoding)
|
| 23 |
predicted_depth = outputs.predicted_depth
|
| 24 |
|
|
|
|
| 25 |
prediction = torch.nn.functional.interpolate(
|
| 26 |
predicted_depth.unsqueeze(1),
|
| 27 |
size=image.size[::-1],
|
|
@@ -29,7 +37,11 @@ def process_image(image_path):
|
|
| 29 |
align_corners=False,
|
| 30 |
).squeeze()
|
| 31 |
output = prediction.cpu().numpy()
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
try:
|
| 35 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
|
@@ -43,27 +55,32 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 43 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 44 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 45 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
| 46 |
-
w, h = depth_image.shape[1], depth_image.shape[0]
|
| 47 |
|
|
|
|
| 48 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 49 |
-
camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)
|
|
|
|
| 50 |
|
| 51 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 52 |
pcd.estimate_normals(
|
| 53 |
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 54 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
|
| 55 |
-
|
|
|
|
| 56 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
| 57 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 58 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
| 59 |
|
| 60 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
| 61 |
mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)
|
| 62 |
-
|
|
|
|
| 63 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 64 |
mesh_crop = mesh.crop(bbox)
|
|
|
|
| 65 |
gltf_path = f'./{image_path.stem}.gltf'
|
| 66 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
|
|
|
| 67 |
return gltf_path
|
| 68 |
|
| 69 |
title = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"
|
|
@@ -90,3 +107,4 @@ if __name__ == "__main__":
|
|
| 90 |
|
| 91 |
|
| 92 |
|
|
|
|
|
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
+
# Load model and feature extractor
|
| 10 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 11 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 12 |
|
| 13 |
def process_image(image_path):
|
| 14 |
+
image_path = Path(image_path) if isinstance(image_path, str) else image_path
|
| 15 |
+
try:
|
| 16 |
+
image_raw = Image.open(image_path).convert("RGB")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error loading image: {e}"
|
| 19 |
+
|
| 20 |
+
# Resize while maintaining aspect ratio
|
| 21 |
image = image_raw.resize(
|
| 22 |
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
| 23 |
+
Image.Resampling.LANCZOS
|
| 24 |
+
)
|
| 25 |
|
| 26 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 27 |
|
|
|
|
| 29 |
outputs = model(**encoding)
|
| 30 |
predicted_depth = outputs.predicted_depth
|
| 31 |
|
| 32 |
+
# Normalize depth image
|
| 33 |
prediction = torch.nn.functional.interpolate(
|
| 34 |
predicted_depth.unsqueeze(1),
|
| 35 |
size=image.size[::-1],
|
|
|
|
| 37 |
align_corners=False,
|
| 38 |
).squeeze()
|
| 39 |
output = prediction.cpu().numpy()
|
| 40 |
+
|
| 41 |
+
if np.max(output) > 0:
|
| 42 |
+
depth_image = (output * 255 / np.max(output)).astype('uint8')
|
| 43 |
+
else:
|
| 44 |
+
depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
|
| 45 |
|
| 46 |
try:
|
| 47 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
|
|
|
| 55 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 56 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 57 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
|
|
|
| 58 |
|
| 59 |
+
w, h = depth_image.shape[1], depth_image.shape[0]
|
| 60 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 61 |
+
camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
|
| 62 |
+
|
| 63 |
|
| 64 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 65 |
pcd.estimate_normals(
|
| 66 |
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 67 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
|
| 68 |
+
|
| 69 |
+
|
| 70 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
| 71 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 72 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
| 73 |
|
| 74 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
| 75 |
mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 79 |
mesh_crop = mesh.crop(bbox)
|
| 80 |
+
|
| 81 |
gltf_path = f'./{image_path.stem}.gltf'
|
| 82 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 83 |
+
|
| 84 |
return gltf_path
|
| 85 |
|
| 86 |
title = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
+
|