Update app.py
Browse files
app.py
CHANGED
|
@@ -54,66 +54,62 @@ def process_image(image_path, depth_map_path=None):
|
|
| 54 |
).squeeze()
|
| 55 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 56 |
|
| 57 |
-
# Resize and apply lighter Gaussian smoothing
|
| 58 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 59 |
-
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur for
|
| 60 |
|
| 61 |
# Ensure matching size and single channel for blending
|
| 62 |
if len(depth_image.shape) == 3:
|
| 63 |
depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
|
| 64 |
-
|
| 65 |
if len(mask_blurred.shape) == 3:
|
| 66 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 67 |
-
|
| 68 |
-
# Resize mask_blurred to match depth_image dimensions
|
| 69 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 70 |
|
| 71 |
-
# Blend depth_image with mask_blurred_resized
|
| 72 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
-
gltf_path = create_3d_obj(np.array(
|
| 76 |
img = Image.fromarray(blended_depth_image)
|
| 77 |
return [img, gltf_path, gltf_path]
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 80 |
-
gltf_path = create_3d_obj(np.array(
|
| 81 |
img = Image.fromarray(blended_depth_image)
|
| 82 |
return [img, gltf_path, gltf_path]
|
| 83 |
|
| 84 |
|
| 85 |
-
def create_3d_obj(
|
| 86 |
-
# Resize and normalize the depth image
|
| 87 |
depth_image_resized = np.array(
|
| 88 |
-
Image.fromarray(depth_image).resize(
|
| 89 |
)
|
| 90 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 91 |
depth_image_resized.max() - depth_image_resized.min()
|
| 92 |
-
)
|
| 93 |
-
depth_image_normalized = depth_image_normalized
|
| 94 |
-
depth_image_inverted = depth_image_normalized.astype(np.uint8)
|
| 95 |
|
| 96 |
-
depth_o3d = o3d.geometry.Image(
|
| 97 |
-
image_o3d = o3d.geometry.Image(
|
| 98 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 99 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 100 |
)
|
| 101 |
-
w, h =
|
| 102 |
|
| 103 |
-
# Camera intrinsic setup
|
| 104 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 105 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 106 |
|
| 107 |
-
# Create Point Cloud from RGBD Image
|
| 108 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 109 |
|
| 110 |
-
# Check if the point cloud has enough points
|
| 111 |
if len(pcd.points) < 100:
|
| 112 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 113 |
|
| 114 |
-
# Estimate and orient normals
|
| 115 |
if not pcd.has_normals():
|
| 116 |
-
print("Estimating normals with increased search radius for sparse data...")
|
| 117 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 118 |
|
| 119 |
if pcd.has_normals():
|
|
@@ -121,23 +117,20 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 121 |
else:
|
| 122 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 123 |
|
| 124 |
-
# Poisson Surface Reconstruction
|
| 125 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 126 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 127 |
)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400 # Decreased voxel size for finer detail
|
| 131 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 132 |
voxel_size=voxel_size,
|
| 133 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 134 |
)
|
| 135 |
-
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 136 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 137 |
mesh_crop = mesh.crop(bbox)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
gltf_path = f"./{image_path.stem}.gltf" # Ensure to use .gltf extension and proper file naming
|
| 141 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 142 |
return gltf_path
|
| 143 |
|
|
|
|
| 54 |
).squeeze()
|
| 55 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 56 |
|
| 57 |
+
# Resize and apply lighter Gaussian smoothing for smoother transitions
|
| 58 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 59 |
+
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur for transition
|
| 60 |
|
| 61 |
# Ensure matching size and single channel for blending
|
| 62 |
if len(depth_image.shape) == 3:
|
| 63 |
depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 64 |
if len(mask_blurred.shape) == 3:
|
| 65 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 66 |
+
|
|
|
|
| 67 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 68 |
|
| 69 |
+
# Blend depth_image with mask_blurred_resized for smoother transition
|
| 70 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 71 |
|
| 72 |
+
# Inpaint background where main subject was removed
|
| 73 |
+
background = remove(image_raw, only_mask=True)
|
| 74 |
+
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 75 |
+
background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
| 76 |
+
|
| 77 |
try:
|
| 78 |
+
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path)
|
| 79 |
img = Image.fromarray(blended_depth_image)
|
| 80 |
return [img, gltf_path, gltf_path]
|
| 81 |
except Exception as e:
|
| 82 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 83 |
+
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path, depth=9)
|
| 84 |
img = Image.fromarray(blended_depth_image)
|
| 85 |
return [img, gltf_path, gltf_path]
|
| 86 |
|
| 87 |
|
| 88 |
+
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
|
|
| 89 |
depth_image_resized = np.array(
|
| 90 |
+
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 91 |
)
|
| 92 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 93 |
depth_image_resized.max() - depth_image_resized.min()
|
| 94 |
+
) * 255
|
| 95 |
+
depth_image_normalized = depth_image_normalized.astype(np.uint8)
|
|
|
|
| 96 |
|
| 97 |
+
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 98 |
+
image_o3d = o3d.geometry.Image(foreground)
|
| 99 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 100 |
image_o3d, depth_o3d, convert_rgb_to_intensity=False
|
| 101 |
)
|
| 102 |
+
w, h = foreground.shape[1], foreground.shape[0]
|
| 103 |
|
|
|
|
| 104 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 105 |
camera_intrinsic.set_intrinsics(w, h, 1500, 1500, w / 2, h / 2)
|
| 106 |
|
|
|
|
| 107 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 108 |
|
|
|
|
| 109 |
if len(pcd.points) < 100:
|
| 110 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 111 |
|
|
|
|
| 112 |
if not pcd.has_normals():
|
|
|
|
| 113 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 114 |
|
| 115 |
if pcd.has_normals():
|
|
|
|
| 117 |
else:
|
| 118 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 119 |
|
|
|
|
| 120 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 121 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 122 |
)
|
| 123 |
|
| 124 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
|
|
|
| 125 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 126 |
voxel_size=voxel_size,
|
| 127 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 128 |
)
|
| 129 |
+
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 130 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 131 |
mesh_crop = mesh.crop(bbox)
|
| 132 |
|
| 133 |
+
gltf_path = f"./{image_path.stem}.gltf"
|
|
|
|
| 134 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 135 |
return gltf_path
|
| 136 |
|