Create depth_estimation.py
Browse files- depth_estimation.py +44 -0
depth_estimation.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing the requirements
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
6 |
+
|
7 |
+
|
8 |
+
# Load the model and feature extractor
|
9 |
+
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-beit-large-512")
|
10 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-beit-large-512")
|
11 |
+
|
12 |
+
|
13 |
+
# Function to process an image and return the formatted depth map as an image
|
14 |
+
def process_image(image):
|
15 |
+
"""
|
16 |
+
Preprocesses an image, passes it through a model, and returns the formatted depth map as an image.
|
17 |
+
|
18 |
+
Args:
|
19 |
+
image (PIL.Image.Image): The input image.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
PIL.Image.Image: The formatted depth map as an image.
|
23 |
+
"""
|
24 |
+
|
25 |
+
# Preprocess the image for the model
|
26 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
27 |
+
|
28 |
+
# Forward pass through the model
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model(**encoding)
|
31 |
+
predicted_depth = outputs.predicted_depth
|
32 |
+
|
33 |
+
# Interpolate the predicted depth map to the original image size
|
34 |
+
prediction = torch.nn.functional.interpolate(
|
35 |
+
predicted_depth.unsqueeze(1),
|
36 |
+
size=image.size[::-1],
|
37 |
+
mode="bicubic",
|
38 |
+
align_corners=False,
|
39 |
+
).squeeze()
|
40 |
+
output = prediction.cpu().numpy()
|
41 |
+
formatted = (output * 255 / np.max(output)).astype("uint8")
|
42 |
+
|
43 |
+
# Return the formatted depth map as an image
|
44 |
+
return Image.fromarray(formatted)
|