Update app.py
Browse files
app.py
CHANGED
|
@@ -95,15 +95,21 @@ def process_image(image_path, depth_map_path=None):
|
|
| 95 |
|
| 96 |
|
| 97 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
|
|
| 98 |
depth_image_resized = np.array(
|
| 99 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 100 |
)
|
|
|
|
|
|
|
| 101 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 102 |
depth_image_resized.max() - depth_image_resized.min()
|
| 103 |
-
) * 65535
|
| 104 |
-
depth_image_normalized = depth_image_normalized.astype(np.uint16) #
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
depth_o3d = o3d.geometry.Image(
|
| 107 |
image_o3d = o3d.geometry.Image(foreground)
|
| 108 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 109 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
|
@@ -142,7 +148,6 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 142 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 143 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 144 |
return gltf_path
|
| 145 |
-
|
| 146 |
# Gradio Interface
|
| 147 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 148 |
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."
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 98 |
+
# Resize depth image while retaining its uint16 format
|
| 99 |
depth_image_resized = np.array(
|
| 100 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 101 |
)
|
| 102 |
+
|
| 103 |
+
# Normalize the depth image to the range of [0, 65535] before conversion
|
| 104 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 105 |
depth_image_resized.max() - depth_image_resized.min()
|
| 106 |
+
) * 65535
|
| 107 |
+
depth_image_normalized = depth_image_normalized.astype(np.uint16) # Keep it uint16
|
| 108 |
+
|
| 109 |
+
# Convert depth image to uint8 for use with Open3D
|
| 110 |
+
depth_image_uint8 = img_as_ubyte(depth_image_normalized / 65535.0) # Scale to [0, 1] before conversion
|
| 111 |
|
| 112 |
+
depth_o3d = o3d.geometry.Image(depth_image_uint8)
|
| 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
|
|
|
|
| 148 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 149 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 150 |
return gltf_path
|
|
|
|
| 151 |
# Gradio Interface
|
| 152 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 153 |
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."
|