-
Notifications
You must be signed in to change notification settings - Fork 12
/
depth.py
57 lines (42 loc) · 1.36 KB
/
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
53
54
55
56
57
#!/usr/bin/env python3
import os
import cv2
import glob
import torch
import numpy as np
import urllib.request
from PIL import Image, ImageOps
import torchvision.transforms as transforms
use_large_model = True
if use_large_model:
midas = torch.hub.load('intel-isl/MiDaS', 'DPT_Large')
else:
midas = torch.hub.load('intel-isl/MiDaS', 'MiDaS_small')
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
midas.to(device)
midas.eval()
midas_transforms = torch.hub.load('intel-isl/MiDaS', 'transforms')
if use_large_model:
transform = midas_transforms.dpt_transform
print('Using large (slow) model.')
else:
transform = midas_transforms.small_transform
print('Using small (fast) model.')
for file in glob.glob('./rgb/*.jpg'):
img = cv2.imread(file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
input_batch = transform(img).to(device)
with torch.no_grad():
prediction = midas(input_batch)
prediction = torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size = img.shape[:2],
mode = 'bicubic',
align_corners = False,
).squeeze()
output = prediction.cpu().numpy()
output_normalized = (output * 255 / np.max(output)).astype('uint8')
output_image = Image.fromarray(output_normalized)
output_image_converted = output_image.convert('RGB').save(file.replace('rgb', 'depth'))
print('Converted: ' + file)
print('Done.')