Update app.py
Browse files
app.py
CHANGED
|
@@ -34,9 +34,7 @@ def normalize_depth_image(depth_image):
|
|
| 34 |
return depth_image_normalized
|
| 35 |
|
| 36 |
|
| 37 |
-
|
| 38 |
# Function to inpaint the background
|
| 39 |
-
|
| 40 |
def inpaint_background(image, mask):
|
| 41 |
# Ensure the mask is binary (0 and 255)
|
| 42 |
mask_binary = (mask * 255).astype(np.uint8)
|
|
@@ -55,7 +53,6 @@ def inpaint_background(image, mask):
|
|
| 55 |
|
| 56 |
return inpainted_image
|
| 57 |
|
| 58 |
-
# Function to inpaint the background
|
| 59 |
|
| 60 |
def process_image(image_path, depth_map_path=None):
|
| 61 |
image_path = Path(image_path)
|
|
@@ -133,23 +130,23 @@ def process_image(image_path, depth_map_path=None):
|
|
| 133 |
return [img, gr.Model3D(), gltf_path]
|
| 134 |
|
| 135 |
|
| 136 |
-
|
| 137 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 138 |
-
# Resize and normalize the depth image
|
| 139 |
depth_image_resized = np.array(
|
| 140 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 141 |
-
).astype(np.float32)
|
| 142 |
|
| 143 |
-
# Normalize depth values to the range [0,
|
| 144 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 145 |
depth_image_resized.max() - depth_image_resized.min()
|
| 146 |
)
|
|
|
|
| 147 |
|
| 148 |
-
# Create Open3D images
|
| 149 |
-
depth_o3d = o3d.geometry.Image(
|
| 150 |
image_o3d = o3d.geometry.Image(foreground)
|
| 151 |
|
| 152 |
-
# Create RGBD image
|
| 153 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 154 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 155 |
)
|
|
@@ -195,7 +192,6 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 195 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 196 |
return gltf_path
|
| 197 |
|
| 198 |
-
|
| 199 |
# Gradio Interface
|
| 200 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 201 |
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."
|
|
|
|
| 34 |
return depth_image_normalized
|
| 35 |
|
| 36 |
|
|
|
|
| 37 |
# Function to inpaint the background
|
|
|
|
| 38 |
def inpaint_background(image, mask):
|
| 39 |
# Ensure the mask is binary (0 and 255)
|
| 40 |
mask_binary = (mask * 255).astype(np.uint8)
|
|
|
|
| 53 |
|
| 54 |
return inpainted_image
|
| 55 |
|
|
|
|
| 56 |
|
| 57 |
def process_image(image_path, depth_map_path=None):
|
| 58 |
image_path = Path(image_path)
|
|
|
|
| 130 |
return [img, gr.Model3D(), gltf_path]
|
| 131 |
|
| 132 |
|
|
|
|
| 133 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 134 |
+
# Resize and normalize the depth image
|
| 135 |
depth_image_resized = np.array(
|
| 136 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 137 |
+
).astype(np.float32)
|
| 138 |
|
| 139 |
+
# Normalize depth values to the range [0, 65535] for uint16
|
| 140 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 141 |
depth_image_resized.max() - depth_image_resized.min()
|
| 142 |
)
|
| 143 |
+
depth_image_uint16 = (depth_image_normalized * 65535).astype(np.uint16) # Scale to uint16
|
| 144 |
|
| 145 |
+
# Create Open3D images
|
| 146 |
+
depth_o3d = o3d.geometry.Image(depth_image_uint16)
|
| 147 |
image_o3d = o3d.geometry.Image(foreground)
|
| 148 |
|
| 149 |
+
# Create RGBD image using the uint16 depth image
|
| 150 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 151 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 152 |
)
|
|
|
|
| 192 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 193 |
return gltf_path
|
| 194 |
|
|
|
|
| 195 |
# Gradio Interface
|
| 196 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 197 |
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."
|