-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathdata_util.py
200 lines (153 loc) · 5.79 KB
/
data_util.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import functools
import cv2
import numpy as np
import imageio
from glob import glob
import os
import shutil
import skimage
import pandas as pd
def load_rgb(path, sidelength=None):
img = imageio.imread(path)[:, :, :3]
img = skimage.img_as_float32(img)
img = square_crop_img(img)
if sidelength is not None:
img = cv2.resize(img, (sidelength, sidelength), interpolation=cv2.INTER_AREA)
img -= 0.5
img *= 2.
img = img.transpose(2, 0, 1)
return img
def load_depth(path, sidelength=None):
img = cv2.imread(path, cv2.IMREAD_UNCHANGED).astype(np.float32)
if sidelength is not None:
img = cv2.resize(img, (sidelength, sidelength), interpolation=cv2.INTER_NEAREST)
img *= 1e-4
if len(img.shape) == 3:
img = img[:, :, :1]
img = img.transpose(2, 0, 1)
else:
img = img[None, :, :]
return img
def load_pose(filename):
lines = open(filename).read().splitlines()
if len(lines) == 1:
pose = np.zeros((4, 4), dtype=np.float32)
for i in range(16):
pose[i // 4, i % 4] = lines[0].split(" ")[i]
return pose.squeeze()
else:
lines = [[x[0], x[1], x[2], x[3]] for x in (x.split(" ") for x in lines[:4])]
return np.asarray(lines).astype(np.float32).squeeze()
def load_params(filename):
lines = open(filename).read().splitlines()
params = np.array([float(x) for x in lines[0].split()]).astype(np.float32).squeeze()
return params
def cond_mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def square_crop_img(img):
min_dim = np.amin(img.shape[:2])
center_coord = np.array(img.shape[:2]) // 2
img = img[center_coord[0] - min_dim // 2:center_coord[0] + min_dim // 2,
center_coord[1] - min_dim // 2:center_coord[1] + min_dim // 2]
return img
def train_val_split(object_dir, train_dir, val_dir):
dirs = [os.path.join(object_dir, x) for x in ['pose', 'rgb', 'depth']]
data_lists = [sorted(glob(os.path.join(dir, x)))
for dir, x in zip(dirs, ['*.txt', "*.png", "*.png"])]
cond_mkdir(train_dir)
cond_mkdir(val_dir)
[cond_mkdir(os.path.join(train_dir, x)) for x in ['pose', 'rgb', 'depth']]
[cond_mkdir(os.path.join(val_dir, x)) for x in ['pose', 'rgb', 'depth']]
shutil.copy(os.path.join(object_dir, 'intrinsics.txt'), os.path.join(val_dir, 'intrinsics.txt'))
shutil.copy(os.path.join(object_dir, 'intrinsics.txt'), os.path.join(train_dir, 'intrinsics.txt'))
for data_name, data_ending, data_list in zip(['pose', 'rgb', 'depth'], ['.txt', '.png', '.png'], data_lists):
val_counter = 0
train_counter = 0
for i, item in enumerate(data_list):
if not i % 3:
shutil.copy(item, os.path.join(train_dir, data_name, "%06d" % train_counter + data_ending))
train_counter += 1
else:
shutil.copy(item, os.path.join(val_dir, data_name, "%06d" % val_counter + data_ending))
val_counter += 1
def glob_imgs(path):
imgs = []
for ext in ['*.png', '*.jpg', '*.JPEG', '*.JPG']:
imgs.extend(glob(os.path.join(path, ext)))
return imgs
def read_view_direction_rays(direction_file):
img = cv2.imread(direction_file, cv2.IMREAD_UNCHANGED).astype(np.float32)
img -= 40000
img /= 10000
return img
def shapenet_train_test_split(shapenet_path, synset_id, name, csv_path):
'''
:param synset_id: synset ID as a string.
:param name:
:param csv_path:
:return:
'''
parsed_csv = pd.read_csv(filepath_or_buffer=csv_path)
synset_df = parsed_csv[parsed_csv['synsetId'] == int(synset_id)]
train = synset_df[synset_df['split'] == 'train']
val = synset_df[synset_df['split'] == 'val']
test = synset_df[synset_df['split'] == 'test']
print(len(train), len(val), len(test))
train_path, val_path, test_path = [os.path.join(shapenet_path, str(synset_id) + '_' + name + '_' + x)
for x in ['train', 'val', 'test']]
cond_mkdir(train_path)
cond_mkdir(val_path)
cond_mkdir(test_path)
for split_df, trgt_path in zip([train, val, test], [train_path, val_path, test_path]):
for row_no, row in split_df.iterrows():
try:
shutil.copytree(os.path.join(shapenet_path, str(synset_id), str(row.modelId)),
os.path.join(shapenet_path, trgt_path, str(row.modelId)))
except FileNotFoundError:
print("%s does not exist" % str(row.modelId))
def transform_viewpoint(v):
"""Transforms the viewpoint vector into a consistent representation"""
return np.concatenate([v[:, :3],
np.cos(v[:, 3:4]),
np.sin(v[:, 3:4]),
np.cos(v[:, 4:5]),
np.sin(v[:, 4:5])], 1)
def euler2mat(z=0, y=0, x=0):
Ms = []
if z:
cosz = np.cos(z)
sinz = np.sin(z)
Ms.append(np.array(
[[cosz, -sinz, 0],
[sinz, cosz, 0],
[0, 0, 1]]))
if y:
cosy = np.cos(y)
siny = np.sin(y)
Ms.append(np.array(
[[cosy, 0, siny],
[0, 1, 0],
[-siny, 0, cosy]]))
if x:
cosx = np.cos(x)
sinx = np.sin(x)
Ms.append(np.array(
[[1, 0, 0],
[0, cosx, -sinx],
[0, sinx, cosx]]))
if Ms:
return functools.reduce(np.dot, Ms[::-1])
return np.eye(3)
def look_at(vec_pos, vec_look_at):
z = vec_look_at - vec_pos
z = z / np.linalg.norm(z)
x = np.cross(z, np.array([0., 1., 0.]))
x = x / np.linalg.norm(x)
y = np.cross(x, z)
y = y / np.linalg.norm(y)
view_mat = np.zeros((3, 3))
view_mat[:3, 0] = x
view_mat[:3, 1] = y
view_mat[:3, 2] = z
return view_mat