codic commited on
Commit
644965a
·
verified ·
1 Parent(s): 61094fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -63,10 +63,11 @@ def process_image(image_path, depth_map_path=None):
63
  return [img, gltf_path, gltf_path]
64
 
65
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
 
66
  depth_image_resized = np.array(
67
  Image.fromarray(depth_image).resize(rgb_image.shape[:2][::-1], Image.NEAREST)
68
  )
69
-
70
  depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
71
  depth_image_resized.max() - depth_image_resized.min()
72
  )
@@ -80,37 +81,43 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
80
  )
81
  w, h = rgb_image.shape[1], rgb_image.shape[0]
82
 
 
83
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
84
  camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
85
 
 
86
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
87
 
 
 
 
 
 
88
  if not pcd.has_normals():
89
- print("Estimating normals...")
90
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30))
91
 
92
  if pcd.has_normals():
93
- print("Orienting normals towards camera location...")
94
- pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 0.0])) # Adjusted camera position
 
95
 
96
- # Apply Poisson Surface Reconstruction with adjusted depth for better detail
97
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
98
  pcd, depth=depth, width=0, scale=1.1, linear_fit=True
99
  )
100
 
101
- # Mesh simplification and smoothing
102
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
103
  mesh = mesh_raw.simplify_vertex_clustering(
104
  voxel_size=voxel_size,
105
  contraction=o3d.geometry.SimplificationContraction.Average,
106
  )
107
- mesh = mesh.filter_smooth_simple(number_of_iterations=3)
108
- bbox = pcd.get_axis_aligned_bounding_box()
109
- mesh_crop = mesh.crop(bbox)
110
 
111
- # Save GLTF
112
  gltf_path = f"./{image_path.stem}.gltf"
113
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
114
  return gltf_path
115
 
116
  # Gradio Interface
 
63
  return [img, gltf_path, gltf_path]
64
 
65
  def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
66
+ # Resize and normalize the depth image
67
  depth_image_resized = np.array(
68
  Image.fromarray(depth_image).resize(rgb_image.shape[:2][::-1], Image.NEAREST)
69
  )
70
+ # Invert depth values
71
  depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
72
  depth_image_resized.max() - depth_image_resized.min()
73
  )
 
81
  )
82
  w, h = rgb_image.shape[1], rgb_image.shape[0]
83
 
84
+ # Camera intrinsic setup
85
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
86
  camera_intrinsic.set_intrinsics(w, h, 1000, 1000, w / 2, h / 2)
87
 
88
+ # Create Point Cloud from RGBD Image
89
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
90
 
91
+ # Check if the point cloud has enough points
92
+ if len(pcd.points) < 100:
93
+ raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
94
+
95
+ # Estimate and orient normals
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=30))
99
 
100
  if pcd.has_normals():
101
+ pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1000.0]))
102
+ else:
103
+ raise RuntimeError("Failed to estimate normals for the point cloud.")
104
 
105
+ # Poisson Surface Reconstruction with a suitable depth
106
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
107
  pcd, depth=depth, width=0, scale=1.1, linear_fit=True
108
  )
109
 
110
+ # Increase mesh quality by reducing voxel size
111
+ voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256 # Reduced for higher detail
112
  mesh = mesh_raw.simplify_vertex_clustering(
113
  voxel_size=voxel_size,
114
  contraction=o3d.geometry.SimplificationContraction.Average,
115
  )
116
+ mesh = mesh.filter_smooth_simple(number_of_iterations=5)
 
 
117
 
118
+ # Save the mesh without cropping
119
  gltf_path = f"./{image_path.stem}.gltf"
120
+ o3d.io.write_triangle_mesh(gltf_path, mesh, write_triangle_uvs=True)
121
  return gltf_path
122
 
123
  # Gradio Interface