-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_uturn.py
76 lines (70 loc) · 3.75 KB
/
make_uturn.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
from PIL import Image
import numpy as np
import os, pickle, tqdm
import matplotlib.pyplot
def right_shift(pil_img, start, num_turn):
assert(0 <= start and start < num_turn)
img = np.array(pil_img)
h, w = img.shape[:2]
unit = w // num_turn
assert((num_turn * unit) == w)
img = np.hstack([img[:,(start*unit):], img[:,:(start*unit)]])
return Image.fromarray(img)
def right_shift_unproj(pkl_unproj, start, num_turn):
assert(0 <= start and start < num_turn)
output, w, h = {}, 1024, 512
for k in range(6):
img = np.array(pkl_unproj[f'w{w}xh{h}'][2::3]).astype(np.int64).reshape((h, w))
unit = w // num_turn
assert((num_turn * unit) == w)
img = np.hstack([img[:,(start*unit):], img[:,:(start*unit)]])
x, y = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0]))
output[f'w{w}xh{h}'] = np.stack([y,x,img],axis=2).astype(np.int64).flatten().tolist()
w //= 2
h //= 2
return output
if __name__ == '__main__':
num_turn = 32
vids = ['000','003','004','005','015','016','018','025','032','033','035','074','077','078','081','082','083','084','086','088','103','110','114','117','158','162','163','165','176','178','199','200','202','203','212','214','217','223','228','240','257','261','267','279','280','284','285','287','289','290']
vids = [int(vid) for vid in vids]
for i in tqdm.tqdm(vids):
pre = 'london_vid2vid_xiaohu_test'
for prefix, ext in [(f'{pre}/images', '.jpg'), (f'{pre}/seg_maps', '.png')]:
final_imgs = []
for j in range(2,17):
img = Image.open(f'{prefix}/stuttgart_00_{i:03d}/stuttgart_00_000000_{j:06d}_leftImg8bit{ext}')
final_imgs.append(img)
for j in range(15):
final_imgs.append(right_shift(final_imgs[14], j+1, num_turn))
for j in range(15):
final_imgs.append(right_shift(final_imgs[14-j], 16, num_turn))
for j in range(15):
final_imgs.append(right_shift(final_imgs[0], j+17, num_turn))
# final_imgs[0].save('tmp.gif',loop=0,duration=200,save_all=True,append_images=final_imgs[1:])
# input('press')
final_imgs = final_imgs[0:1] + final_imgs
final_imgs = final_imgs[0:1] + final_imgs
for j, final_img in enumerate(final_imgs):
new_pre = prefix.replace(pre, f'{pre}uturn')
os.makedirs(f'{new_pre}/stuttgart_00_{i:03d}', exist_ok=True)
final_img.save(f'{new_pre}/stuttgart_00_{i:03d}/stuttgart_00_000000_{j:06d}_leftImg8bit{ext}')
# quit()
for prefix, ext in [(f'{pre}/unprojections', '.pkl')]:
final_imgs = []
for j in range(2,17):
with open(f'{prefix}/stuttgart_00_{i:03d}/stuttgart_00_000000_{j:06d}_leftImg8bit{ext}', 'rb') as f:
final_imgs.append(pickle.load(f))
# final_imgs.append(right_shift_unproj(pickle.load(f), 0, num_turn))
for j in range(15):
final_imgs.append(right_shift_unproj(final_imgs[14], j+1, num_turn))
for j in range(15):
final_imgs.append(right_shift_unproj(final_imgs[14-j], 16, num_turn))
for j in range(15):
final_imgs.append(right_shift_unproj(final_imgs[0], j+17, num_turn))
final_imgs = final_imgs[0:1] + final_imgs
final_imgs = final_imgs[0:1] + final_imgs
for j, final_img in enumerate(final_imgs):
new_pre = prefix.replace(pre, f'{pre}uturn')
os.makedirs(f'{new_pre}/stuttgart_00_{i:03d}', exist_ok=True)
pickle.dump(final_img, open(f'{new_pre}/stuttgart_00_{i:03d}/stuttgart_00_000000_{j:06d}_leftImg8bit{ext}', 'wb'))
# quit()