Update app.py
Browse files
app.py
CHANGED
|
@@ -22,16 +22,23 @@ def process_image(image_path, depth_map_path=None):
|
|
| 22 |
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
| 23 |
Image.Resampling.LANCZOS,
|
| 24 |
)
|
|
|
|
| 25 |
|
| 26 |
# Step 1: Perform background removal
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Load the foreground image from the returned file path
|
| 33 |
foreground_image_path = result # This contains the path to the processed image
|
| 34 |
foreground_image = Image.open(foreground_image_path) # Open the image
|
|
|
|
| 35 |
|
| 36 |
if depth_map_path:
|
| 37 |
# Load and resize the user-provided depth map to match the RGB image size
|
|
@@ -42,6 +49,8 @@ def process_image(image_path, depth_map_path=None):
|
|
| 42 |
# Normalize depth map if needed
|
| 43 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 44 |
depth_image = (depth_image * 255).astype('uint8')
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
# Automatically generate the depth map using the DPT model
|
| 47 |
encoding = feature_extractor(image, return_tensors="pt")
|
|
@@ -57,6 +66,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 57 |
align_corners=False,
|
| 58 |
).squeeze()
|
| 59 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
|
|
|
| 60 |
|
| 61 |
# Step 2: Create flat background depth layer and combine with foreground
|
| 62 |
background_depth_value = 20 # Slight depth for background
|
|
@@ -67,6 +77,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 67 |
|
| 68 |
# Combine the flat background with the foreground depth
|
| 69 |
combined_depth_image = np.where(foreground_mask_resized[..., None], depth_image[..., None], background_depth[..., None])
|
|
|
|
| 70 |
|
| 71 |
# Step 3: Generate 3D model using the combined depth map
|
| 72 |
gltf_path = create_3d_obj(np.array(image), combined_depth_image, image_path)
|
|
|
|
| 22 |
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
| 23 |
Image.Resampling.LANCZOS,
|
| 24 |
)
|
| 25 |
+
print("Image loaded and resized successfully.")
|
| 26 |
|
| 27 |
# Step 1: Perform background removal
|
| 28 |
+
try:
|
| 29 |
+
result = client.predict(
|
| 30 |
+
f=str(image_path), # Convert Path to string
|
| 31 |
+
api_name="/png"
|
| 32 |
+
)
|
| 33 |
+
print("Background removal completed successfully.")
|
| 34 |
+
except Exception as bg_removal_exception:
|
| 35 |
+
print(f"Background removal failed: {bg_removal_exception}")
|
| 36 |
+
raise
|
| 37 |
|
| 38 |
# Load the foreground image from the returned file path
|
| 39 |
foreground_image_path = result # This contains the path to the processed image
|
| 40 |
foreground_image = Image.open(foreground_image_path) # Open the image
|
| 41 |
+
print("Foreground image loaded successfully.")
|
| 42 |
|
| 43 |
if depth_map_path:
|
| 44 |
# Load and resize the user-provided depth map to match the RGB image size
|
|
|
|
| 49 |
# Normalize depth map if needed
|
| 50 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 51 |
depth_image = (depth_image * 255).astype('uint8')
|
| 52 |
+
print("Depth map loaded and normalized successfully.")
|
| 53 |
+
|
| 54 |
else:
|
| 55 |
# Automatically generate the depth map using the DPT model
|
| 56 |
encoding = feature_extractor(image, return_tensors="pt")
|
|
|
|
| 66 |
align_corners=False,
|
| 67 |
).squeeze()
|
| 68 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 69 |
+
print("Depth map generated successfully.")
|
| 70 |
|
| 71 |
# Step 2: Create flat background depth layer and combine with foreground
|
| 72 |
background_depth_value = 20 # Slight depth for background
|
|
|
|
| 77 |
|
| 78 |
# Combine the flat background with the foreground depth
|
| 79 |
combined_depth_image = np.where(foreground_mask_resized[..., None], depth_image[..., None], background_depth[..., None])
|
| 80 |
+
print("Combined depth image created successfully.")
|
| 81 |
|
| 82 |
# Step 3: Generate 3D model using the combined depth map
|
| 83 |
gltf_path = create_3d_obj(np.array(image), combined_depth_image, image_path)
|