codic commited on
Commit
95a1586
·
verified ·
1 Parent(s): e1f5912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -91,7 +91,8 @@ def process_image(image_path, depth_map_path=None):
91
  img = Image.fromarray(depth_display)
92
  return [img, gltf_path, gltf_path]
93
 
94
- def create_3d_obj(foreground, depth_image, image_path, depth=12):
 
95
  """
96
  Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
97
 
@@ -100,6 +101,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12):
100
  depth_image (np.array): The depth map array for the main subject only.
101
  image_path (str): Path to save the output model.
102
  depth (int): Poisson reconstruction depth value for high detail.
 
103
 
104
  Returns:
105
  str: Path to the saved 3D model (.gltf).
@@ -112,8 +114,20 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12):
112
  depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
113
  (depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
114
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  # Create Open3D RGBD image
116
- depth_o3d = o3d.geometry.Image(depth_image_normalized)
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
 
91
  img = Image.fromarray(depth_display)
92
  return [img, gltf_path, gltf_path]
93
 
94
+
95
+ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
96
  """
97
  Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
98
 
 
101
  depth_image (np.array): The depth map array for the main subject only.
102
  image_path (str): Path to save the output model.
103
  depth (int): Poisson reconstruction depth value for high detail.
104
+ edge_strength (float): Strength of the edge thresholding for sharper subject boundaries.
105
 
106
  Returns:
107
  str: Path to the saved 3D model (.gltf).
 
114
  depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
115
  (depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
116
 
117
+ # Apply edge detection to create sharper boundaries around the main subject
118
+ sobel_x = sobel(depth_image_normalized, axis=0)
119
+ sobel_y = sobel(depth_image_normalized, axis=1)
120
+ gradient_magnitude = np.hypot(sobel_x, sobel_y)
121
+
122
+ # Threshold the gradient magnitude to detect edges
123
+ edge_mask = gradient_magnitude > (gradient_magnitude.max() * edge_strength)
124
+
125
+ # Apply the edge mask to the depth map, removing blur around edges
126
+ depth_image_sharpened = np.copy(depth_image_normalized)
127
+ depth_image_sharpened[~edge_mask] = 0 # Remove non-edge areas by setting them to 0 (background)
128
+
129
  # Create Open3D RGBD image
130
+ depth_o3d = o3d.geometry.Image(depth_image_sharpened)
131
  image_o3d = o3d.geometry.Image(foreground)
132
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
133
  image_o3d, depth_o3d, convert_rgb_to_intensity=False