codic commited on
Commit
873ee75
·
verified ·
1 Parent(s): ef3100a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -108,23 +108,24 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
108
  str: Path to the saved 3D model (.gltf).
109
  """
110
 
111
- # Resize and normalize depth image to align with the foreground
112
  depth_image_resized = np.array(
113
  Image.fromarray((depth_image * 255).astype(np.uint8)).resize(foreground.shape[:2][::-1], Image.LANCZOS)
114
  )
 
 
115
  depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
116
  (depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
117
-
118
- # Apply edge detection to create sharper boundaries around the main subject
119
  sobel_x = sobel(depth_image_normalized, axis=0)
120
  sobel_y = sobel(depth_image_normalized, axis=1)
121
  gradient_magnitude = np.hypot(sobel_x, sobel_y)
122
-
123
- # Threshold the gradient magnitude to detect edges, then smooth out with morphological filtering
124
- edge_mask = gradient_magnitude > (gradient_magnitude.max() * edge_strength)
125
- edge_mask = np.logical_and(depth_image_normalized > 10, edge_mask) # Ensure main subject is prioritized
126
-
127
- # Apply the edge mask to the depth map, keeping main subject depth values
128
  depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
129
 
130
  # Create Open3D RGBD image
@@ -138,20 +139,20 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
138
  w, h = foreground.shape[1], foreground.shape[0]
139
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(w, h, 2000, 2000, w / 2, h / 2)
140
 
141
- # Generate point cloud and apply strict filtering on depth
142
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
143
 
144
- # Ensure minimum point count for robust 3D reconstruction
145
  if len(pcd.points) < 100:
146
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
147
 
148
- # Estimate normals with refined parameters for detail
149
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
150
 
151
  # Orient normals towards a distant camera position
152
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
153
 
154
- # Create Poisson mesh with main subject-only depth
155
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
156
  pcd, depth=depth, width=0, scale=2, linear_fit=True
157
  )
@@ -163,7 +164,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
163
  contraction=o3d.geometry.SimplificationContraction.Average,
164
  )
165
 
166
- # Apply Laplacian smoothing to improve surface quality
167
  mesh = mesh.filter_smooth_laplacian(number_of_iterations=4)
168
 
169
  # Crop mesh to bounding box around main subject only
 
108
  str: Path to the saved 3D model (.gltf).
109
  """
110
 
111
+ # Resize and normalize depth image to align with the foreground dimensions
112
  depth_image_resized = np.array(
113
  Image.fromarray((depth_image * 255).astype(np.uint8)).resize(foreground.shape[:2][::-1], Image.LANCZOS)
114
  )
115
+
116
+ # Normalize depth map and set background to zero (remove background noise)
117
  depth_image_normalized = ((depth_image_resized - depth_image_resized.min()) /
118
  (depth_image_resized.max() - depth_image_resized.min()) * 255.0).astype(np.float32)
119
+
120
+ # Apply edge detection to enhance the subject’s edges
121
  sobel_x = sobel(depth_image_normalized, axis=0)
122
  sobel_y = sobel(depth_image_normalized, axis=1)
123
  gradient_magnitude = np.hypot(sobel_x, sobel_y)
124
+
125
+ # Generate an edge mask that captures only strong edges
126
+ edge_mask = gradient_magnitude < (gradient_magnitude.max() * edge_strength)
127
+
128
+ # Keep depth values only within the edge mask area (isolating the main subject)
 
129
  depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
130
 
131
  # Create Open3D RGBD image
 
139
  w, h = foreground.shape[1], foreground.shape[0]
140
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(w, h, 2000, 2000, w / 2, h / 2)
141
 
142
+ # Generate point cloud and apply depth-focused filtering
143
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
144
 
145
+ # Check minimum point count for normals estimation
146
  if len(pcd.points) < 100:
147
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
148
 
149
+ # Estimate normals with refined parameters
150
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
151
 
152
  # Orient normals towards a distant camera position
153
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
154
 
155
+ # Create Poisson mesh using main subject's isolated depth
156
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
157
  pcd, depth=depth, width=0, scale=2, linear_fit=True
158
  )
 
164
  contraction=o3d.geometry.SimplificationContraction.Average,
165
  )
166
 
167
+ # Apply Laplacian smoothing
168
  mesh = mesh.filter_smooth_laplacian(number_of_iterations=4)
169
 
170
  # Crop mesh to bounding box around main subject only