-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
executable file
·134 lines (94 loc) · 3.33 KB
/
utils.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
128
129
130
131
132
133
134
# Filename: utils.py
# License: LICENSES/LICENSE_UVIC_EPFL
import gzip
import pickle
import numpy as np
import h5py
def np_skew_symmetric(v):
zero = np.zeros_like(v[:, 0])
M = np.stack([
zero, -v[:, 2], v[:, 1],
v[:, 2], zero, -v[:, 0],
-v[:, 1], v[:, 0], zero,
], axis=1)
return M
def denorm_points(x, T):
x = (x - np.array([T[0,2], T[1,2]])) / np.asarray([T[0,0], T[1,1]])
return x
def compute_T_with_imagesize(w, h, f=None, ratio=1.0):
cx = (w - 1.0) * 0.5
cy = (h - 1.0) * 0.5
mean = np.array([cx, cy])
if f is not None:
f = f
else:
f = max(w - 1.0, h - 1.0) * ratio
scale = 1.0 / f
T = np.zeros((3, 3,))
T[0, 0], T[1, 1], T[2, 2] = scale, scale, 1
T[0, 2], T[1, 2] = -scale * mean[0], -scale * mean[1]
return T.copy()
def norm_points(x):
x_mean = np.mean(x, axis=0)
dist = x - x_mean
meandist = np.sqrt((dist**2).sum(axis=1)).mean()
scale = np.sqrt(2) / meandist
T = np.zeros([3,3])
T[0,0], T[1,1], T[2,2] = scale, scale, 1
T[0,2], T[1,2] = -scale*x_mean[0], -scale*x_mean[1]
x = x * np.asarray([T[0,0], T[1,1]]) + np.array([T[0,2], T[1,2]])
return x, T
def norm_points_with_T(x, T):
x = x * np.asarray([T[0,0], T[1,1]]) + np.array([T[0,2], T[1,2]])
return x
def savepklz(data_to_dump, dump_file_full_name, force_run=False):
''' Saves a pickle object and gzip it '''
if not force_run:
raise RuntimeError("This function should no longer be used!")
with gzip.open(dump_file_full_name, 'wb') as out_file:
pickle.dump(data_to_dump, out_file)
def loadpklz(dump_file_full_name, force_run=False):
''' Loads a gziped pickle object '''
if not force_run:
raise RuntimeError("This function should no longer be used!")
with gzip.open(dump_file_full_name, 'rb') as in_file:
dump_data = pickle.load(in_file)
return dump_data
def saveh5(dict_to_dump, dump_file_full_name):
''' Saves a dictionary as h5 file '''
with h5py.File(dump_file_full_name, 'w') as h5file:
if isinstance(dict_to_dump, list):
for i, d in enumerate(dict_to_dump):
newdict = {'dict' + str(i): d}
writeh5(newdict, h5file)
else:
writeh5(dict_to_dump, h5file)
def writeh5(dict_to_dump, h5node):
''' Recursive function to write dictionary to h5 nodes '''
for _key in dict_to_dump.keys():
if isinstance(dict_to_dump[_key], dict):
h5node.create_group(_key)
cur_grp = h5node[_key]
writeh5(dict_to_dump[_key], cur_grp)
else:
h5node[_key] = dict_to_dump[_key]
def loadh5(dump_file_full_name):
''' Loads a h5 file as dictionary '''
try:
with h5py.File(dump_file_full_name, 'r') as h5file:
dict_from_file = readh5(h5file)
except Exception as e:
print("Error while loading {}".format(dump_file_full_name))
raise e
return dict_from_file
def readh5(h5node):
''' Recursive function to read h5 nodes as dictionary '''
dict_from_file = {}
for _key in h5node.keys():
if isinstance(h5node[_key], h5py._hl.group.Group):
dict_from_file[_key] = readh5(h5node[_key])
else:
dict_from_file[_key] = h5node[_key].value
return dict_from_file
#
# utils.py ends here