Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,12 @@ def process_image(image_path, depth_map_path=None):
|
|
| 28 |
foreground = remove(image_raw)
|
| 29 |
foreground = Image.fromarray(np.array(foreground)).convert("RGB")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Check if user-provided depth map is available
|
| 32 |
if depth_map_path:
|
| 33 |
if depth_map_path.endswith('.npy'):
|
|
@@ -40,7 +46,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 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('
|
| 44 |
else:
|
| 45 |
# Generate depth map using DPT model
|
| 46 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
@@ -54,17 +60,17 @@ def process_image(image_path, depth_map_path=None):
|
|
| 54 |
mode="bicubic",
|
| 55 |
align_corners=False,
|
| 56 |
).squeeze()
|
| 57 |
-
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("
|
| 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 |
-
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
depth_image[
|
| 68 |
|
| 69 |
# Inpaint background where the main subject was removed
|
| 70 |
background = remove(image_raw, only_mask=True)
|
|
@@ -74,24 +80,33 @@ def process_image(image_path, depth_map_path=None):
|
|
| 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(
|
| 79 |
-
img = Image.fromarray(
|
| 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(
|
| 84 |
-
img = Image.fromarray(
|
| 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)
|
| 90 |
)
|
|
|
|
|
|
|
| 91 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 92 |
depth_image_resized.max() - depth_image_resized.min()
|
| 93 |
-
)
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 97 |
image_o3d = o3d.geometry.Image(foreground)
|
|
|
|
| 28 |
foreground = remove(image_raw)
|
| 29 |
foreground = Image.fromarray(np.array(foreground)).convert("RGB")
|
| 30 |
|
| 31 |
+
# Create a binary mask of the foreground
|
| 32 |
+
foreground_edges = cv2.Canny(np.array(foreground), 100, 200)
|
| 33 |
+
|
| 34 |
+
# Resize foreground edges to match depth image dimensions
|
| 35 |
+
foreground_edges_resized = cv2.resize(foreground_edges, (image.size[0], image.size[1]), interpolation=cv2.INTER_NEAREST)
|
| 36 |
+
|
| 37 |
# Check if user-provided depth map is available
|
| 38 |
if depth_map_path:
|
| 39 |
if depth_map_path.endswith('.npy'):
|
|
|
|
| 46 |
|
| 47 |
# Normalize depth image
|
| 48 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 49 |
+
depth_image = (depth_image * 255).astype('float32') # Changed to float32
|
| 50 |
else:
|
| 51 |
# Generate depth map using DPT model
|
| 52 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
|
|
|
| 60 |
mode="bicubic",
|
| 61 |
align_corners=False,
|
| 62 |
).squeeze()
|
| 63 |
+
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("float32") # Changed to float32
|
| 64 |
|
| 65 |
# Resize and apply Gaussian smoothing for smoother 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, (5, 5), 1.0)
|
| 68 |
|
| 69 |
+
# Blend depth_image with mask_blurred for smoother transition
|
| 70 |
+
blended_depth_image = cv2.addWeighted(depth_image, 0.7, mask_blurred, 0.3, 0)
|
| 71 |
+
|
| 72 |
+
# Sharpen subject edges slightly
|
| 73 |
+
depth_image[foreground_edges_resized > 0] = depth_image[foreground_edges_resized > 0] * 1.2
|
| 74 |
|
| 75 |
# Inpaint background where the main subject was removed
|
| 76 |
background = remove(image_raw, only_mask=True)
|
|
|
|
| 80 |
# Scale up background to prevent cropping and maintain colors
|
| 81 |
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.2), int(image.height * 1.2)))
|
| 82 |
|
| 83 |
+
# Optionally, adjust the foreground position and flatten the back
|
| 84 |
+
foreground_flat = np.array(foreground)
|
| 85 |
+
foreground_flat = foreground_flat[:, :, :3] # Keep only RGB channels
|
| 86 |
+
foreground_flat = np.insert(foreground_flat, 2, values=0, axis=2) # Add a zero channel for depth
|
| 87 |
+
|
| 88 |
try:
|
| 89 |
+
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path)
|
| 90 |
+
img = Image.fromarray(blended_depth_image)
|
| 91 |
return [img, gltf_path, gltf_path]
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 94 |
+
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9)
|
| 95 |
+
img = Image.fromarray(blended_depth_image)
|
| 96 |
return [img, gltf_path, gltf_path]
|
| 97 |
|
| 98 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 99 |
depth_image_resized = np.array(
|
| 100 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
| 101 |
)
|
| 102 |
+
|
| 103 |
+
# Normalize depth for float32 representation
|
| 104 |
depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
|
| 105 |
depth_image_resized.max() - depth_image_resized.min()
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Convert to float32 for depth processing
|
| 109 |
+
depth_image_normalized = depth_image_normalized.astype(np.float32)
|
| 110 |
|
| 111 |
depth_o3d = o3d.geometry.Image(depth_image_normalized)
|
| 112 |
image_o3d = o3d.geometry.Image(foreground)
|