Update app.py
Browse files
app.py
CHANGED
|
@@ -57,81 +57,68 @@ def process_image(image_path, depth_map_path=None):
|
|
| 57 |
|
| 58 |
# Create a binary mask of the foreground
|
| 59 |
foreground_edges = cv2.Canny(np.array(foreground), 100, 200)
|
| 60 |
-
|
| 61 |
-
# Resize foreground edges to match depth image dimensions
|
| 62 |
foreground_edges_resized = cv2.resize(foreground_edges, (image.size[0], image.size[1]), interpolation=cv2.INTER_NEAREST)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
if depth_map_path:
|
| 66 |
if depth_map_path.endswith('.npy'):
|
| 67 |
depth_image = np.load(depth_map_path)
|
| 68 |
-
depth_image = 1 - depth_image # Invert depth
|
| 69 |
else:
|
| 70 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 71 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 72 |
depth_image = np.array(depth_image)
|
| 73 |
-
|
| 74 |
-
# Normalize and process the depth image
|
| 75 |
depth_image = process_depth_image(depth_image) # Apply processing
|
| 76 |
else:
|
| 77 |
-
# Generate depth map using DPT model
|
| 78 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 79 |
with torch.no_grad():
|
| 80 |
outputs = model(**encoding)
|
| 81 |
predicted_depth = outputs.predicted_depth
|
| 82 |
-
|
| 83 |
prediction = torch.nn.functional.interpolate(
|
| 84 |
predicted_depth.unsqueeze(1),
|
| 85 |
size=image.size[::-1],
|
| 86 |
mode="bicubic",
|
| 87 |
align_corners=False,
|
| 88 |
).squeeze()
|
| 89 |
-
depth_image = (prediction.cpu().numpy() - np.min(prediction.cpu().numpy())) / (np.max(prediction.cpu().numpy()) - np.min(prediction.cpu().numpy())) # Normalize
|
| 90 |
depth_image = process_depth_image(depth_image) # Apply processing
|
| 91 |
|
| 92 |
-
#
|
| 93 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 94 |
mask_blurred = cv2.GaussianBlur(depth_image, (5, 5), 1.0)
|
| 95 |
-
|
| 96 |
-
# Blend depth_image with mask_blurred for smoother transition
|
| 97 |
blended_depth_image = cv2.addWeighted(depth_image, 0.7, mask_blurred, 0.3, 0)
|
|
|
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Create mask for the main subject
|
| 103 |
-
mask = (depth_image > 0).astype(np.uint8) # Create a binary mask for the subject
|
| 104 |
-
mask = clear_border(mask) # Clear objects touching the border
|
| 105 |
-
|
| 106 |
-
# Inpaint background where the main subject was removed
|
| 107 |
background = remove(image_raw, only_mask=True)
|
| 108 |
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 109 |
background_inpainted = inpaint_background(np.array(image_raw), mask)
|
| 110 |
-
|
| 111 |
-
# Scale up background to prevent cropping and maintain colors
|
| 112 |
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.2), int(image.height * 1.2)))
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
foreground_flat = np.array(foreground)
|
| 116 |
-
foreground_flat = foreground_flat
|
| 117 |
-
foreground_flat = np.insert(foreground_flat, 2, values=0, axis=2) # Add a zero channel for depth
|
| 118 |
|
| 119 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# Ensure a placeholder for the 3D model output
|
| 124 |
-
model_3d = gr.Model3D() # Add a placeholder for Model3D output
|
| 125 |
-
return [img, model_3d, gltf_path] # Return all three outputs
|
| 126 |
except Exception as e:
|
| 127 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 128 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9)
|
| 129 |
-
img = Image.fromarray(blended_depth_image)
|
| 130 |
-
|
| 131 |
-
# Ensure a placeholder for the 3D model output
|
| 132 |
-
model_3d = gr.Model3D() # Add a placeholder for Model3D output
|
| 133 |
-
return [img, model_3d, gltf_path] # Return all three outputs
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 136 |
# Resize background to match the original image size (no cropping)
|
| 137 |
background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
|
|
|
|
| 57 |
|
| 58 |
# Create a binary mask of the foreground
|
| 59 |
foreground_edges = cv2.Canny(np.array(foreground), 100, 200)
|
|
|
|
|
|
|
| 60 |
foreground_edges_resized = cv2.resize(foreground_edges, (image.size[0], image.size[1]), interpolation=cv2.INTER_NEAREST)
|
| 61 |
|
| 62 |
+
# Depth map handling
|
| 63 |
if depth_map_path:
|
| 64 |
if depth_map_path.endswith('.npy'):
|
| 65 |
depth_image = np.load(depth_map_path)
|
| 66 |
+
depth_image = 1 - depth_image # Invert depth for npy files
|
| 67 |
else:
|
| 68 |
depth_image_raw = Image.open(depth_map_path).convert("L")
|
| 69 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 70 |
depth_image = np.array(depth_image)
|
|
|
|
|
|
|
| 71 |
depth_image = process_depth_image(depth_image) # Apply processing
|
| 72 |
else:
|
|
|
|
| 73 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 74 |
with torch.no_grad():
|
| 75 |
outputs = model(**encoding)
|
| 76 |
predicted_depth = outputs.predicted_depth
|
|
|
|
| 77 |
prediction = torch.nn.functional.interpolate(
|
| 78 |
predicted_depth.unsqueeze(1),
|
| 79 |
size=image.size[::-1],
|
| 80 |
mode="bicubic",
|
| 81 |
align_corners=False,
|
| 82 |
).squeeze()
|
| 83 |
+
depth_image = (prediction.cpu().numpy() - np.min(prediction.cpu().numpy())) / (np.max(prediction.cpu().numpy()) - np.min(prediction.cpu().numpy())) # Normalize
|
| 84 |
depth_image = process_depth_image(depth_image) # Apply processing
|
| 85 |
|
| 86 |
+
# Smoothing and blending
|
| 87 |
depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
|
| 88 |
mask_blurred = cv2.GaussianBlur(depth_image, (5, 5), 1.0)
|
|
|
|
|
|
|
| 89 |
blended_depth_image = cv2.addWeighted(depth_image, 0.7, mask_blurred, 0.3, 0)
|
| 90 |
+
blended_depth_image[foreground_edges_resized > 0] *= 1.2
|
| 91 |
|
| 92 |
+
# Mask creation
|
| 93 |
+
mask = (depth_image > 0).astype(np.uint8)
|
| 94 |
+
mask = clear_border(mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
background = remove(image_raw, only_mask=True)
|
| 96 |
background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
|
| 97 |
background_inpainted = inpaint_background(np.array(image_raw), mask)
|
|
|
|
|
|
|
| 98 |
bg_scaled = cv2.resize(background_inpainted, (int(image.width * 1.2), int(image.height * 1.2)))
|
| 99 |
|
| 100 |
+
# Flatten foreground and add zero depth channel
|
| 101 |
+
foreground_flat = np.array(foreground)[:, :, :3]
|
| 102 |
+
foreground_flat = np.insert(foreground_flat, 2, values=0, axis=2)
|
|
|
|
| 103 |
|
| 104 |
try:
|
| 105 |
+
# Convert depth image to 8-bit grayscale for PNG compatibility
|
| 106 |
+
blended_depth_image_8bit = (blended_depth_image * 255).astype(np.uint8)
|
| 107 |
+
img = Image.fromarray(blended_depth_image_8bit, mode="L")
|
| 108 |
+
|
| 109 |
+
# Create 3D model and return paths
|
| 110 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path)
|
| 111 |
+
model_3d = gr.Model3D() # Placeholder
|
| 112 |
+
return [img, model_3d, gltf_path]
|
|
|
|
|
|
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 115 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
# Re-convert depth to 8-bit grayscale if retrying
|
| 118 |
+
img = Image.fromarray(blended_depth_image_8bit, mode="L")
|
| 119 |
+
model_3d = gr.Model3D() # Placeholder
|
| 120 |
+
return [img, model_3d, gltf_path]
|
| 121 |
+
|
| 122 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 123 |
# Resize background to match the original image size (no cropping)
|
| 124 |
background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
|