Update app.py
Browse files
app.py
CHANGED
|
@@ -64,7 +64,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 64 |
|
| 65 |
# Step 2: Edge-preserving filter (Enhance small structures)
|
| 66 |
depth_image = cv2.edgePreservingFilter(
|
| 67 |
-
(depth_image_cleaned * 255).astype(np.uint8), flags=1, sigma_s=
|
| 68 |
)
|
| 69 |
depth_image = depth_image.astype(np.float32) / 255 # Normalize back to [0, 1]
|
| 70 |
|
|
@@ -80,7 +80,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 80 |
# Step 6: Create an edge-aware mask (if needed for further processing)
|
| 81 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 82 |
# Improved edge detection for the main subject
|
| 83 |
-
edges = cv2.Canny(img_as_ubyte(mask), threshold1=
|
| 84 |
edges_dilated = cv2.dilate(edges, np.ones((9, 9), np.uint8), iterations=4) # Larger kernel, fewer gaps
|
| 85 |
edges_eroded = cv2.erode(edges_dilated, np.ones((9, 9), np.uint8), iterations=2)
|
| 86 |
|
|
@@ -102,7 +102,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 102 |
img = Image.fromarray(depth_display)
|
| 103 |
return [img, gltf_path, gltf_path]
|
| 104 |
|
| 105 |
-
def create_3d_obj(foreground, depth_image, image_path, depth=
|
| 106 |
"""
|
| 107 |
Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
|
| 108 |
|
|
@@ -156,7 +156,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=10, edge_strength=0
|
|
| 156 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 157 |
|
| 158 |
# Estimate normals with refined parameters
|
| 159 |
-
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.
|
| 160 |
pcd.orient_normals_consistent_tangent_plane(k=10)
|
| 161 |
|
| 162 |
|
|
@@ -174,7 +174,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=10, edge_strength=0
|
|
| 174 |
|
| 175 |
|
| 176 |
# Simplify with smaller voxel size for more detail
|
| 177 |
-
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) /
|
| 178 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 179 |
voxel_size=voxel_size,
|
| 180 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
|
|
|
| 64 |
|
| 65 |
# Step 2: Edge-preserving filter (Enhance small structures)
|
| 66 |
depth_image = cv2.edgePreservingFilter(
|
| 67 |
+
(depth_image_cleaned * 255).astype(np.uint8), flags=1, sigma_s=13, sigma_r=0.3
|
| 68 |
)
|
| 69 |
depth_image = depth_image.astype(np.float32) / 255 # Normalize back to [0, 1]
|
| 70 |
|
|
|
|
| 80 |
# Step 6: Create an edge-aware mask (if needed for further processing)
|
| 81 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 82 |
# Improved edge detection for the main subject
|
| 83 |
+
edges = cv2.Canny(img_as_ubyte(mask), threshold1=30, threshold2=150) # Adjust thresholds for edges
|
| 84 |
edges_dilated = cv2.dilate(edges, np.ones((9, 9), np.uint8), iterations=4) # Larger kernel, fewer gaps
|
| 85 |
edges_eroded = cv2.erode(edges_dilated, np.ones((9, 9), np.uint8), iterations=2)
|
| 86 |
|
|
|
|
| 102 |
img = Image.fromarray(depth_display)
|
| 103 |
return [img, gltf_path, gltf_path]
|
| 104 |
|
| 105 |
+
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 106 |
"""
|
| 107 |
Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
|
| 108 |
|
|
|
|
| 156 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 157 |
|
| 158 |
# Estimate normals with refined parameters
|
| 159 |
+
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=100))
|
| 160 |
pcd.orient_normals_consistent_tangent_plane(k=10)
|
| 161 |
|
| 162 |
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
# Simplify with smaller voxel size for more detail
|
| 177 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 6200
|
| 178 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 179 |
voxel_size=voxel_size,
|
| 180 |
contraction=o3d.geometry.SimplificationContraction.Average,
|