Update app.py
Browse files
app.py
CHANGED
|
@@ -109,8 +109,16 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 109 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 110 |
depth_image_resized.max() - depth_image_resized.min()
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
depth_image_normalized = depth_image_normalized.astype(np.float32)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# Create flat background depth with a small gap for empty space
|
| 115 |
background_depth = np.zeros_like(depth_image_normalized) + 1.5
|
| 116 |
|
|
@@ -123,11 +131,16 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 123 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 124 |
)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
w, h = foreground.shape[1], foreground.shape[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 129 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 130 |
|
|
|
|
| 131 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 132 |
|
| 133 |
# Debugging: Print point cloud size
|
|
@@ -135,10 +148,6 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 135 |
if len(pcd.points) < 100:
|
| 136 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 137 |
|
| 138 |
-
# Ensure the depth image is valid
|
| 139 |
-
if np.count_nonzero(depth_image_normalized) == 0:
|
| 140 |
-
raise RuntimeError("Depth image contains only zeros. Please check the input.")
|
| 141 |
-
|
| 142 |
# Estimate normals and orient
|
| 143 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 144 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
|
@@ -148,6 +157,10 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 148 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 149 |
)
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
# Simplify and smooth the mesh
|
| 152 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 153 |
mesh = mesh_raw.simplify_vertex_clustering(
|
|
@@ -157,10 +170,6 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 157 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 158 |
|
| 159 |
# Validate the mesh before cropping
|
| 160 |
-
if not mesh.has_triangles():
|
| 161 |
-
raise RuntimeError("No triangles found in the generated mesh. Check the point cloud data.")
|
| 162 |
-
|
| 163 |
-
# Crop and align model based on the bounding box
|
| 164 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 165 |
if bbox.is_empty():
|
| 166 |
raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
|
|
@@ -174,6 +183,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 174 |
|
| 175 |
return gltf_path
|
| 176 |
|
|
|
|
| 177 |
# Gradio Interface
|
| 178 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 179 |
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."
|
|
|
|
| 109 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 110 |
depth_image_resized.max() - depth_image_resized.min()
|
| 111 |
)
|
| 112 |
+
|
| 113 |
+
# Debugging: Check normalization results
|
| 114 |
+
print(f"Depth Image Min: {depth_image_resized.min()}, Max: {depth_image_resized.max()}")
|
| 115 |
+
|
| 116 |
depth_image_normalized = depth_image_normalized.astype(np.float32)
|
| 117 |
|
| 118 |
+
# Ensure depth image is valid
|
| 119 |
+
if np.all(depth_image_normalized == 0):
|
| 120 |
+
raise RuntimeError("Depth image normalization resulted in all zeros.")
|
| 121 |
+
|
| 122 |
# Create flat background depth with a small gap for empty space
|
| 123 |
background_depth = np.zeros_like(depth_image_normalized) + 1.5
|
| 124 |
|
|
|
|
| 131 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 132 |
)
|
| 133 |
|
| 134 |
+
# Check if the depth and foreground sizes are compatible
|
| 135 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 136 |
+
if rgbd_image.color.shape[1] != w or rgbd_image.color.shape[0] != h:
|
| 137 |
+
raise RuntimeError("RGBD image dimensions do not match foreground dimensions.")
|
| 138 |
+
|
| 139 |
+
# Camera intrinsic setup
|
| 140 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 141 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 142 |
|
| 143 |
+
# Create the point cloud
|
| 144 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 145 |
|
| 146 |
# Debugging: Print point cloud size
|
|
|
|
| 148 |
if len(pcd.points) < 100:
|
| 149 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
# Estimate normals and orient
|
| 152 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 153 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
|
|
|
| 157 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 158 |
)
|
| 159 |
|
| 160 |
+
# Validate mesh creation
|
| 161 |
+
if not mesh_raw.has_triangles():
|
| 162 |
+
raise RuntimeError("No triangles found in the generated mesh. Check the point cloud data.")
|
| 163 |
+
|
| 164 |
# Simplify and smooth the mesh
|
| 165 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 166 |
mesh = mesh_raw.simplify_vertex_clustering(
|
|
|
|
| 170 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 171 |
|
| 172 |
# Validate the mesh before cropping
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 174 |
if bbox.is_empty():
|
| 175 |
raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
|
|
|
|
| 183 |
|
| 184 |
return gltf_path
|
| 185 |
|
| 186 |
+
|
| 187 |
# Gradio Interface
|
| 188 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 189 |
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."
|