Update app.py
Browse files
app.py
CHANGED
|
@@ -10,47 +10,12 @@ import open3d as o3d
|
|
| 10 |
from pathlib import Path
|
| 11 |
import os
|
| 12 |
import cv2
|
| 13 |
-
import tempfile
|
| 14 |
from rembg import remove
|
| 15 |
-
|
| 16 |
-
import requests
|
| 17 |
-
from io import BytesIO
|
| 18 |
-
import base64
|
| 19 |
|
| 20 |
# Initialize model and feature extractor for depth estimation
|
| 21 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 22 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 23 |
-
background_removal_client = Client("not-lain/background-removal")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def remove_background(image):
|
| 28 |
-
# Create a temporary directory to store temporary files
|
| 29 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
| 30 |
-
temp_file_path = os.path.join(temp_dir, "temp_image.png")
|
| 31 |
-
|
| 32 |
-
# Convert the PIL image to a byte stream and save it to the temp file
|
| 33 |
-
image.save(temp_file_path, format="PNG")
|
| 34 |
-
|
| 35 |
-
# Step 1: Remove background using the Gradio API
|
| 36 |
-
result_path = background_removal_client.predict(
|
| 37 |
-
f=temp_file_path, # Pass the path of the temporary file
|
| 38 |
-
api_name="/png"
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
# Step 2: Determine if result_path is a local file path or URL
|
| 42 |
-
if os.path.exists(result_path):
|
| 43 |
-
# If the result is a local file path, open it directly
|
| 44 |
-
foreground = Image.open(result_path).convert("RGBA")
|
| 45 |
-
else:
|
| 46 |
-
# Otherwise, handle it as a URL (assuming it returns a URL)
|
| 47 |
-
response = requests.get(result_path)
|
| 48 |
-
foreground = Image.open(BytesIO(response.content)).convert("RGBA")
|
| 49 |
-
|
| 50 |
-
# Step 3: Apply `rmbg` for additional cleanup
|
| 51 |
-
cleaned_foreground = remove(foreground)
|
| 52 |
-
|
| 53 |
-
return cleaned_foreground.convert("RGB") # Directly return cleaned foreground
|
| 54 |
|
| 55 |
def process_image(image_path, depth_map_path=None):
|
| 56 |
image_path = Path(image_path)
|
|
@@ -60,8 +25,8 @@ def process_image(image_path, depth_map_path=None):
|
|
| 60 |
Image.Resampling.LANCZOS,
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Remove background
|
| 64 |
-
foreground =
|
| 65 |
|
| 66 |
# Check if user-provided depth map is available
|
| 67 |
if depth_map_path:
|
|
@@ -78,7 +43,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 78 |
depth_image = np.clip(depth_image, 0, 1)
|
| 79 |
depth_image = img_as_ubyte(depth_image)
|
| 80 |
else:
|
| 81 |
-
# Generate depth map using DPT model
|
| 82 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 83 |
with torch.no_grad():
|
| 84 |
outputs = model(**encoding)
|
|
@@ -97,9 +62,9 @@ def process_image(image_path, depth_map_path=None):
|
|
| 97 |
depth_image = np.clip(depth_image, 0, 1)
|
| 98 |
depth_image = img_as_ubyte(depth_image)
|
| 99 |
|
| 100 |
-
# Resize and apply
|
| 101 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 102 |
-
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8)
|
| 103 |
|
| 104 |
# Ensure matching size and single channel for blending
|
| 105 |
if len(depth_image.shape) == 3:
|
|
@@ -112,7 +77,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 112 |
# Blend depth_image with mask_blurred_resized for smoother transition
|
| 113 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 114 |
|
| 115 |
-
# Clear border for a smooth edge around main subject
|
| 116 |
blended_depth_image = clear_border(blended_depth_image)
|
| 117 |
|
| 118 |
try:
|
|
@@ -162,7 +127,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 162 |
)
|
| 163 |
|
| 164 |
# Flatten the back of the model from the zero point
|
| 165 |
-
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) /
|
| 166 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 167 |
voxel_size=voxel_size,
|
| 168 |
contraction=o3d.geometry.SimplificationContraction.Average,
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
import os
|
| 12 |
import cv2
|
|
|
|
| 13 |
from rembg import remove
|
| 14 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Initialize model and feature extractor for depth estimation
|
| 17 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 18 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def process_image(image_path, depth_map_path=None):
|
| 21 |
image_path = Path(image_path)
|
|
|
|
| 25 |
Image.Resampling.LANCZOS,
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Remove background from the main image using rembg
|
| 29 |
+
foreground = remove(image_raw).convert("RGB")
|
| 30 |
|
| 31 |
# Check if user-provided depth map is available
|
| 32 |
if depth_map_path:
|
|
|
|
| 43 |
depth_image = np.clip(depth_image, 0, 1)
|
| 44 |
depth_image = img_as_ubyte(depth_image)
|
| 45 |
else:
|
| 46 |
+
# Generate depth map using DPT model, focusing only on the foreground
|
| 47 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 48 |
with torch.no_grad():
|
| 49 |
outputs = model(**encoding)
|
|
|
|
| 62 |
depth_image = np.clip(depth_image, 0, 1)
|
| 63 |
depth_image = img_as_ubyte(depth_image)
|
| 64 |
|
| 65 |
+
# Resize and apply Gaussian smoothing for smoother depth transitions
|
| 66 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 67 |
+
mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8)
|
| 68 |
|
| 69 |
# Ensure matching size and single channel for blending
|
| 70 |
if len(depth_image.shape) == 3:
|
|
|
|
| 77 |
# Blend depth_image with mask_blurred_resized for smoother transition
|
| 78 |
blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
|
| 79 |
|
| 80 |
+
# Clear border for a smooth edge around the main subject
|
| 81 |
blended_depth_image = clear_border(blended_depth_image)
|
| 82 |
|
| 83 |
try:
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
# Flatten the back of the model from the zero point
|
| 130 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
| 131 |
mesh = mesh_raw.simplify_vertex_clustering(
|
| 132 |
voxel_size=voxel_size,
|
| 133 |
contraction=o3d.geometry.SimplificationContraction.Average,
|