codic commited on
Commit
3ab28df
·
verified ·
1 Parent(s): a4e945b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -94,26 +94,27 @@ def process_image(image_path, depth_map_path=None):
94
  return [img, gltf_path, gltf_path]
95
 
96
 
 
 
97
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
98
- # Resize depth image, specifying 'I;16' mode for uint16 compatibility
99
- depth_image_resized = np.array(
100
- Image.fromarray(depth_image, mode='I;16').resize(foreground.shape[:2][::-1], Image.LANCZOS)
101
- )
102
-
103
- # Normalize and convert to uint16 if it needs to stay uint16
104
- depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
105
- depth_image_resized.max() - depth_image_resized.min()
106
- ) * 65535
107
- depth_image_normalized = depth_image_normalized.astype(np.uint16)
108
 
109
- # Convert to uint8 for compatibility with Open3D if necessary
110
- depth_image_uint8 = img_as_ubyte(depth_image_normalized / 65535.0) # Scale to [0, 1] for uint8 conversion
 
111
 
112
- depth_o3d = o3d.geometry.Image(depth_image_uint8)
113
  image_o3d = o3d.geometry.Image(foreground)
114
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
115
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
116
  )
 
 
117
  w, h = foreground.shape[1], foreground.shape[0]
118
 
119
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
@@ -126,7 +127,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
126
 
127
  if not pcd.has_normals():
128
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
129
-
130
  if pcd.has_normals():
131
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
132
  else:
 
94
  return [img, gltf_path, gltf_path]
95
 
96
 
97
+ import cv2 # Make sure OpenCV is installed
98
+
99
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
100
+ # Convert depth_image to uint8 if 16-bit isn't required
101
+ if depth_image.dtype == np.uint16:
102
+ depth_image = (depth_image / 256).astype(np.uint8) # Convert to uint8
103
+
104
+ # Resize using OpenCV to maintain compatibility with 16-bit images if needed
105
+ resized_depth_image = cv2.resize(depth_image, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LANCZOS4)
 
 
 
 
106
 
107
+ # Convert resized depth back to uint8 if necessary for Open3D compatibility
108
+ if resized_depth_image.dtype != np.uint8:
109
+ resized_depth_image = (resized_depth_image / 65535.0 * 255).astype(np.uint8) # Scale and convert to uint8
110
 
111
+ depth_o3d = o3d.geometry.Image(resized_depth_image)
112
  image_o3d = o3d.geometry.Image(foreground)
113
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
114
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
115
  )
116
+
117
+ # Continue with the rest of the Open3D operations
118
  w, h = foreground.shape[1], foreground.shape[0]
119
 
120
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
 
127
 
128
  if not pcd.has_normals():
129
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
130
+
131
  if pcd.has_normals():
132
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
133
  else: