-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_io.py
178 lines (141 loc) · 6.58 KB
/
file_io.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
#####################################################################
# This file is part of the 4D Light Field Benchmark. #
# #
# This work is licensed under the Creative Commons #
# Attribution-NonCommercial-ShareAlike 4.0 International License. #
# To view a copy of this license, #
# visit http://creativecommons.org/licenses/by-nc-sa/4.0/. #
#####################################################################
import ConfigParser
import os
import sys
import numpy as np
def read_lightfield(data_folder):
params = read_parameters(data_folder)
light_field = np.zeros((params["num_cams_x"], params["num_cams_y"], params["height"], params["width"], 3), dtype=np.uint8)
views = sorted([f for f in os.listdir(data_folder) if f.startswith("input_") and f.endswith(".png")])
for idx, view in enumerate(views):
fpath = os.path.join(data_folder, view)
try:
img = read_img(fpath)
light_field[idx / params["num_cams_x"], idx % params["num_cams_y"], :, :, :] = img
except IOError:
print "Could not read input file: %s" % fpath
sys.exit()
return light_field
def read_parameters(data_folder):
params = dict()
with open(os.path.join(data_folder, "parameters.cfg"), "r") as f:
parser = ConfigParser.ConfigParser()
parser.readfp(f)
section = "intrinsics"
params["width"] = int(parser.get(section, 'image_resolution_x_px'))
params["height"] = int(parser.get(section, 'image_resolution_y_px'))
params["focal_length_mm"] = float(parser.get(section, 'focal_length_mm'))
params["sensor_size_mm"] = float(parser.get(section, 'sensor_size_mm'))
params["fstop"] = float(parser.get(section, 'fstop'))
section = "extrinsics"
params["num_cams_x"] = int(parser.get(section, 'num_cams_x'))
params["num_cams_y"] = int(parser.get(section, 'num_cams_y'))
params["baseline_mm"] = float(parser.get(section, 'baseline_mm'))
params["focus_distance_m"] = float(parser.get(section, 'focus_distance_m'))
params["center_cam_x_m"] = float(parser.get(section, 'center_cam_x_m'))
params["center_cam_y_m"] = float(parser.get(section, 'center_cam_y_m'))
params["center_cam_z_m"] = float(parser.get(section, 'center_cam_z_m'))
params["center_cam_rx_rad"] = float(parser.get(section, 'center_cam_rx_rad'))
params["center_cam_ry_rad"] = float(parser.get(section, 'center_cam_ry_rad'))
params["center_cam_rz_rad"] = float(parser.get(section, 'center_cam_rz_rad'))
section = "meta"
params["disp_min"] = float(parser.get(section, 'disp_min'))
params["disp_max"] = float(parser.get(section, 'disp_max'))
params["frustum_disp_min"] = float(parser.get(section, 'frustum_disp_min'))
params["frustum_disp_max"] = float(parser.get(section, 'frustum_disp_max'))
params["depth_map_scale"] = float(parser.get(section, 'depth_map_scale'))
params["scene"] = parser.get(section, 'scene')
params["category"] = parser.get(section, 'category')
params["date"] = parser.get(section, 'date')
params["version"] = parser.get(section, 'version')
params["authors"] = parser.get(section, 'authors').split(", ")
params["contact"] = parser.get(section, 'contact')
return params
def read_depth(data_folder, highres=False):
fpath = os.path.join(data_folder, "gt_depth_%s.pfm" % ("highres" if highres else "lowres"))
try:
data = read_pfm(fpath)
except IOError:
print "Could not read depth file: %s" % fpath
sys.exit()
return data
def read_disparity(data_folder, highres=False):
fpath = os.path.join(data_folder, "gt_disp_%s.pfm" % ("highres" if highres else "lowres"))
try:
data = read_pfm(fpath)
except IOError:
print "Could not read disparity file: %s" % fpath
sys.exit()
return data
def read_img(fpath):
from scipy import misc
data = misc.imread(fpath)
return data
def write_hdf5(data, fpath):
import h5py
h = h5py.File(fpath, 'w')
for key, value in data.iteritems():
h.create_dataset(key, data=value)
h.close()
def write_pfm(data, fpath, scale=1, file_identifier="Pf", dtype="float32"):
# PFM format definition: http://netpbm.sourceforge.net/doc/pfm.html
data = np.flipud(data)
height, width = np.shape(data)[:2]
values = np.ndarray.flatten(np.asarray(data, dtype=dtype))
endianess = data.dtype.byteorder
print endianess
if endianess == '<' or (endianess == '=' and sys.byteorder == 'little'):
scale *= -1
with open(fpath, 'wb') as file:
file.write(file_identifier + '\n')
file.write('%d %d\n' % (width, height))
file.write('%d\n' % scale)
file.write(values)
def read_pfm(fpath, expected_identifier="Pf"):
# PFM format definition: http://netpbm.sourceforge.net/doc/pfm.html
with open(fpath, 'rb') as f:
# header
identifier = _get_next_line(f)
if identifier != expected_identifier:
raise Exception('Unknown identifier. Expected: "%s", got: "%s".' % (expected_identifier, identifier))
try:
line_dimensions = _get_next_line(f)
dimensions = line_dimensions.split(' ')
width = int(dimensions[0].strip())
height = int(dimensions[1].strip())
except:
raise Exception('Could not parse dimensions: "%s". '
'Expected "width height", e.g. "512 512".' % line_dimensions)
try:
line_scale = _get_next_line(f)
scale = float(line_scale)
assert scale != 0
if scale < 0:
endianness = "<"
else:
endianness = ">"
except:
raise Exception('Could not parse max value / endianess information: "%s". '
'Should be a non-zero number.' % line_scale)
try:
data = np.fromfile(f, "%sf" % endianness)
data = np.reshape(data, (height, width))
data = np.flipud(data)
with np.errstate(invalid="ignore"):
data *= abs(scale)
except:
raise Exception('Invalid binary values. Could not create %dx%d array from input.' % (height, width))
return data
def _get_next_line(f):
next_line = f.readline().rstrip()
# ignore comments
while next_line.startswith('#'):
next_line = f.readline().rstrip()
return next_line