-
Notifications
You must be signed in to change notification settings - Fork 5
/
transform_dataloader.py
283 lines (210 loc) · 8.81 KB
/
transform_dataloader.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import numpy as np
import torch
import cv2
import struct
import json
def convert_sfm_pose_to_nerf(transform):
"""
Convert camera pose from COLMAP to a transform for rendering
"""
c2w = np.linalg.inv(transform)
flip_mat = np.array([
[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, 1]
])
return np.matmul(c2w, flip_mat)
def qvec2rotmat(qvec):
"""
Converts a quartonian to a rotation matrix
"""
return np.array([
[
1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]
], [
2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]
], [
2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
1 - 2 * qvec[1]**2 - 2 * qvec[2]**2
]
])
def read_next_bytes(fid, num_bytes, format_char_sequence, endian_character="<"):
"""
Reads in the next byte from a bin file
"""
return struct.unpack(endian_character + format_char_sequence, fid.read(num_bytes))
def get_colmap_bin_intrinsics(file_path):
"""
Calculates camera intrinsics from a COLMAP bin file
"""
camera_intrinsics = {}
with open(file_path, "rb") as colmap_file:
num_cameras = read_next_bytes(colmap_file, 8, "Q")[0]
for _ in range(num_cameras):
elems = read_next_bytes(
colmap_file, num_bytes=56, format_char_sequence="iiQQdddd"
)
camera_id = elems[0]
if elems[1] != 1:
raise AttributeError("Colmap cameras bin must be Pinhole camera type")
camera_intrinsics[camera_id] = elems[2:]
return camera_intrinsics
def get_colmap_txt_intrinsics(file_path):
"""
Calculates camera intrinsics from a COLMAP txt file
"""
camera_intrinsics = {}
with open(file_path, "r") as colmap_file:
for line in colmap_file:
line = line.strip()
if len(line) != 0 and line[0] == "#":
continue
elems=line.split(" ")
camera_id = int(elems[0])
if elems[1].lower().strip() != "pinhole":
raise AttributeError("Colmap cameras txt must be Pinhole camera type")
camera_intrinsics[camera_id] = elems[2:]
return camera_intrinsics
def get_colmap_img_transform(elems):
"""
Calculates transforms for cameras from a COLMAP line
"""
bottom = np.array([0.0, 0.0, 0.0, 1.0]).reshape([1, 4])
image_id = str(elems[0])
qvec = np.array(tuple(map(float, elems[1:5])))
tvec = np.array(tuple(map(float, elems[5:8])))
R = qvec2rotmat(-qvec)
t = tvec.reshape([3,1])
c2w = np.concatenate([np.concatenate([R, t], 1), bottom], 0)
c2w_flipped = convert_sfm_pose_to_nerf(c2w)
return c2w_flipped.tolist()
def load_colmap_bin_data(input_path, skip_rate=0):
"""
Load in transforms and camera intrinsics from a COLMAP directory of bin files
"""
colmap_transforms = {}
transform_cameras = {}
transform_file_path = os.path.join(input_path, "images.bin")
intrinsics_file_path = os.path.join(input_path, "cameras.bin")
colmap_cameras = get_colmap_bin_intrinsics(intrinsics_file_path)
i = 0
with open(transform_file_path, "rb") as colmap_file:
num_reg_images = read_next_bytes(colmap_file, 8, "Q")[0]
for _ in range(num_reg_images):
elems = read_next_bytes(
colmap_file, num_bytes=64, format_char_sequence="idddddddi"
)
image_id = elems[0]
transform = get_colmap_img_transform(elems)
camera_id = elems[8]
binary_image_name = b""
current_char = read_next_bytes(colmap_file, 1, "c")[0]
while current_char != b"\x00": # look for the ASCII 0 entry
binary_image_name += current_char
current_char = read_next_bytes(colmap_file, 1, "c")[0]
name = binary_image_name.decode("utf-8")
num_points2D = read_next_bytes(
colmap_file, num_bytes=8, format_char_sequence="Q"
)[0]
x_y_id_s = read_next_bytes(
colmap_file,
num_bytes=24 * num_points2D,
format_char_sequence="ddq" * num_points2D,
)
if i % (skip_rate + 1) == 0:
colmap_transforms[name] = transform
transform_cameras[name] = colmap_cameras[camera_id]
i += 1
return colmap_transforms, transform_cameras
def load_colmap_txt_data(input_path, skip_rate=0):
"""
Load in poses and camera intrinsics from a COLMAP directory of txt files
"""
colmap_transforms = {}
transform_cameras = {}
i = 0
transform_file_path = os.path.join(input_path, "images.txt")
intrinsics_file_path = os.path.join(input_path, "cameras.txt")
colmap_cameras = get_colmap_txt_intrinsics(intrinsics_file_path)
with open(transform_file_path, "r") as colmap_file:
for line in colmap_file:
line = line.strip()
if len(line) != 0 and line[0] == "#":
continue
i = i + 1
if len(line) == 0:
continue
if i % 2 == 1:
if i % (skip_rate + 1) == 0:
elems = line.split(" ")
camera_id = int(elems[8])
name = str(elems[9])
transform = get_colmap_img_transform(elems)
colmap_transforms[name] = transform
transform_cameras[name] = colmap_cameras[camera_id]
return colmap_transforms, transform_cameras
def get_transform_intrinsics(transforms, fname):
"""
Reads in camera intrinsics from a transforms dictionary
"""
intrinsics = [0, 0, 0, 0]
intrinsics[2] = transforms["fl_x"]
if "fl_y" in transforms.keys():
intrinsics[3] = transforms["fl_y"]
else:
# Assuming that focal lengths are same in both dimensions
intrinsics[3] = intrinsics[2]
if "w" in transforms and "h" in transforms:
intrinsics[0] = transforms["w"]
intrinsics[1] = transforms["h"]
else:
img_pixels = cv2.imread(fname)
intrinsics[0] = img_pixels.shape[1]
intrinsics[1] = img_pixels.shape[0]
return intrinsics
def load_transform_json_data(input_path, skip_rate=0):
"""
Load in poses and camera intrinsics from a transforms JSON file
"""
with open(input_path, "r") as transform_file:
transforms = json.load(transform_file)
json_transforms = {}
intrinsics = {}
all_intrinsics = None
if "fl_x" in transforms.keys():
all_intrinsics = get_transform_intrinsics(transforms, transforms["frames"][0]["file_path"])
for i, frame in enumerate(transforms["frames"]):
fname = os.path.basename(frame["file_path"])
transform = frame["transform_matrix"]
if all_intrinsics is None:
intrinsics[fname] = get_transform_intrinsics(frame, frame["file_path"])
else:
intrinsics[fname] = all_intrinsics
if i % (skip_rate + 1) == 0:
json_transforms[fname] = transform
return json_transforms, intrinsics
def load_transform_data(input_path, skip_rate=0):
if os.path.isdir(input_path):
if os.path.exists(os.path.join(input_path, "images.txt")):
return load_colmap_txt_data(input_path, skip_rate=skip_rate)
if os.path.exists(os.path.join(input_path, "images.bin")):
return load_colmap_bin_data(input_path, skip_rate=skip_rate)
# Check if transforms directory path is of the standard 3DGS dataset convention
input_path = os.path.join(input_path, "sparse", "0")
if os.path.exists(input_path):
if os.path.exists(os.path.join(input_path, "images.txt")):
return load_colmap_txt_data(input_path, skip_rate=skip_rate)
if os.path.exists(os.path.join(input_path, "images.bin")):
return load_colmap_bin_data(input_path, skip_rate=skip_rate)
else:
file_extension = os.path.splitext(input_path)[1]
if file_extension == ".json":
return load_transform_json_data(input_path, skip_rate=skip_rate)
raise AttributeError("Unsupported transform data type")