codic commited on
Commit
1ff455f
·
verified ·
1 Parent(s): c33d486

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -90,6 +90,11 @@ def process_image(image_path, depth_map_path=None):
90
  img = Image.fromarray(blended_depth_image)
91
  return [img, gltf_path, gltf_path]
92
 
 
 
 
 
 
93
  def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
94
  # Resize depth_image to match the size of the foreground
95
  depth_image_resized = np.array(
@@ -97,23 +102,28 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
97
  )
98
 
99
  # Normalize depth_image_resized to the range [0, 1]
100
- depth_image_resized = (depth_image_resized - np.min(depth_image_resized)) / (
101
- np.max(depth_image_resized) - np.min(depth_image_resized)
102
- )
 
 
 
 
103
  depth_image_resized = np.clip(depth_image_resized, 0, 1) # Ensure values are between 0 and 1
104
 
105
- # Convert the normalized image to float format
106
  depth_image_float = img_as_float(depth_image_resized)
107
 
108
- # Ensure the float image is between -1 and 1 before converting to uint8
109
  if np.min(depth_image_float) < -1 or np.max(depth_image_float) > 1:
110
- raise ValueError("Depth image must be in the range [-1, 1] before conversion to uint8.")
 
111
 
112
  # Convert to 8-bit unsigned integers
113
- depth_image_resized = img_as_ubyte(depth_image_float)
114
 
115
  # Create Open3D image from the depth and foreground images
116
- depth_o3d = o3d.geometry.Image(depth_image_resized)
117
  image_o3d = o3d.geometry.Image(foreground)
118
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
119
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
@@ -160,6 +170,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
160
 
161
  return gltf_path
162
 
 
163
  # Gradio Interface
164
  title = "Depth Estimation & 3D Reconstruction Demo"
165
  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."
 
90
  img = Image.fromarray(blended_depth_image)
91
  return [img, gltf_path, gltf_path]
92
 
93
+ import numpy as np
94
+ import open3d as o3d
95
+ from PIL import Image
96
+ from skimage.util import img_as_ubyte, img_as_float
97
+
98
  def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
99
  # Resize depth_image to match the size of the foreground
100
  depth_image_resized = np.array(
 
102
  )
103
 
104
  # Normalize depth_image_resized to the range [0, 1]
105
+ min_depth = np.min(depth_image_resized)
106
+ max_depth = np.max(depth_image_resized)
107
+
108
+ if max_depth - min_depth == 0:
109
+ raise ValueError("Depth image has no variation; cannot normalize.")
110
+
111
+ depth_image_resized = (depth_image_resized - min_depth) / (max_depth - min_depth)
112
  depth_image_resized = np.clip(depth_image_resized, 0, 1) # Ensure values are between 0 and 1
113
 
114
+ # Convert the normalized float image to float format
115
  depth_image_float = img_as_float(depth_image_resized)
116
 
117
+ # Check the range of the depth image after conversion to float
118
  if np.min(depth_image_float) < -1 or np.max(depth_image_float) > 1:
119
+ raise ValueError(f"Depth image values after float conversion must be in the range [-1, 1]: "
120
+ f"min={np.min(depth_image_float)}, max={np.max(depth_image_float)}")
121
 
122
  # Convert to 8-bit unsigned integers
123
+ depth_image_resized_uint8 = img_as_ubyte(depth_image_float)
124
 
125
  # Create Open3D image from the depth and foreground images
126
+ depth_o3d = o3d.geometry.Image(depth_image_resized_uint8)
127
  image_o3d = o3d.geometry.Image(foreground)
128
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
129
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
 
170
 
171
  return gltf_path
172
 
173
+
174
  # Gradio Interface
175
  title = "Depth Estimation & 3D Reconstruction Demo"
176
  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."