-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestimate_depth.py
52 lines (40 loc) · 1.58 KB
/
estimate_depth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from transformers import pipeline
from PIL import Image
import requests
import os
import numpy as np
import cv2
import torch
def visulize_depth(abs_depth, output_file):
normalized_depth = (1 - ((abs_depth - abs_depth.min()) / (abs_depth.max() - abs_depth.min())))*255.0
# normalized_depth = normalized_depth.numpy()
cv2.imwrite(output_file, normalized_depth)
input_dir = "/home/raja/courses/csci677/HW3/gaussian-splatting/data/10/images"
output_dir = "/home/raja/courses/csci677/HW3/gaussian-splatting/data/10/abs_depth"
# input_dir = "./input"
# output_dir = "./output"
resolution = (1600, 1200)
checkpoint = "vinvino02/glpn-nyu"
depth_estimator = pipeline("depth-estimation", model=checkpoint)
for curr_image in os.listdir(input_dir):
curr_image_path = os.path.join(input_dir, curr_image)
file_name = curr_image.split('.')[0]
print("processing", file_name)
image = Image.open(curr_image_path)
image = image.resize(resolution)
print(image.size)
predictions = depth_estimator(image)
# absolute_depth = predictions["predicted_depth"][0]
predicted_depth = predictions["predicted_depth"]
prediction = torch.nn.functional.interpolate(
predicted_depth.unsqueeze(1),
size=image.size[::-1],
mode="bicubic",
align_corners=False,
).squeeze()
prediction = prediction.numpy()
print(prediction.shape)
output_npy_file = os.path.join(output_dir, file_name + '.npy')
visu_file = os.path.join(output_dir, file_name + '.png')
np.save(output_npy_file, prediction)
visulize_depth(prediction, visu_file)