Update app.py
Browse files
app.py
CHANGED
|
@@ -16,20 +16,21 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 16 |
def process_image(image_path, depth_map_path=None):
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
|
| 19 |
image = image_raw.resize(
|
| 20 |
(2048, int(2048 * image_raw.size[1] / image_raw.size[0])),
|
| 21 |
Image.Resampling.LANCZOS,
|
| 22 |
)
|
| 23 |
|
| 24 |
# Remove background using rembg
|
| 25 |
-
foreground = remove(image_raw)
|
| 26 |
-
foreground = Image.fromarray(np.array(foreground)).convert("RGB")
|
| 27 |
|
| 28 |
-
# Duplicate the background
|
| 29 |
-
bg_copy = image_raw.
|
| 30 |
image_combined = Image.alpha_composite(bg_copy, foreground)
|
| 31 |
|
| 32 |
-
# Check if user-provided depth map is available
|
| 33 |
if depth_map_path:
|
| 34 |
if depth_map_path.endswith('.npy'):
|
| 35 |
depth_image = np.load(depth_map_path)
|
|
@@ -38,16 +39,17 @@ def process_image(image_path, depth_map_path=None):
|
|
| 38 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 39 |
depth_image = np.array(depth_image)
|
| 40 |
|
| 41 |
-
# Normalize depth image
|
| 42 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 43 |
depth_image = (depth_image * 255).astype('uint8')
|
| 44 |
else:
|
| 45 |
-
# Generate depth map using DPT model
|
| 46 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 47 |
with torch.no_grad():
|
| 48 |
outputs = model(**encoding)
|
| 49 |
predicted_depth = outputs.predicted_depth
|
| 50 |
|
|
|
|
| 51 |
prediction = torch.nn.functional.interpolate(
|
| 52 |
predicted_depth.unsqueeze(1),
|
| 53 |
size=image.size[::-1],
|
|
@@ -56,9 +58,9 @@ def process_image(image_path, depth_map_path=None):
|
|
| 56 |
).squeeze()
|
| 57 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 58 |
|
| 59 |
-
# Resize and apply
|
| 60 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 61 |
-
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8)
|
| 62 |
|
| 63 |
# Ensure matching size and single channel for blending
|
| 64 |
if len(depth_image.shape) == 3:
|
|
@@ -67,7 +69,10 @@ def process_image(image_path, depth_map_path=None):
|
|
| 67 |
if len(mask_blurred.shape) == 3:
|
| 68 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 69 |
|
|
|
|
| 70 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
|
|
|
|
|
|
| 71 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 16 |
def process_image(image_path, depth_map_path=None):
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB")
|
| 19 |
+
|
| 20 |
+
# Resize the main image for consistent depth estimation size
|
| 21 |
image = image_raw.resize(
|
| 22 |
(2048, int(2048 * image_raw.size[1] / image_raw.size[0])),
|
| 23 |
Image.Resampling.LANCZOS,
|
| 24 |
)
|
| 25 |
|
| 26 |
# Remove background using rembg
|
| 27 |
+
foreground = Image.fromarray(np.array(remove(image_raw))).convert("RGBA") # Convert foreground to RGBA
|
|
|
|
| 28 |
|
| 29 |
+
# Duplicate the background, adding depth to one and keeping the other flat
|
| 30 |
+
bg_copy = image_raw.convert("RGBA") # Ensure background is also in RGBA mode
|
| 31 |
image_combined = Image.alpha_composite(bg_copy, foreground)
|
| 32 |
|
| 33 |
+
# Check if a user-provided depth map is available
|
| 34 |
if depth_map_path:
|
| 35 |
if depth_map_path.endswith('.npy'):
|
| 36 |
depth_image = np.load(depth_map_path)
|
|
|
|
| 39 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 40 |
depth_image = np.array(depth_image)
|
| 41 |
|
| 42 |
+
# Normalize the depth image
|
| 43 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 44 |
depth_image = (depth_image * 255).astype('uint8')
|
| 45 |
else:
|
| 46 |
+
# Generate a depth map using the DPT model
|
| 47 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 48 |
with torch.no_grad():
|
| 49 |
outputs = model(**encoding)
|
| 50 |
predicted_depth = outputs.predicted_depth
|
| 51 |
|
| 52 |
+
# Interpolate the depth map to match image dimensions
|
| 53 |
prediction = torch.nn.functional.interpolate(
|
| 54 |
predicted_depth.unsqueeze(1),
|
| 55 |
size=image.size[::-1],
|
|
|
|
| 58 |
).squeeze()
|
| 59 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 60 |
|
| 61 |
+
# Resize and apply a lighter Gaussian blur for minimal depth on background
|
| 62 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 63 |
+
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur
|
| 64 |
|
| 65 |
# Ensure matching size and single channel for blending
|
| 66 |
if len(depth_image.shape) == 3:
|
|
|
|
| 69 |
if len(mask_blurred.shape) == 3:
|
| 70 |
mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
|
| 71 |
|
| 72 |
+
# Resize mask_blurred to match depth_image dimensions
|
| 73 |
mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
|
| 74 |
+
|
| 75 |
+
# Blend depth_image with mask_blurred_resized
|
| 76 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 77 |
|
| 78 |
try:
|