codic commited on
Commit
7167dc0
·
verified ·
1 Parent(s): c7300ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -98,38 +98,38 @@ 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: 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(
135
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
 
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 in the correct format (2D array) and dtype
102
+ if depth_image.ndim != 2:
103
+ raise ValueError("Depth image should be a 2D array, but got: {}".format(depth_image.shape))
104
+
105
  depth_image = depth_image.astype(np.uint16)
106
 
107
+ # Create a PIL image from the depth image
108
+ try:
109
+ depth_image_pil = Image.fromarray(depth_image, mode='I;16')
110
+ print(f"Depth image mode: {depth_image_pil.mode}") # Debug: Check mode after creation
111
+ except Exception as e:
112
+ raise ValueError(f"Error creating PIL image from depth array: {e}")
113
+
114
+ # Convert to 'L' mode if resizing fails with 'I;16' mode
115
+ if depth_image_pil.mode != 'L':
116
+ depth_image_pil = depth_image_pil.convert('L')
117
+ print(f"Converted depth image mode to: {depth_image_pil.mode}") # Debug
118
 
119
+ # Resize depth image
120
  depth_image_resized = depth_image_pil.resize(
121
+ foreground.shape[1::-1], Image.Resampling.BILINEAR
122
  )
123
 
124
+ # Convert back to numpy array
125
+ depth_image_resized_np = np.array(depth_image_resized)
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ # Check for valid shape and dtype
128
+ if depth_image_resized_np.ndim != 2 or depth_image_resized_np.shape[0] == 0 or depth_image_resized_np.shape[1] == 0:
129
+ raise ValueError(f"Resized depth image is not valid. Shape: {depth_image_resized_np.shape}")
130
 
131
+ # Create Open3D image from depth
132
+ depth_o3d = o3d.geometry.Image(depth_image_resized_np)
133
  image_o3d = o3d.geometry.Image(foreground)
134
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
135
  image_o3d, depth_o3d, convert_rgb_to_intensity=False