Update app.py
Browse files
app.py
CHANGED
|
@@ -64,19 +64,14 @@ def process_image(image_path, depth_map_path=None):
|
|
| 64 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
|
| 65 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
edges = cv2.Canny(img_as_ubyte(mask), threshold1=
|
| 69 |
-
edges_dilated = cv2.dilate(edges, np.ones((3, 3), np.uint8), iterations=
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Apply the edge-aware mask to limit depth to the main subject only
|
| 76 |
-
depth_subject_only = np.where(edges_dilated == 0, depth_eroded, 0)
|
| 77 |
-
|
| 78 |
-
# Set depth to zero in the background and around subject edges
|
| 79 |
-
depth_subject_only[~mask] = 0
|
| 80 |
|
| 81 |
# Generate the final depth image with reduced blur artifacts
|
| 82 |
depth_display = img_as_ubyte(depth_subject_only)
|
|
@@ -91,7 +86,6 @@ def process_image(image_path, depth_map_path=None):
|
|
| 91 |
gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, image_path, depth=8)
|
| 92 |
img = Image.fromarray(depth_display)
|
| 93 |
return [img, gltf_path, gltf_path]
|
| 94 |
-
|
| 95 |
|
| 96 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 97 |
"""
|
|
|
|
| 64 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
|
| 65 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1)
|
| 66 |
|
| 67 |
+
# Improved edge detection for the main subject
|
| 68 |
+
edges = cv2.Canny(img_as_ubyte(mask), threshold1=50, threshold2=150)
|
| 69 |
+
edges_dilated = cv2.dilate(edges, np.ones((3, 3), np.uint8), iterations=2)
|
| 70 |
+
edges_eroded = cv2.erode(edges_dilated, np.ones((3, 3), np.uint8), iterations=1)
|
| 71 |
|
| 72 |
+
# Use the refined edge mask to clean up the depth map
|
| 73 |
+
depth_subject_only = np.where(edges_eroded == 0, depth_image * mask, 0)
|
| 74 |
+
depth_subject_only[~mask] = 0 # Ensure background areas are zeroed out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Generate the final depth image with reduced blur artifacts
|
| 77 |
depth_display = img_as_ubyte(depth_subject_only)
|
|
|
|
| 86 |
gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, image_path, depth=8)
|
| 87 |
img = Image.fromarray(depth_display)
|
| 88 |
return [img, gltf_path, gltf_path]
|
|
|
|
| 89 |
|
| 90 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 91 |
"""
|