forked from autonomousvision/mip-splatting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_blender_data.py
219 lines (200 loc) · 6.93 KB
/
convert_blender_data.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
# copy from https://github.com/hjxwhy/mipnerf_pl/blob/dev/datasets/convert_blender_data.py
# This file is modified from official mipnerf
import json
import os
import argparse
from os import path
import numpy as np
from PIL import Image
import cv2
def load_renderings(data_dir, split):
"""Load images and metadata from disk."""
f = 'transforms_{}.json'.format(split)
with open(path.join(data_dir, f), 'r') as fp:
meta = json.load(fp)
images = []
cams = []
print('Loading imgs')
for frame in meta['frames']:
fname = os.path.join(data_dir, frame['file_path'] + '.png')
with open(fname, 'rb') as imgin:
image = np.array(Image.open(imgin), dtype=np.float32) / 255.
cams.append(frame['transform_matrix'])
images.append(image)
ret = {}
ret['images'] = np.stack(images, axis=0)
print('Loaded all images, shape is', ret['images'].shape)
ret['camtoworlds'] = np.stack(cams, axis=0)
w = ret['images'].shape[2]
camera_angle_x = float(meta['camera_angle_x'])
ret['focal'] = .5 * w / np.tan(.5 * camera_angle_x)
return ret
def down2(img):
"""box downsampling"""
sh = img.shape
return np.mean(np.reshape(img, [sh[0] // 2, 2, sh[1] // 2, 2, -1]), (1, 3))
def down2_cv(img):
return cv2.resize(img, (img.shape[1] // 2, img.shape[0] // 2))
def convert_to_nerfdata(basedir, newdir, n_down):
"""Convert Blender data to multiscale."""
if not os.path.exists(newdir):
os.makedirs(newdir)
splits = ['train', 'val', 'test']
bigmeta = {}
# Foreach split in the dataset
for split in splits:
print('Split', split)
# Load everything
data = load_renderings(basedir, split)
# Save out all the images
imgdir = 'images_{}'.format(split)
os.makedirs(os.path.join(newdir, imgdir), exist_ok=True)
fnames = []
widths = []
heights = []
focals = []
cam2worlds = []
lossmults = []
labels = []
nears, fars = [], []
f = data['focal']
print('Saving images')
for i, img in enumerate(data['images']):
for j in range(n_down):
fname = '{}/{:03d}_d{}.png'.format(imgdir, i, j)
fnames.append(fname)
fname = os.path.join(newdir, fname)
with open(fname, 'wb') as imgout:
img8 = Image.fromarray(np.uint8(img * 255))
img8.save(imgout)
widths.append(img.shape[1])
heights.append(img.shape[0])
focals.append(f / 2 ** j)
cam2worlds.append(data['camtoworlds'][i].tolist())
lossmults.append(4. ** j)
labels.append(j)
nears.append(2.)
fars.append(6.)
img = down2(img)
# Create metadata
meta = {}
meta['file_path'] = fnames
meta['cam2world'] = cam2worlds
meta['width'] = widths
meta['height'] = heights
meta['focal'] = focals
meta['label'] = labels
meta['near'] = nears
meta['far'] = fars
meta['lossmult'] = lossmults
fx = np.array(focals)
fy = np.array(focals)
cx = np.array(meta['width']) * .5
cy = np.array(meta['height']) * .5
arr0 = np.zeros_like(cx)
arr1 = np.ones_like(cx)
k_inv = np.array([
[arr1 / fx, arr0, -cx / fx],
[arr0, -arr1 / fy, cy / fy],
[arr0, arr0, -arr1],
])
k_inv = np.moveaxis(k_inv, -1, 0)
meta['pix2cam'] = k_inv.tolist()
bigmeta[split] = meta
for k in bigmeta:
for j in bigmeta[k]:
print(k, j, type(bigmeta[k][j]), np.array(bigmeta[k][j]).shape)
jsonfile = os.path.join(newdir, 'metadata.json')
with open(jsonfile, 'w') as f:
json.dump(bigmeta, f, ensure_ascii=False, indent=4)
def convert_to_nerfdata_from_socket(data, n_down):
"""Convert Blender data to multiscale."""
bigmeta = {}
imgs = []
widths = []
heights = []
focals = []
cam2worlds = []
lossmults = []
labels = []
nears, fars = [], []
# pix2cams = []
f = data['focal']
# image_dir = 'images'
# if not os.path.exists(image_dir):
# os.makedirs(image_dir)
# print('Saving images')
for i, img in enumerate(data['img']):
for j in range(n_down):
# fname = '{}/{:03d}_d{}.png'.format(image_dir, i, j)
# with open(fname, 'wb') as imgout:
# img8 = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# img8.save(imgout)
widths.append(img.shape[1])
heights.append(img.shape[0])
focals.append(f[i] / 2 ** j)
cam2worlds.append(data['cam2world'][i].tolist())
lossmults.append(4. ** j)
labels.append(j)
nears.append(2.)
fars.append(6.)
imgs.append(img)
# pix2cams.append(data['pix2cam'][i].tolist())
# img = down2_cv(img)
# Create metadata
meta = {}
meta['img'] = imgs
meta['cam2world'] = cam2worlds
# meta['pix2cam'] = pix2cams
meta['width'] = widths
meta['height'] = heights
meta['focal'] = focals
meta['label'] = labels
meta['near'] = nears
meta['far'] = fars
meta['lossmult'] = lossmults
# json.dump(meta['cam2world'], open('meta.json', 'w'))
fx = np.array(focals)
fy = np.array(focals)
cx = np.array(meta['width']) * .5
cy = np.array(meta['height']) * .5
arr0 = np.zeros_like(cx)
arr1 = np.ones_like(cx)
k_inv = np.array([
[arr1 / fx, arr0, -cx / fx],
[arr0, -arr1 / fy, cy / fy],
[arr0, arr0, -arr1],
])
k_inv = np.moveaxis(k_inv, -1, 0)
meta['pix2cam'] = k_inv.tolist()
bigmeta = {
'test':meta,
'train':meta,
}
return bigmeta
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--blender_dir", help="data root path", type=str,)
parser.add_argument("--object_name", help="While object you want to make multi scale", type=str,
default=None)
parser.add_argument("--out_dir", help="Output directory.", type=str)
parser.add_argument("--n_down", help="Numbers of scale you want to scale.", type=int, default=4)
args = parser.parse_args()
blenderdir = args.blender_dir
outdir = args.out_dir
n_down = args.n_down
if not os.path.exists(outdir):
os.makedirs(outdir)
scenes = os.listdir(blenderdir)
if args.object_name is not None:
scenes = [args.object_name]
dirs = [os.path.join(blenderdir, f) for f in scenes]
dirs = [d for d in dirs if os.path.isdir(d)]
print(dirs)
for basedir in dirs:
print()
newdir = os.path.join(outdir, os.path.basename(basedir))
print('Converting from', basedir, 'to', newdir)
convert_to_nerfdata(basedir, newdir, n_down)
if __name__ == '__main__':
main()