Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,20 @@ import cv2
|
|
4 |
import numpy as np
|
5 |
from torchvision import transforms
|
6 |
from PIL import Image
|
7 |
-
from transformers import DPTForDepthEstimation, DPTFeatureExtractor, MidasForDepthEstimation, MidasImageProcessor
|
8 |
|
9 |
-
# Load depth estimation model
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
def estimate_depth(image):
|
16 |
-
"""Estimate depth map
|
17 |
image = image.convert("RGB")
|
18 |
-
|
19 |
with torch.no_grad():
|
20 |
-
|
21 |
-
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
22 |
depth = cv2.resize(depth, (image.width, image.height))
|
23 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
24 |
return depth.astype(np.uint8)
|
@@ -82,4 +81,4 @@ iface = gr.Interface(
|
|
82 |
)
|
83 |
|
84 |
if __name__ == "__main__":
|
85 |
-
iface.launch(share=True)
|
|
|
4 |
import numpy as np
|
5 |
from torchvision import transforms
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
+
# Load MiDaS depth estimation model from torch.hub
|
9 |
+
midas_model = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
|
10 |
+
midas_model.eval()
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
midas_model.to(device)
|
13 |
+
midas_transform = torch.hub.load("intel-isl/MiDaS", "transforms").default_transform
|
14 |
|
15 |
def estimate_depth(image):
|
16 |
+
"""Estimate depth map using MiDaS v3."""
|
17 |
image = image.convert("RGB")
|
18 |
+
input_tensor = midas_transform(image).unsqueeze(0).to(device)
|
19 |
with torch.no_grad():
|
20 |
+
depth = midas_model(input_tensor).squeeze().cpu().numpy()
|
|
|
21 |
depth = cv2.resize(depth, (image.width, image.height))
|
22 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
23 |
return depth.astype(np.uint8)
|
|
|
81 |
)
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
+
iface.launch(share=True)
|