-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
128 lines (70 loc) · 2.87 KB
/
data_loader.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import numpy as np
import os
import math
from PIL import Image
import torch
import torch.utils.data as data
from torch.autograd.variable import Variable
image_size_sfm = [416, 128]
def getArray(file : str, four = False) -> np.ndarray :
arr = np.fromfile(file, dtype = 'float32')
arr = arr.reshape((-1, 4))
if four != True:
arr = arr[:, :3]
return arr
def getSingleDepthSfmLearner(img_file, depth_file, channel = 1):
img = Image.open(img_file)
if channel == 1:
img = img.convert('L')
img = np.array(img)
depth = np.load(depth_file)
return img, depth
def findVelo(f, velo_dir):
_, _, _, _, scene, f = f.split(os.path.sep)
velo_file = os.path.join(velo_dir, scene[5:10], scene, 'velodyne_points', 'data', f.replace('jpg', 'bin'))
return velo_file
def getBatchDataFromSfmLearner(root, velo_dir, train = True):
global image_size_sfm
scene_list_path = os.path.join(root, 'train.txt') if train else os.path.join(root, 'val.txt')
scene_list = [os.path.join(root, folder[:-1]) for folder in open(scene_list_path)]
total = []
for scene in scene_list:
if not os.path.isdir(scene):
continue
imgs = [os.path.join(scene, f) for f in os.listdir(scene) if f.find('jpg') != -1]
total.extend(list(map(lambda x : (x, x.replace('jpg', 'npy'), findVelo(x, velo_dir)), imgs)))
total = np.array(total)
return total
def angleOfVector(p1, p2):
p1_norm = p1 / np.linalg.norm(p1)
p2_norm = p2 / np.linalg.norm(p2)
return np.degrees(np.arccos(np.clip(np.dot(p1_norm, p2_norm), -1.0, 1.0)))
def shouldLeft(point, hfov = 45.0, vfov = 35.0):
assert (point.shape == (3,))
h_angle = angleOfVector(np.array([point[0], point[1]]), np.array([1, 0]))
# v_angle = angleOfVector(np.array([point[0], point[2]]), np.array([0, 1]))
# print(h_angle, v_angle)
return h_angle < hfov #and v_angle < vfov
def filterPoint(arr : np.ndarray):
assert (arr.shape[1] == 3)
sign = np.apply_along_axis(lambda x : shouldLeft(x), 1, arr)
return arr[sign]
class DepthData(data.Dataset):
def __init__(self, root, velo_dir, channel, train = True,):
self.data = getBatchDataFromSfmLearner(root, velo_dir, train)
self.channel = channel
def __len__(self):
return len(self.data)
def __getitem__(self, index):
_img, _depth, _velo = self.data[index]
img, depth = getSingleDepthSfmLearner(_img, _depth, channel = self.channel)
if self.channel != 1:
img = np.transpose(img, [2, 1, 0])
else:
img = np.transpose(img)
img = img[np.newaxis, :]
img = Variable(torch.from_numpy(img).float())
depth = np.transpose(depth)
depth = depth[np.newaxis, :]
depth = Variable(torch.from_numpy(depth).float())
return img, depth, _velo