Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
from skimage import img_as_ubyte
|
| 2 |
from skimage.filters import gaussian
|
|
|
|
| 3 |
from skimage.segmentation import clear_border
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 6 |
import torch
|
|
@@ -9,7 +12,6 @@ from PIL import Image
|
|
| 9 |
import open3d as o3d
|
| 10 |
from pathlib import Path
|
| 11 |
import os
|
| 12 |
-
import cv2
|
| 13 |
from rembg import remove
|
| 14 |
import tempfile
|
| 15 |
|
|
@@ -35,8 +37,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 35 |
depth_image = 1 - depth_image # Invert depth only for npy files
|
| 36 |
else:
|
| 37 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 38 |
-
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 39 |
-
depth_image = np.array(depth_image)
|
| 40 |
|
| 41 |
# Normalize depth image to [0, 1] range
|
| 42 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
|
@@ -62,22 +63,28 @@ def process_image(image_path, depth_map_path=None):
|
|
| 62 |
depth_image = np.clip(depth_image, 0, 1)
|
| 63 |
depth_image = img_as_ubyte(depth_image)
|
| 64 |
|
| 65 |
-
# Resize
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
# Blend
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
# Clear border for a
|
| 81 |
blended_depth_image = clear_border(blended_depth_image)
|
| 82 |
|
| 83 |
try:
|
|
@@ -163,4 +170,4 @@ iface = gr.Interface(
|
|
| 163 |
allow_flagging="never",
|
| 164 |
cache_examples=False,
|
| 165 |
)
|
| 166 |
-
iface.launch(debug=True, show_api=True
|
|
|
|
| 1 |
+
from skimage import img_as_ubyte, img_as_float
|
| 2 |
from skimage.filters import gaussian
|
| 3 |
+
from skimage.transform import resize
|
| 4 |
from skimage.segmentation import clear_border
|
| 5 |
+
from skimage.color import rgb2gray
|
| 6 |
+
from skimage.morphology import dilation, square
|
| 7 |
import gradio as gr
|
| 8 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 9 |
import torch
|
|
|
|
| 12 |
import open3d as o3d
|
| 13 |
from pathlib import Path
|
| 14 |
import os
|
|
|
|
| 15 |
from rembg import remove
|
| 16 |
import tempfile
|
| 17 |
|
|
|
|
| 37 |
depth_image = 1 - depth_image # Invert depth only for npy files
|
| 38 |
else:
|
| 39 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 40 |
+
depth_image = np.array(depth_image_raw.resize(image.size, Image.Resampling.NEAREST))
|
|
|
|
| 41 |
|
| 42 |
# Normalize depth image to [0, 1] range
|
| 43 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
|
|
|
| 63 |
depth_image = np.clip(depth_image, 0, 1)
|
| 64 |
depth_image = img_as_ubyte(depth_image)
|
| 65 |
|
| 66 |
+
# Resize depth image with skimage to match image size
|
| 67 |
+
depth_image_resized = resize(depth_image, (image.size[1], image.size[0]), order=3, preserve_range=True)
|
| 68 |
+
depth_image_resized = img_as_ubyte(depth_image_resized)
|
| 69 |
|
| 70 |
+
# Create edge mask to prevent edge blur
|
| 71 |
+
foreground_array = np.array(foreground)
|
| 72 |
+
edge_mask = dilation((foreground_array.sum(axis=2) > 0).astype(np.uint8), square(5))
|
| 73 |
+
|
| 74 |
+
# Apply Gaussian smoothing only within the interior of the depth map
|
| 75 |
+
depth_image_smoothed = gaussian(depth_image_resized, sigma=0.8, preserve_range=True)
|
| 76 |
+
depth_image_smoothed = img_as_ubyte(depth_image_smoothed)
|
| 77 |
|
| 78 |
+
# Blend smoothly within interior areas
|
| 79 |
+
depth_float = img_as_float(depth_image_resized)
|
| 80 |
+
mask_blurred_float = img_as_float(depth_image_smoothed)
|
| 81 |
+
|
| 82 |
+
# Edge-preserving blend
|
| 83 |
+
blended_depth_image = img_as_ubyte(
|
| 84 |
+
depth_float * edge_mask + mask_blurred_float * (1 - edge_mask)
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
# Clear border for a clean edge around the main subject
|
| 88 |
blended_depth_image = clear_border(blended_depth_image)
|
| 89 |
|
| 90 |
try:
|
|
|
|
| 170 |
allow_flagging="never",
|
| 171 |
cache_examples=False,
|
| 172 |
)
|
| 173 |
+
iface.launch(debug=True, show_api=True)
|