Update app.py
Browse files
app.py
CHANGED
|
@@ -29,8 +29,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 29 |
if depth_map_path:
|
| 30 |
if depth_map_path.endswith('.npy'):
|
| 31 |
depth_image = np.load(depth_map_path)
|
| 32 |
-
# Invert depth only for npy files
|
| 33 |
-
depth_image = 1 - depth_image
|
| 34 |
else:
|
| 35 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 36 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
|
@@ -56,31 +55,26 @@ def process_image(image_path, depth_map_path=None):
|
|
| 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, (
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 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=
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image,
|
| 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,
|
| 84 |
img = Image.fromarray(blended_depth_image)
|
| 85 |
return [img, gltf_path, gltf_path]
|
| 86 |
|
|
@@ -109,6 +103,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 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 |
|
|
@@ -117,16 +112,20 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 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 |
|
|
|
|
| 29 |
if depth_map_path:
|
| 30 |
if depth_map_path.endswith('.npy'):
|
| 31 |
depth_image = np.load(depth_map_path)
|
| 32 |
+
depth_image = 1 - depth_image # Invert depth only for npy files
|
|
|
|
| 33 |
else:
|
| 34 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 35 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
|
|
|
| 55 |
|
| 56 |
# Resize and apply lighter Gaussian smoothing for smoother transitions
|
| 57 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 58 |
+
mask_blurred = cv2.GaussianBlur(depth_image, (5, 5), 1.0) # Smoother blur for transition
|
| 59 |
|
| 60 |
+
# Blend depth_image with mask_blurred for smoother transition
|
| 61 |
+
blended_depth_image = cv2.addWeighted(depth_image, 0.7, mask_blurred, 0.3, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Inpaint background where main subject was removed
|
| 64 |
background = remove(image_raw, only_mask=True)
|
| 65 |
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 66 |
+
background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=5, flags=cv2.INPAINT_TELEA)
|
| 67 |
+
|
| 68 |
+
# Slightly scale up background to prevent cropping
|
| 69 |
+
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.1), int(image.height * 1.1)))
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, bg_scaled, image_path)
|
| 73 |
img = Image.fromarray(blended_depth_image)
|
| 74 |
return [img, gltf_path, gltf_path]
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 77 |
+
gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, bg_scaled, image_path, depth=9)
|
| 78 |
img = Image.fromarray(blended_depth_image)
|
| 79 |
return [img, gltf_path, gltf_path]
|
| 80 |
|
|
|
|
| 103 |
if len(pcd.points) < 100:
|
| 104 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 105 |
|
| 106 |
+
# Orient normals and smooth
|
| 107 |
if not pcd.has_normals():
|
| 108 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 109 |
|
|
|
|
| 112 |
else:
|
| 113 |
raise RuntimeError("Failed to estimate normals for the point cloud.")
|
| 114 |
|
| 115 |
+
# Generate 3D mesh with smoothing and flatten back
|
| 116 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 117 |
pcd, depth=depth, width=0, scale=1.2, linear_fit=True
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Simplify mesh for better performance
|
| 121 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
|
| 122 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 123 |
voxel_size=voxel_size,
|
| 124 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
| 125 |
)
|
| 126 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 127 |
+
|
| 128 |
+
# Crop and align model
|
| 129 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 130 |
mesh_crop = mesh.crop(bbox)
|
| 131 |
|