Update app.py
Browse files
app.py
CHANGED
|
@@ -81,9 +81,13 @@ def process_image(image_path, depth_map_path=None):
|
|
| 81 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 82 |
|
| 83 |
# Improved edge detection for the main subject
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Use the refined edge mask to clean up the depth map
|
| 89 |
depth_subject_only = np.where(edges_eroded == 0, depth_image * mask, 0)
|
|
@@ -166,16 +170,16 @@ def create_3d_obj(foreground, depth_image, image_path, depth=10, edge_strength=0
|
|
| 166 |
|
| 167 |
# Create Poisson mesh using main subject's isolated depth
|
| 168 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 169 |
-
pcd, depth=depth, width=0, scale=
|
| 170 |
)
|
| 171 |
# Remove low-density artifacts
|
| 172 |
-
density_threshold = np.quantile(np.asarray(densities), 0.
|
| 173 |
vertices_to_remove = densities < density_threshold
|
| 174 |
mesh_raw.remove_vertices_by_mask(vertices_to_remove)
|
| 175 |
|
| 176 |
|
| 177 |
# Simplify with smaller voxel size for more detail
|
| 178 |
-
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) /
|
| 179 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 180 |
voxel_size=voxel_size,
|
| 181 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
|
@@ -195,7 +199,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=10, edge_strength=0
|
|
| 195 |
# Save high-resolution texture image
|
| 196 |
texture_path = f"./{image_path.stem}_texture_highres.png"
|
| 197 |
foreground_pil = Image.fromarray(foreground)
|
| 198 |
-
high_res_texture = foreground_pil.resize((
|
| 199 |
high_res_texture.save(texture_path)
|
| 200 |
|
| 201 |
# Apply the high-resolution texture and export the mesh as a GLTF file
|
|
|
|
| 81 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 82 |
|
| 83 |
# Improved edge detection for the main subject
|
| 84 |
+
gray_foreground = cv2.cvtColor(np.array(foreground), cv2.COLOR_RGB2GRAY)
|
| 85 |
+
v = np.median(gray_foreground)
|
| 86 |
+
lower = int(max(0, 0.66 * v))
|
| 87 |
+
upper = int(min(255, 1.33 * v))
|
| 88 |
+
edges = cv2.Canny(gray_foreground, threshold1=lower, threshold2=upper)
|
| 89 |
+
edges_dilated = cv2.dilate(edges, np.ones((7, 7), np.uint8), iterations=2)
|
| 90 |
+
edges_eroded = cv2.erode(edges_dilated, np.ones((5, 5), np.uint8), iterations=1)
|
| 91 |
|
| 92 |
# Use the refined edge mask to clean up the depth map
|
| 93 |
depth_subject_only = np.where(edges_eroded == 0, depth_image * mask, 0)
|
|
|
|
| 170 |
|
| 171 |
# Create Poisson mesh using main subject's isolated depth
|
| 172 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 173 |
+
pcd, depth=depth, width=0, scale=4, linear_fit=True
|
| 174 |
)
|
| 175 |
# Remove low-density artifacts
|
| 176 |
+
density_threshold = np.quantile(np.asarray(densities), 0.06) # Adjust quantile as needed
|
| 177 |
vertices_to_remove = densities < density_threshold
|
| 178 |
mesh_raw.remove_vertices_by_mask(vertices_to_remove)
|
| 179 |
|
| 180 |
|
| 181 |
# Simplify with smaller voxel size for more detail
|
| 182 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 5200
|
| 183 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 184 |
voxel_size=voxel_size,
|
| 185 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
|
|
|
| 199 |
# Save high-resolution texture image
|
| 200 |
texture_path = f"./{image_path.stem}_texture_highres.png"
|
| 201 |
foreground_pil = Image.fromarray(foreground)
|
| 202 |
+
high_res_texture = foreground_pil.resize((3096, 3096), Image.LANCZOS) # Increased resolution for finer details
|
| 203 |
high_res_texture.save(texture_path)
|
| 204 |
|
| 205 |
# Apply the high-resolution texture and export the mesh as a GLTF file
|