-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_util.py
128 lines (93 loc) · 3.03 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
import functools
import cv2
import numpy as np
import imageio
from glob import glob
import os
import shutil
import skimage
import h5py
import io
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_NEAREST)
# img -= 0.5
# img *= 2.
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_numpy_hdf5(instance_ds, key):
rgb_ds = instance_ds['rgb']
raw = rgb_ds[key][...]
s = raw.tostring()
f = io.BytesIO(s)
img = imageio.imread(f)[:, :, :3]
img = skimage.img_as_float32(img)
img = square_crop_img(img)
img -= 0.5
img *= 2.
return img
def load_rgb_hdf5(instance_ds, key, sidelength=None):
rgb_ds = instance_ds['rgb']
raw = rgb_ds[key][...]
s = raw.tostring()
f = io.BytesIO(s)
img = imageio.imread(f)[:, :, :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.
return img
def load_pose_hdf5(instance_ds, key):
pose_ds = instance_ds['pose']
raw = pose_ds[key][...]
ba = bytearray(raw)
s = ba.decode('ascii')
lines = s.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]
# processed_pose = pose.squeeze()
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 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 glob_imgs(path):
imgs = []
for ext in ['*.png', '*.jpg', '*.JPEG', '*.JPG']:
imgs.extend(glob(os.path.join(path, ext)))
return imgs