codic commited on
Commit
a3c5d97
·
verified ·
1 Parent(s): 5fcdff5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -100,6 +100,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
100
  # Resize background to match the original image size (no cropping)
101
  background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
102
 
 
103
  depth_image_resized = np.array(
104
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
105
  )
@@ -108,12 +109,10 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
108
  depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
109
  depth_image_resized.max() - depth_image_resized.min()
110
  )
111
-
112
- # Convert to float32 for depth processing
113
  depth_image_normalized = depth_image_normalized.astype(np.float32)
114
 
115
  # Create flat background depth with a small gap for empty space
116
- background_depth = np.zeros_like(depth_image_normalized) + 1.5 # Adjust value for the empty space
117
 
118
  # Combine depths to create the illusion of an empty space
119
  combined_depth = np.maximum(depth_image_normalized * 0.5, background_depth)
@@ -123,36 +122,33 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
123
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
124
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
125
  )
126
- w, h = foreground.shape[1], foreground.shape[0]
127
 
 
 
128
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
129
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
130
 
131
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
132
 
133
- # Check if the point cloud has points
134
- if len(pcd.points) == 0:
135
- raise RuntimeError("Point cloud is empty. Check the depth image and foreground.")
 
136
 
137
- # Orient normals and smooth
138
- if not pcd.has_normals():
139
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
140
 
141
- if pcd.has_normals():
142
- pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
143
- else:
144
- raise RuntimeError("Failed to estimate normals for the point cloud.")
145
 
146
- # Generate 3D mesh with smoothing and flatten back
147
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
148
  pcd, depth=depth, width=0, scale=1.2, linear_fit=True
149
  )
150
 
151
- # Check if the raw mesh has triangles
152
- if not mesh_raw.has_triangles():
153
- raise RuntimeError("No triangles found in the generated mesh. Check the point cloud data.")
154
-
155
- # Simplify mesh for better performance
156
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
157
  mesh = mesh_raw.simplify_vertex_clustering(
158
  voxel_size=voxel_size,
@@ -160,18 +156,22 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
160
  )
161
  mesh = mesh.filter_smooth_simple(number_of_iterations=3)
162
 
163
- # Validate and crop the mesh
 
 
 
 
164
  bbox = pcd.get_axis_aligned_bounding_box()
165
-
166
- # Check if bounding box is valid
167
  if bbox.is_empty():
168
  raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
169
 
170
- # Perform cropping
171
  mesh_crop = mesh.crop(bbox)
172
 
 
173
  gltf_path = f"./{image_path.stem}.gltf"
174
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
 
175
  return gltf_path
176
 
177
  # Gradio Interface
 
100
  # Resize background to match the original image size (no cropping)
101
  background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
102
 
103
+ # Resize and normalize the depth image
104
  depth_image_resized = np.array(
105
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
106
  )
 
109
  depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
110
  depth_image_resized.max() - depth_image_resized.min()
111
  )
 
 
112
  depth_image_normalized = depth_image_normalized.astype(np.float32)
113
 
114
  # Create flat background depth with a small gap for empty space
115
+ background_depth = np.zeros_like(depth_image_normalized) + 1.5
116
 
117
  # Combine depths to create the illusion of an empty space
118
  combined_depth = np.maximum(depth_image_normalized * 0.5, background_depth)
 
122
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
123
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
124
  )
 
125
 
126
+ # Debugging: Check point cloud creation
127
+ w, h = foreground.shape[1], foreground.shape[0]
128
  camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
129
  camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
130
 
131
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
132
 
133
+ # Debugging: Print point cloud size
134
+ print(f"Point Cloud Size: {len(pcd.points)}")
135
+ if len(pcd.points) < 100:
136
+ raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
137
 
138
+ # Ensure the depth image is valid
139
+ if np.count_nonzero(depth_image_normalized) == 0:
140
+ raise RuntimeError("Depth image contains only zeros. Please check the input.")
141
 
142
+ # Estimate normals and orient
143
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
144
+ pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
 
145
 
146
+ # Generate 3D mesh using Poisson reconstruction
147
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
148
  pcd, depth=depth, width=0, scale=1.2, linear_fit=True
149
  )
150
 
151
+ # Simplify and smooth the mesh
 
 
 
 
152
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
153
  mesh = mesh_raw.simplify_vertex_clustering(
154
  voxel_size=voxel_size,
 
156
  )
157
  mesh = mesh.filter_smooth_simple(number_of_iterations=3)
158
 
159
+ # Validate the mesh before cropping
160
+ if not mesh.has_triangles():
161
+ raise RuntimeError("No triangles found in the generated mesh. Check the point cloud data.")
162
+
163
+ # Crop and align model based on the bounding box
164
  bbox = pcd.get_axis_aligned_bounding_box()
 
 
165
  if bbox.is_empty():
166
  raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
167
 
168
+ # Crop the mesh based on the bounding box
169
  mesh_crop = mesh.crop(bbox)
170
 
171
+ # Write the final 3D mesh to a GLTF file
172
  gltf_path = f"./{image_path.stem}.gltf"
173
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
174
+
175
  return gltf_path
176
 
177
  # Gradio Interface