codic commited on
Commit
ad25aab
·
verified ·
1 Parent(s): 745805f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -73,24 +73,29 @@ def process_image(image_path, depth_map_path=None):
73
  raise Exception("Error reconstructing 3D model")
74
 
75
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
76
- depth_o3d = o3d.geometry.Image(depth_image)
 
 
 
 
 
77
  image_o3d = o3d.geometry.Image(rgb_image)
78
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
79
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
80
  )
81
- w, h = depth_image.shape[1], depth_image.shape[0]
82
 
83
- # Camera intrinsic setup for 3D point cloud
84
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
85
  camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
86
 
87
  # Create Point Cloud from RGBD Image
88
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
89
 
90
- # Ensure normals are estimated before trying to orient them
91
  if not pcd.has_normals():
92
- print("Estimating normals for point cloud...")
93
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30))
94
 
95
  # Confirm if normals are present
96
  if pcd.has_normals():
@@ -99,10 +104,6 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
99
  else:
100
  raise RuntimeError("Failed to estimate normals for the point cloud.")
101
 
102
- # Transformations
103
- pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
104
- pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
105
-
106
  # Poisson Surface Reconstruction
107
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
108
  pcd, depth=depth, width=0, scale=1.1, linear_fit=True
@@ -123,7 +124,6 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
123
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
124
  return gltf_path
125
 
126
-
127
  # Gradio Interface
128
  title = "Depth Estimation & 3D Reconstruction Demo"
129
  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."
 
73
  raise Exception("Error reconstructing 3D model")
74
 
75
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
76
+ # Resize depth image to match the RGB image dimensions if needed
77
+ depth_image_resized = np.array(
78
+ Image.fromarray(depth_image).resize(rgb_image.shape[:2][::-1], Image.NEAREST)
79
+ )
80
+
81
+ depth_o3d = o3d.geometry.Image(depth_image_resized)
82
  image_o3d = o3d.geometry.Image(rgb_image)
83
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
84
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
85
  )
86
+ w, h = rgb_image.shape[1], rgb_image.shape[0]
87
 
88
+ # Camera intrinsic setup
89
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
90
  camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
91
 
92
  # Create Point Cloud from RGBD Image
93
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
94
 
95
+ # Ensure normals are estimated before orienting them
96
  if not pcd.has_normals():
97
+ print("Estimating normals with increased search radius for sparse data...")
98
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=50))
99
 
100
  # Confirm if normals are present
101
  if pcd.has_normals():
 
104
  else:
105
  raise RuntimeError("Failed to estimate normals for the point cloud.")
106
 
 
 
 
 
107
  # Poisson Surface Reconstruction
108
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
109
  pcd, depth=depth, width=0, scale=1.1, linear_fit=True
 
124
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
125
  return gltf_path
126
 
 
127
  # Gradio Interface
128
  title = "Depth Estimation & 3D Reconstruction Demo"
129
  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."