Update app.py
Browse files
app.py
CHANGED
|
@@ -84,64 +84,70 @@ def process_image(image_path, depth_map_path=None):
|
|
| 84 |
img = Image.fromarray(sharp_depth_image)
|
| 85 |
return [img, gltf_path, gltf_path]
|
| 86 |
|
| 87 |
-
def create_3d_obj(foreground, depth_image, background, image_path, depth=10, min_depth=
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 91 |
-
)
|
| 92 |
-
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 93 |
-
depth_image_resized.max() - depth_image_resized.min()
|
| 94 |
-
) * 255
|
| 95 |
-
depth_image_normalized = depth_image_normalized.astype(np.uint8)
|
| 96 |
-
|
| 97 |
-
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 98 |
-
image_o3d = o3d.geometry.Image(foreground)
|
| 99 |
-
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 100 |
-
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 101 |
-
)
|
| 102 |
-
w, h = foreground.shape[1], foreground.shape[0]
|
| 103 |
-
|
| 104 |
-
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 105 |
-
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 106 |
-
|
| 107 |
-
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 108 |
-
|
| 109 |
-
# Check the point count
|
| 110 |
-
num_points = len(pcd.points)
|
| 111 |
-
logging.debug(f"Depth level: {depth}, Point count: {num_points}")
|
| 112 |
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 117 |
-
# Otherwise, reduce depth and try again
|
| 118 |
-
return create_3d_obj(foreground, depth_image, background, image_path, depth=depth - 1, min_depth=min_depth)
|
| 119 |
-
|
| 120 |
-
if not pcd.has_normals():
|
| 121 |
-
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 122 |
-
|
| 123 |
-
if pcd.has_normals():
|
| 124 |
-
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 125 |
-
else:
|
| 126 |
-
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
# Gradio Interface
|
| 146 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 147 |
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."
|
|
|
|
| 84 |
img = Image.fromarray(sharp_depth_image)
|
| 85 |
return [img, gltf_path, gltf_path]
|
| 86 |
|
| 87 |
+
def create_3d_obj(foreground, depth_image, background, image_path, depth=10, min_depth=5):
|
| 88 |
+
if depth < min_depth:
|
| 89 |
+
raise RuntimeError("Reached minimum depth without sufficient points for the point cloud.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
try:
|
| 92 |
+
# Resize and normalize depth image
|
| 93 |
+
depth_image_resized = np.array(
|
| 94 |
+
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 95 |
+
)
|
| 96 |
+
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 97 |
+
depth_image_resized.max() - depth_image_resized.min()
|
| 98 |
+
) * 255
|
| 99 |
+
depth_image_normalized = depth_image_normalized.astype(np.uint8)
|
| 100 |
+
|
| 101 |
+
# Convert to Open3D format
|
| 102 |
+
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 103 |
+
image_o3d = o3d.geometry.Image(foreground)
|
| 104 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 105 |
+
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 106 |
+
)
|
| 107 |
+
w, h = foreground.shape[1], foreground.shape[0]
|
| 108 |
+
|
| 109 |
+
# Set up camera intrinsic
|
| 110 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 111 |
+
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 112 |
+
|
| 113 |
+
# Create point cloud
|
| 114 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 115 |
+
if len(pcd.points) < 100:
|
| 116 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
if not pcd.has_normals():
|
| 119 |
+
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 120 |
+
|
| 121 |
+
if pcd.has_normals():
|
| 122 |
+
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 123 |
+
else:
|
| 124 |
+
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 125 |
+
|
| 126 |
+
# Mesh reconstruction
|
| 127 |
+
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 128 |
+
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Simplify and crop the mesh
|
| 132 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 133 |
+
mesh = mesh_raw.simplify_vertex_clustering(
|
| 134 |
+
voxel_size=voxel_size,
|
| 135 |
+
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 136 |
+
)
|
| 137 |
+
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 138 |
+
bbox = pcd.get_axis_aligned_bounding_box()
|
| 139 |
+
mesh_crop = mesh.crop(bbox)
|
| 140 |
+
|
| 141 |
+
# Save to GLTF
|
| 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 |
+
except RuntimeError as e:
|
| 147 |
+
print(f"Error: {str(e)}. Retrying with a shallower depth.")
|
| 148 |
+
# Recursive call with reduced depth if point cloud is insufficient
|
| 149 |
+
return create_3d_obj(foreground, depth_image, background, image_path, depth=depth - 1, min_depth=min_depth)
|
| 150 |
+
|
| 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."
|