Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,13 @@ from rembg import remove # For background removal
|
|
| 13 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 14 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def process_image(image_path, depth_map_path=None):
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path)
|
|
@@ -43,12 +50,18 @@ def process_image(image_path, depth_map_path=None):
|
|
| 43 |
|
| 44 |
# Step 3: Generate or use existing depth map
|
| 45 |
if depth_map_path:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
# Generate depth map using the model
|
| 54 |
encoding = feature_extractor(image, return_tensors="pt")
|
|
|
|
| 13 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 14 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 15 |
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
import numpy as np
|
| 18 |
+
import cv2
|
| 19 |
+
from PIL import Image
|
| 20 |
+
import torch
|
| 21 |
+
from rembg import remove # Assuming you're using rembg for background removal
|
| 22 |
+
|
| 23 |
def process_image(image_path, depth_map_path=None):
|
| 24 |
image_path = Path(image_path)
|
| 25 |
image_raw = Image.open(image_path)
|
|
|
|
| 50 |
|
| 51 |
# Step 3: Generate or use existing depth map
|
| 52 |
if depth_map_path:
|
| 53 |
+
if depth_map_path.endswith('.npy'):
|
| 54 |
+
# Load the depth map from a .npy file and invert it
|
| 55 |
+
depth_image = np.load(depth_map_path)
|
| 56 |
+
depth_image = 255 - (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image)) * 255
|
| 57 |
+
depth_image = depth_image.astype('uint8')
|
| 58 |
+
else:
|
| 59 |
+
# Resize user-provided depth map to match RGB size
|
| 60 |
+
depth_image_raw = Image.open(depth_map_path)
|
| 61 |
+
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 62 |
+
depth_image = np.array(depth_image)
|
| 63 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 64 |
+
depth_image = (depth_image * 255).astype('uint8')
|
| 65 |
else:
|
| 66 |
# Generate depth map using the model
|
| 67 |
encoding = feature_extractor(image, return_tensors="pt")
|