Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,9 @@ from pathlib import Path
|
|
| 8 |
import os
|
| 9 |
import cv2
|
| 10 |
from rembg import remove
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Initialize model and feature extractor for depth estimation
|
| 13 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
@@ -53,32 +56,34 @@ def process_image(image_path, depth_map_path=None):
|
|
| 53 |
).squeeze()
|
| 54 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 55 |
|
| 56 |
-
# Resize and apply
|
| 57 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# Inpaint background where main subject was removed
|
| 64 |
background = remove(image_raw, only_mask=True)
|
| 65 |
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 66 |
background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=5, flags=cv2.INPAINT_TELEA)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.
|
| 70 |
|
| 71 |
try:
|
| 72 |
-
gltf_path = create_3d_obj(np.array(foreground),
|
| 73 |
-
img = Image.fromarray(
|
| 74 |
return [img, gltf_path, gltf_path]
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 77 |
-
gltf_path = create_3d_obj(np.array(foreground),
|
| 78 |
-
img = Image.fromarray(
|
| 79 |
return [img, gltf_path, gltf_path]
|
| 80 |
|
| 81 |
-
|
| 82 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 83 |
depth_image_resized = np.array(
|
| 84 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
@@ -106,7 +111,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 106 |
# Orient normals and smooth
|
| 107 |
if not pcd.has_normals():
|
| 108 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 109 |
-
|
| 110 |
if pcd.has_normals():
|
| 111 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 112 |
else:
|
|
|
|
| 8 |
import os
|
| 9 |
import cv2
|
| 10 |
from rembg import remove
|
| 11 |
+
from skimage import img_as_ubyte
|
| 12 |
+
from skimage.filters import gaussian
|
| 13 |
+
from skimage.segmentation import clear_border
|
| 14 |
|
| 15 |
# Initialize model and feature extractor for depth estimation
|
| 16 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
|
|
| 56 |
).squeeze()
|
| 57 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 58 |
|
| 59 |
+
# Resize and apply Gaussian smoothing for smoother transitions
|
| 60 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 61 |
+
depth_image = img_as_ubyte(gaussian(depth_image, sigma=1.0, mode='nearest'))
|
| 62 |
|
| 63 |
+
# Sharpen the edges of the main subject
|
| 64 |
+
mask = np.array(remove(image_raw, only_mask=True))
|
| 65 |
+
mask = clear_border(mask)
|
| 66 |
+
foreground_edges = cv2.Canny(mask, threshold1=50, threshold2=150)
|
| 67 |
+
depth_image[foreground_edges > 0] = depth_image[foreground_edges > 0] * 1.2 # Sharpen subject edges slightly
|
| 68 |
|
| 69 |
+
# Inpaint background where the main subject was removed
|
| 70 |
background = remove(image_raw, only_mask=True)
|
| 71 |
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 72 |
background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=5, flags=cv2.INPAINT_TELEA)
|
| 73 |
|
| 74 |
+
# Scale up background to prevent cropping and maintain colors
|
| 75 |
+
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.2), int(image.height * 1.2)))
|
| 76 |
|
| 77 |
try:
|
| 78 |
+
gltf_path = create_3d_obj(np.array(foreground), depth_image, bg_scaled, image_path)
|
| 79 |
+
img = Image.fromarray(depth_image)
|
| 80 |
return [img, gltf_path, gltf_path]
|
| 81 |
except Exception as e:
|
| 82 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 83 |
+
gltf_path = create_3d_obj(np.array(foreground), depth_image, bg_scaled, image_path, depth=9)
|
| 84 |
+
img = Image.fromarray(depth_image)
|
| 85 |
return [img, gltf_path, gltf_path]
|
| 86 |
|
|
|
|
| 87 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 88 |
depth_image_resized = np.array(
|
| 89 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
|
|
| 111 |
# Orient normals and smooth
|
| 112 |
if not pcd.has_normals():
|
| 113 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
|
| 114 |
+
|
| 115 |
if pcd.has_normals():
|
| 116 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 117 |
else:
|