Update app.py
Browse files
app.py
CHANGED
|
@@ -93,25 +93,27 @@ def process_image(image_path, depth_map_path=None):
|
|
| 93 |
return [img, gltf_path, gltf_path]
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 96 |
-
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=
|
| 97 |
img = Image.fromarray(blended_depth_image)
|
| 98 |
return [img, gltf_path, gltf_path]
|
| 99 |
|
| 100 |
-
def create_3d_obj(foreground, depth_image, background, image_path, depth=
|
| 101 |
# Resize depth_image to match the size of the foreground
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 104 |
)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
depth_image_resized = (depth_image_resized - np.min(depth_image_resized)) / (
|
| 108 |
np.max(depth_image_resized) - np.min(depth_image_resized)
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
# Convert the normalized float image to 8-bit unsigned integers
|
| 112 |
depth_image_resized = img_as_ubyte(depth_image_resized)
|
| 113 |
|
| 114 |
-
# The rest of the function follows
|
| 115 |
depth_o3d = o3d.geometry.Image(depth_image_resized)
|
| 116 |
image_o3d = o3d.geometry.Image(foreground)
|
| 117 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
@@ -128,7 +130,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 128 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 129 |
|
| 130 |
if not pcd.has_normals():
|
| 131 |
-
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.
|
| 132 |
|
| 133 |
if pcd.has_normals():
|
| 134 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
|
@@ -136,7 +138,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 136 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 137 |
|
| 138 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 139 |
-
pcd, depth=depth, width=
|
| 140 |
)
|
| 141 |
|
| 142 |
# Flatten the back of the model from the zero point
|
|
|
|
| 93 |
return [img, gltf_path, gltf_path]
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 96 |
+
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=8)
|
| 97 |
img = Image.fromarray(blended_depth_image)
|
| 98 |
return [img, gltf_path, gltf_path]
|
| 99 |
|
| 100 |
+
def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
| 101 |
# Resize depth_image to match the size of the foreground
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# Normalize depth_image_resized to the range [0, 1]
|
| 107 |
depth_image_resized = (depth_image_resized - np.min(depth_image_resized)) / (
|
| 108 |
np.max(depth_image_resized) - np.min(depth_image_resized)
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# Check if the normalized depth_image_resized is within the range [0, 1]
|
| 112 |
+
depth_image_resized = np.clip(depth_image_resized, 0, 1) # Ensures the values are between 0 and 1
|
| 113 |
+
|
| 114 |
# Convert the normalized float image to 8-bit unsigned integers
|
| 115 |
depth_image_resized = img_as_ubyte(depth_image_resized)
|
| 116 |
|
|
|
|
| 117 |
depth_o3d = o3d.geometry.Image(depth_image_resized)
|
| 118 |
image_o3d = o3d.geometry.Image(foreground)
|
| 119 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 130 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 131 |
|
| 132 |
if not pcd.has_normals():
|
| 133 |
+
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.5, max_nn=50))
|
| 134 |
|
| 135 |
if pcd.has_normals():
|
| 136 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
|
|
|
| 138 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 139 |
|
| 140 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 141 |
+
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 142 |
)
|
| 143 |
|
| 144 |
# Flatten the back of the model from the zero point
|