Update app.py
Browse files
app.py
CHANGED
|
@@ -98,19 +98,13 @@ def process_image(image_path, depth_map_path=None):
|
|
| 98 |
import cv2 # Make sure OpenCV is installed
|
| 99 |
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 101 |
-
# Ensure depth_image is 2D and of type uint16
|
| 102 |
-
if depth_image.ndim != 2:
|
| 103 |
-
raise ValueError("Depth image must be a 2D array.")
|
| 104 |
-
if depth_image.dtype != np.uint16:
|
| 105 |
-
depth_image = depth_image.astype(np.uint16)
|
| 106 |
-
|
| 107 |
# Step 1: Resize depth_image with PIL using bilinear interpolation for smoother resizing
|
| 108 |
depth_image_resized = np.array(
|
| 109 |
-
Image.fromarray(depth_image, mode='I').resize(
|
| 110 |
foreground.shape[:2][::-1], Image.Resampling.BILINEAR
|
| 111 |
)
|
| 112 |
)
|
| 113 |
-
|
| 114 |
# Step 2: Apply a slight Gaussian blur to the depth map to smooth out artifacts
|
| 115 |
depth_image_smoothed = gaussian_filter(depth_image_resized, sigma=1)
|
| 116 |
|
|
@@ -120,6 +114,14 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 120 |
(depth_image_smoothed.max() - depth_image_smoothed.min()) * 65535
|
| 121 |
).astype(np.uint16)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
# Step 4: Convert the normalized depth image to Open3D Image object
|
| 124 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 125 |
image_o3d = o3d.geometry.Image(foreground)
|
|
|
|
| 98 |
import cv2 # Make sure OpenCV is installed
|
| 99 |
|
| 100 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Step 1: Resize depth_image with PIL using bilinear interpolation for smoother resizing
|
| 102 |
depth_image_resized = np.array(
|
| 103 |
+
Image.fromarray(depth_image.astype(np.uint16), mode='I;16').resize(
|
| 104 |
foreground.shape[:2][::-1], Image.Resampling.BILINEAR
|
| 105 |
)
|
| 106 |
)
|
| 107 |
+
|
| 108 |
# Step 2: Apply a slight Gaussian blur to the depth map to smooth out artifacts
|
| 109 |
depth_image_smoothed = gaussian_filter(depth_image_resized, sigma=1)
|
| 110 |
|
|
|
|
| 114 |
(depth_image_smoothed.max() - depth_image_smoothed.min()) * 65535
|
| 115 |
).astype(np.uint16)
|
| 116 |
|
| 117 |
+
# Check for shape and dtype before creating PIL Image
|
| 118 |
+
if depth_image_normalized.ndim != 2 or depth_image_normalized.shape[0] == 0 or depth_image_normalized.shape[1] == 0:
|
| 119 |
+
raise ValueError("Normalized depth image is not valid. Shape: {}".format(depth_image_normalized.shape))
|
| 120 |
+
|
| 121 |
+
# Check dtype
|
| 122 |
+
if depth_image_normalized.dtype != np.uint16:
|
| 123 |
+
raise ValueError("Depth image is not of type uint16, but rather: {}".format(depth_image_normalized.dtype))
|
| 124 |
+
|
| 125 |
# Step 4: Convert the normalized depth image to Open3D Image object
|
| 126 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 127 |
image_o3d = o3d.geometry.Image(foreground)
|