Update app.py
Browse files
app.py
CHANGED
|
@@ -100,21 +100,32 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
|
| 100 |
depth_image_resized = (depth_image_resized - np.min(depth_image_resized)) / (
|
| 101 |
np.max(depth_image_resized) - np.min(depth_image_resized)
|
| 102 |
)
|
| 103 |
-
depth_image_resized = np.clip(depth_image_resized, 0, 1) #
|
| 104 |
|
| 105 |
-
# Convert the normalized
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
| 108 |
depth_o3d = o3d.geometry.Image(depth_image_resized)
|
| 109 |
image_o3d = o3d.geometry.Image(foreground)
|
| 110 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 111 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 112 |
)
|
|
|
|
| 113 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 114 |
|
|
|
|
| 115 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 116 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 117 |
|
|
|
|
| 118 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 119 |
|
| 120 |
if len(pcd.points) < 100:
|
|
@@ -128,6 +139,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
|
| 128 |
else:
|
| 129 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 130 |
|
|
|
|
| 131 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 132 |
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 133 |
)
|
|
@@ -142,8 +154,10 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
|
| 142 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 143 |
mesh_crop = mesh.crop(bbox)
|
| 144 |
|
|
|
|
| 145 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 146 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
|
|
|
| 147 |
return gltf_path
|
| 148 |
|
| 149 |
# Gradio Interface
|
|
|
|
| 100 |
depth_image_resized = (depth_image_resized - np.min(depth_image_resized)) / (
|
| 101 |
np.max(depth_image_resized) - np.min(depth_image_resized)
|
| 102 |
)
|
| 103 |
+
depth_image_resized = np.clip(depth_image_resized, 0, 1) # Ensure values are between 0 and 1
|
| 104 |
|
| 105 |
+
# Convert the normalized image to float format
|
| 106 |
+
depth_image_float = img_as_float(depth_image_resized)
|
| 107 |
+
|
| 108 |
+
# Ensure the float image is between -1 and 1 before converting to uint8
|
| 109 |
+
if np.min(depth_image_float) < -1 or np.max(depth_image_float) > 1:
|
| 110 |
+
raise ValueError("Depth image must be in the range [-1, 1] before conversion to uint8.")
|
| 111 |
+
|
| 112 |
+
# Convert to 8-bit unsigned integers
|
| 113 |
+
depth_image_resized = img_as_ubyte(depth_image_float)
|
| 114 |
|
| 115 |
+
# Create Open3D image from the depth and foreground images
|
| 116 |
depth_o3d = o3d.geometry.Image(depth_image_resized)
|
| 117 |
image_o3d = o3d.geometry.Image(foreground)
|
| 118 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 119 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 120 |
)
|
| 121 |
+
|
| 122 |
w, h = foreground.shape[1], foreground.shape[0]
|
| 123 |
|
| 124 |
+
# Set up the camera intrinsic parameters
|
| 125 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 126 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 127 |
|
| 128 |
+
# Create point cloud from RGBD image
|
| 129 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 130 |
|
| 131 |
if len(pcd.points) < 100:
|
|
|
|
| 139 |
else:
|
| 140 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 141 |
|
| 142 |
+
# Generate the mesh using Poisson reconstruction
|
| 143 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 144 |
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 145 |
)
|
|
|
|
| 154 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 155 |
mesh_crop = mesh.crop(bbox)
|
| 156 |
|
| 157 |
+
# Save the mesh to a GLTF file
|
| 158 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 159 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 160 |
+
|
| 161 |
return gltf_path
|
| 162 |
|
| 163 |
# Gradio Interface
|