Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
| 17 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 18 |
|
| 19 |
# Function to inpaint the background
|
|
|
|
| 20 |
def inpaint_background(image, mask):
|
| 21 |
"""
|
| 22 |
Inpaint the background of the image using the given mask.
|
|
@@ -30,38 +31,31 @@ def inpaint_background(image, mask):
|
|
| 30 |
"""
|
| 31 |
# Ensure the mask is binary (0 and 255)
|
| 32 |
mask_binary = (mask * 255).astype(np.uint8)
|
| 33 |
-
|
| 34 |
-
# Resize the mask to match the dimensions of the image
|
| 35 |
-
mask_binary_resized = cv2.resize(mask_binary, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 36 |
|
| 37 |
-
# Debugging: Check dimensions
|
| 38 |
-
print(f"Image shape: {image.shape}")
|
| 39 |
-
print(f"Mask shape: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Assert that the dimensions match
|
| 42 |
assert image.shape[:2] == mask_binary_resized.shape[:2], "Image and mask dimensions do not match!"
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return inpainted_image
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
# Ensure depth_image is of type float32
|
| 52 |
-
depth_image = depth_image.astype(np.float32)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
min_val = np.min(depth_image)
|
| 56 |
-
max_val = np.max(depth_image)
|
| 57 |
-
|
| 58 |
-
# Avoid division by zero
|
| 59 |
-
if max_val - min_val > 0:
|
| 60 |
-
depth_image_normalized = (depth_image - min_val) / (max_val - min_val)
|
| 61 |
-
else:
|
| 62 |
-
depth_image_normalized = depth_image - min_val # Handle the case where all values are the same
|
| 63 |
-
|
| 64 |
-
return depth_image_normalized
|
| 65 |
|
| 66 |
# Function to inpaint the background
|
| 67 |
def inpaint_background(image, mask):
|
|
|
|
| 17 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 18 |
|
| 19 |
# Function to inpaint the background
|
| 20 |
+
|
| 21 |
def inpaint_background(image, mask):
|
| 22 |
"""
|
| 23 |
Inpaint the background of the image using the given mask.
|
|
|
|
| 31 |
"""
|
| 32 |
# Ensure the mask is binary (0 and 255)
|
| 33 |
mask_binary = (mask * 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Debugging: Check dimensions before resizing
|
| 36 |
+
print(f"Original Image shape: {image.shape}")
|
| 37 |
+
print(f"Original Mask shape: {mask_binary.shape}")
|
| 38 |
+
|
| 39 |
+
# Resize the mask to match the dimensions of the image if necessary
|
| 40 |
+
if image.shape[:2] != mask_binary.shape[:2]:
|
| 41 |
+
mask_binary_resized = cv2.resize(mask_binary, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 42 |
+
else:
|
| 43 |
+
mask_binary_resized = mask_binary
|
| 44 |
+
|
| 45 |
+
# Debugging: Check dimensions after resizing
|
| 46 |
+
print(f"Resized Mask shape: {mask_binary_resized.shape}")
|
| 47 |
|
| 48 |
# Assert that the dimensions match
|
| 49 |
assert image.shape[:2] == mask_binary_resized.shape[:2], "Image and mask dimensions do not match!"
|
| 50 |
|
| 51 |
+
# Convert the image to uint8 if necessary
|
| 52 |
+
if image.dtype != np.uint8:
|
| 53 |
+
image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
|
|
|
|
| 54 |
|
| 55 |
+
# Inpaint the image using the resized mask
|
| 56 |
+
inpainted_image = cv2.inpaint(image, mask_binary_resized, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
return inpainted_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Function to inpaint the background
|
| 61 |
def inpaint_background(image, mask):
|