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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -97,26 +97,26 @@ def process_image(image_path, depth_map_path=None):
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()
121
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
122
 
@@ -127,7 +127,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
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:
@@ -149,7 +149,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
149
  gltf_path = f"./{image_path.stem}.gltf"
150
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
151
  return gltf_path
152
-
153
  # Gradio Interface
154
  title = "Depth Estimation & 3D Reconstruction Demo"
155
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
 
97
  import cv2 # Make sure OpenCV is installed
98
 
99
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
100
+ # Resize depth_image with PIL while retaining uint16 detail
101
+ depth_image_resized = np.array(
102
+ Image.fromarray(depth_image.astype(np.uint16), mode='I;16').resize(
103
+ foreground.shape[:2][::-1], Image.Resampling.NEAREST
104
+ )
105
+ )
106
 
107
+ # Normalize depth_image to 0-255 range for Open3D compatibility while preserving uint16 precision
108
+ depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
109
+ (depth_image_resized.max() - depth_image_resized.min()) * 65535).astype(np.uint16)
110
 
111
+ # Convert the depth image to an Open3D Image object
112
+ depth_o3d = o3d.geometry.Image(depth_image_normalized)
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
+
118
+ # Remaining steps unchanged...
119
  w, h = foreground.shape[1], foreground.shape[0]
 
120
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
121
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
122
 
 
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:
 
149
  gltf_path = f"./{image_path.stem}.gltf"
150
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
151
  return gltf_path
152
+
153
  # Gradio Interface
154
  title = "Depth Estimation & 3D Reconstruction Demo"
155
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."