Update app.py
Browse files
app.py
CHANGED
|
@@ -97,26 +97,26 @@ def process_image(image_path, depth_map_path=None):
|
|
| 97 |
import cv2 # Make sure OpenCV is installed
|
| 98 |
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
|
|
|
| 112 |
image_o3d = o3d.geometry.Image(foreground)
|
| 113 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 114 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 119 |
-
|
| 120 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 121 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 122 |
|
|
@@ -127,7 +127,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 127 |
|
| 128 |
if not pcd.has_normals():
|
| 129 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 130 |
-
|
| 131 |
if pcd.has_normals():
|
| 132 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 133 |
else:
|
|
@@ -149,7 +149,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 149 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 150 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 151 |
return gltf_path
|
| 152 |
-
|
| 153 |
# Gradio Interface
|
| 154 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 155 |
description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
|
|
|
|
| 97 |
import cv2 # Make sure OpenCV is installed
|
| 98 |
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
+
# Resize depth_image with PIL while retaining uint16 detail
|
| 101 |
+
depth_image_resized = np.array(
|
| 102 |
+
Image.fromarray(depth_image.astype(np.uint16), mode='I;16').resize(
|
| 103 |
+
foreground.shape[:2][::-1], Image.Resampling.NEAREST
|
| 104 |
+
)
|
| 105 |
+
)
|
| 106 |
|
| 107 |
+
# Normalize depth_image to 0-255 range for Open3D compatibility while preserving uint16 precision
|
| 108 |
+
depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
|
| 109 |
+
(depth_image_resized.max() - depth_image_resized.min()) * 65535).astype(np.uint16)
|
| 110 |
|
| 111 |
+
# Convert the depth image to an Open3D Image object
|
| 112 |
+
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 113 |
image_o3d = o3d.geometry.Image(foreground)
|
| 114 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 115 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 116 |
)
|
| 117 |
+
|
| 118 |
+
# Remaining steps unchanged...
|
| 119 |
w, h = foreground.shape[1], foreground.shape[0]
|
|
|
|
| 120 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 121 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 122 |
|
|
|
|
| 127 |
|
| 128 |
if not pcd.has_normals():
|
| 129 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 130 |
+
|
| 131 |
if pcd.has_normals():
|
| 132 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 133 |
else:
|
|
|
|
| 149 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 150 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 151 |
return gltf_path
|
| 152 |
+
|
| 153 |
# Gradio Interface
|
| 154 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 155 |
description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
|