Update app.py
Browse files
app.py
CHANGED
|
@@ -96,15 +96,19 @@ def process_image(image_path, depth_map_path=None):
|
|
| 96 |
|
| 97 |
import cv2 # Make sure OpenCV is installed
|
| 98 |
|
|
|
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
# Resize depth_image with PIL while retaining uint16 detail
|
| 101 |
depth_image_resized = np.array(
|
| 102 |
-
Image.fromarray(depth_image.astype(np.uint16), mode='I;16').resize(
|
| 103 |
foreground.shape[:2][::-1], Image.Resampling.NEAREST
|
| 104 |
)
|
| 105 |
)
|
| 106 |
|
| 107 |
-
#
|
|
|
|
|
|
|
|
|
|
| 108 |
depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
|
| 109 |
(depth_image_resized.max() - depth_image_resized.min()) * 65535).astype(np.uint16)
|
| 110 |
|
|
@@ -114,17 +118,19 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 114 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 115 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 116 |
)
|
| 117 |
-
|
| 118 |
-
#
|
| 119 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 120 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 121 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 122 |
|
|
|
|
| 123 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 124 |
|
| 125 |
if len(pcd.points) < 100:
|
| 126 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 127 |
|
|
|
|
| 128 |
if not pcd.has_normals():
|
| 129 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 130 |
|
|
@@ -133,19 +139,24 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 133 |
else:
|
| 134 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 135 |
|
|
|
|
| 136 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 137 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 138 |
)
|
| 139 |
|
|
|
|
| 140 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 141 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 142 |
voxel_size=voxel_size,
|
| 143 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 144 |
)
|
| 145 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
|
|
|
|
|
|
| 146 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 147 |
mesh_crop = mesh.crop(bbox)
|
| 148 |
|
|
|
|
| 149 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 150 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 151 |
return gltf_path
|
|
|
|
| 96 |
|
| 97 |
import cv2 # Make sure OpenCV is installed
|
| 98 |
|
| 99 |
+
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 101 |
# Resize depth_image with PIL while retaining uint16 detail
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
+
Image.fromarray((depth_image * 256).astype(np.uint16), mode='I;16').resize(
|
| 104 |
foreground.shape[:2][::-1], Image.Resampling.NEAREST
|
| 105 |
)
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Convert depth_image_resized back to uint8 for compatibility if needed
|
| 109 |
+
depth_image_resized_uint8 = (depth_image_resized / 256).astype(np.uint8)
|
| 110 |
+
|
| 111 |
+
# Normalize depth_image_resized to 0-65535 range for Open3D compatibility, keeping uint16 precision
|
| 112 |
depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
|
| 113 |
(depth_image_resized.max() - depth_image_resized.min()) * 65535).astype(np.uint16)
|
| 114 |
|
|
|
|
| 118 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 119 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 120 |
)
|
| 121 |
+
|
| 122 |
+
# Set camera intrinsic parameters
|
| 123 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 124 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 125 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 126 |
|
| 127 |
+
# Create point cloud from RGBD image
|
| 128 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 129 |
|
| 130 |
if len(pcd.points) < 100:
|
| 131 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 132 |
|
| 133 |
+
# Estimate normals
|
| 134 |
if not pcd.has_normals():
|
| 135 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 136 |
|
|
|
|
| 139 |
else:
|
| 140 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 141 |
|
| 142 |
+
# Create mesh using Poisson reconstruction
|
| 143 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 144 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 145 |
)
|
| 146 |
|
| 147 |
+
# Simplify mesh
|
| 148 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 149 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 150 |
voxel_size=voxel_size,
|
| 151 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 152 |
)
|
| 153 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 154 |
+
|
| 155 |
+
# Crop mesh to point cloud bounding box
|
| 156 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 157 |
mesh_crop = mesh.crop(bbox)
|
| 158 |
|
| 159 |
+
# Save as GLTF
|
| 160 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 161 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 162 |
return gltf_path
|