Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
| 16 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 17 |
|
| 18 |
|
|
|
|
| 19 |
def process_image(image_path, depth_map_path=None):
|
| 20 |
image_path = Path(image_path)
|
| 21 |
image_raw = Image.open(image_path).convert("RGB")
|
|
@@ -61,19 +62,23 @@ def process_image(image_path, depth_map_path=None):
|
|
| 61 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
|
| 62 |
depth_image = np.clip(depth_image, 0, 1)
|
| 63 |
|
| 64 |
-
# Resize and apply
|
| 65 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Apply depth only to main subject
|
| 69 |
foreground_array = np.array(foreground)
|
| 70 |
-
mask = np.all(foreground_array != [0, 0, 0], axis=-1)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
depth_display = img_as_ubyte(blended_depth_image)
|
| 78 |
|
| 79 |
# Background removal - commented for potential future use
|
|
|
|
| 16 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 17 |
|
| 18 |
|
| 19 |
+
|
| 20 |
def process_image(image_path, depth_map_path=None):
|
| 21 |
image_path = Path(image_path)
|
| 22 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
| 62 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
|
| 63 |
depth_image = np.clip(depth_image, 0, 1)
|
| 64 |
|
| 65 |
+
# Resize and apply mask processing for cleaner subject isolation
|
| 66 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
|
| 67 |
+
|
| 68 |
+
# Create mask for main subject and apply morphological cleaning to eliminate extensions
|
|
|
|
| 69 |
foreground_array = np.array(foreground)
|
| 70 |
+
mask = np.all(foreground_array != [0, 0, 0], axis=-1)
|
| 71 |
+
kernel = np.ones((3, 3), np.uint8)
|
| 72 |
+
mask_eroded = cv2.erode(mask.astype(np.uint8), kernel, iterations=3)
|
| 73 |
+
mask_cleaned = cv2.dilate(mask_eroded, kernel, iterations=2)
|
| 74 |
+
|
| 75 |
+
# Apply the cleaned mask to isolate the subject precisely in depth
|
| 76 |
+
depth_image_subject = np.zeros_like(depth_image)
|
| 77 |
+
depth_image_subject[mask_cleaned.astype(bool)] = depth_image[mask_cleaned.astype(bool)]
|
| 78 |
+
|
| 79 |
+
# Apply a controlled smooth blend at the edges for smooth transition, avoiding extensions beyond main subject
|
| 80 |
+
mask_blurred = cv2.bilateralFilter(depth_image_subject, 5, 50, 50)
|
| 81 |
+
blended_depth_image = cv2.addWeighted(depth_image_subject, 0.9, mask_blurred, 0.1, 0)
|
| 82 |
depth_display = img_as_ubyte(blended_depth_image)
|
| 83 |
|
| 84 |
# Background removal - commented for potential future use
|