codic commited on
Commit
22f4ff7
·
verified ·
1 Parent(s): 353bd0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -96,6 +96,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
96
  # Ensure depth is at least 2 for Open3D compatibility
97
  depth = max(depth, 2)
98
 
 
99
  depth_image_resized = np.array(
100
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
101
  )
@@ -104,34 +105,35 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
104
  ) * 255
105
  depth_image_normalized = depth_image_normalized.astype(np.uint8)
106
 
 
107
  depth_o3d = o3d.geometry.Image(depth_image_normalized)
108
  image_o3d = o3d.geometry.Image(foreground)
109
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
110
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
111
  )
112
- w, h = foreground.shape[1], foreground.shape[0]
113
 
 
 
114
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
115
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
116
 
 
117
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
118
 
 
119
  if len(pcd.points) < 100:
120
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
121
-
122
- if not pcd.has_normals():
123
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=50))
124
 
125
- if pcd.has_normals():
126
- pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
127
- else:
128
- raise RuntimeError("Failed to estimate normals for the point cloud.")
129
 
 
130
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
131
  pcd, depth=depth, width=1, scale=2, linear_fit=True
132
  )
133
 
134
- # Flatten the back of the model from the zero point
135
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
136
  mesh = mesh_raw.simplify_vertex_clustering(
137
  voxel_size=voxel_size,
@@ -141,10 +143,10 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
141
  bbox = pcd.get_axis_aligned_bounding_box()
142
  mesh_crop = mesh.crop(bbox)
143
 
 
144
  gltf_path = f"./{image_path.stem}.gltf"
145
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
146
  return gltf_path
147
-
148
  # Gradio Interface
149
  title = "Depth Estimation & 3D Reconstruction Demo"
150
  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."
 
96
  # Ensure depth is at least 2 for Open3D compatibility
97
  depth = max(depth, 2)
98
 
99
+ # Resize and normalize depth image
100
  depth_image_resized = np.array(
101
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
102
  )
 
105
  ) * 255
106
  depth_image_normalized = depth_image_normalized.astype(np.uint8)
107
 
108
+ # Convert to Open3D images
109
  depth_o3d = o3d.geometry.Image(depth_image_normalized)
110
  image_o3d = o3d.geometry.Image(foreground)
111
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
112
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
113
  )
 
114
 
115
+ # Camera intrinsic setup
116
+ w, h = foreground.shape[1], foreground.shape[0]
117
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
118
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
119
 
120
+ # Create point cloud from RGBD image
121
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
122
 
123
+ # Check for point cloud validity
124
  if len(pcd.points) < 100:
125
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
 
 
 
126
 
127
+ # Estimate and orient normals
128
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=50))
129
+ pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
 
130
 
131
+ # Poisson surface reconstruction with enforced depth
132
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
133
  pcd, depth=depth, width=1, scale=2, linear_fit=True
134
  )
135
 
136
+ # Simplify and crop mesh
137
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
138
  mesh = mesh_raw.simplify_vertex_clustering(
139
  voxel_size=voxel_size,
 
143
  bbox = pcd.get_axis_aligned_bounding_box()
144
  mesh_crop = mesh.crop(bbox)
145
 
146
+ # Save to glTF
147
  gltf_path = f"./{image_path.stem}.gltf"
148
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
149
  return gltf_path
 
150
  # Gradio Interface
151
  title = "Depth Estimation & 3D Reconstruction Demo"
152
  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."