Update app.py
Browse files
app.py
CHANGED
|
@@ -98,31 +98,37 @@ 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 |
-
# Step 1:
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
|
|
|
| 109 |
depth_image_smoothed = gaussian_filter(depth_image_resized, sigma=1)
|
| 110 |
|
| 111 |
-
# Step
|
| 112 |
depth_image_normalized = (
|
| 113 |
-
(depth_image_smoothed - depth_image_smoothed.min()) /
|
| 114 |
(depth_image_smoothed.max() - depth_image_smoothed.min()) * 65535
|
| 115 |
).astype(np.uint16)
|
| 116 |
|
| 117 |
-
# Check for shape and dtype before creating
|
| 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
|
| 126 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 127 |
image_o3d = o3d.geometry.Image(foreground)
|
| 128 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 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: Ensure depth_image is in the right format
|
| 102 |
+
depth_image = depth_image.astype(np.uint16)
|
| 103 |
+
|
| 104 |
+
# Step 2: Create a PIL image from the depth image
|
| 105 |
+
depth_image_pil = Image.fromarray(depth_image, mode='I;16')
|
| 106 |
+
|
| 107 |
+
# Step 3: Resize depth_image with PIL using bilinear interpolation
|
| 108 |
+
depth_image_resized = depth_image_pil.resize(
|
| 109 |
+
foreground.shape[:2][::-1], Image.Resampling.BILINEAR
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Convert back to a numpy array after resizing
|
| 113 |
+
depth_image_resized = np.array(depth_image_resized)
|
| 114 |
+
|
| 115 |
+
# Step 4: Apply a slight Gaussian blur to the depth map to smooth out artifacts
|
| 116 |
depth_image_smoothed = gaussian_filter(depth_image_resized, sigma=1)
|
| 117 |
|
| 118 |
+
# Step 5: Normalize the depth image to 0-65535 range without over-compressing details
|
| 119 |
depth_image_normalized = (
|
| 120 |
+
(depth_image_smoothed - depth_image_smoothed.min()) /
|
| 121 |
(depth_image_smoothed.max() - depth_image_smoothed.min()) * 65535
|
| 122 |
).astype(np.uint16)
|
| 123 |
|
| 124 |
+
# Check for shape and dtype before creating Open3D Image object
|
| 125 |
if depth_image_normalized.ndim != 2 or depth_image_normalized.shape[0] == 0 or depth_image_normalized.shape[1] == 0:
|
| 126 |
raise ValueError("Normalized depth image is not valid. Shape: {}".format(depth_image_normalized.shape))
|
| 127 |
+
|
|
|
|
| 128 |
if depth_image_normalized.dtype != np.uint16:
|
| 129 |
raise ValueError("Depth image is not of type uint16, but rather: {}".format(depth_image_normalized.dtype))
|
| 130 |
|
| 131 |
+
# Step 6: Convert the normalized depth image to Open3D Image object
|
| 132 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 133 |
image_o3d = o3d.geometry.Image(foreground)
|
| 134 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|