Update app.py
Browse files
app.py
CHANGED
|
@@ -136,15 +136,19 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 136 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 137 |
).astype(np.float32)
|
| 138 |
|
| 139 |
-
# Normalize depth values
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
@@ -183,15 +187,18 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 183 |
)
|
| 184 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 185 |
|
| 186 |
-
# Crop the mesh to fit the bounding box of the
|
| 187 |
-
bbox =
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
# Save the
|
| 191 |
-
gltf_path =
|
| 192 |
-
o3d.io.write_triangle_mesh(gltf_path,
|
| 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."
|
|
|
|
| 136 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 137 |
).astype(np.float32)
|
| 138 |
|
| 139 |
+
# Normalize depth values and convert to 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 |
+
# Log depth image details to verify compatibility
|
| 146 |
+
print("Depth image dtype:", depth_image_uint16.dtype)
|
| 147 |
+
print("Depth image min, max values:", depth_image_uint16.min(), depth_image_uint16.max())
|
| 148 |
+
|
| 149 |
+
# Convert to Open3D images
|
| 150 |
depth_o3d = o3d.geometry.Image(depth_image_uint16)
|
| 151 |
+
image_o3d = o3d.geometry.Image(foreground.astype(np.uint8)) # Ensure foreground is uint8
|
| 152 |
|
| 153 |
# Create RGBD image using the uint16 depth image
|
| 154 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 187 |
)
|
| 188 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 189 |
|
| 190 |
+
# Crop the mesh to fit the bounding box of the subject
|
| 191 |
+
bbox = mesh.get_axis_aligned_bounding_box()
|
| 192 |
+
bbox.min_bound -= [1, 1, 1]
|
| 193 |
+
bbox.max_bound += [1, 1, 1]
|
| 194 |
+
mesh = mesh.crop(bbox)
|
| 195 |
|
| 196 |
+
# Save the mesh to a GLTF file
|
| 197 |
+
gltf_path = str(image_path.with_suffix(".gltf"))
|
| 198 |
+
o3d.io.write_triangle_mesh(gltf_path, mesh)
|
|
|
|
| 199 |
|
| 200 |
+
return gltf_path
|
| 201 |
+
|
| 202 |
# Gradio Interface
|
| 203 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 204 |
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."
|