forked from TimSong412/OmniTrackFast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_exhaust.py
More file actions
184 lines (138 loc) · 6.14 KB
/
Copy pathvisualize_exhaust.py
File metadata and controls
184 lines (138 loc) · 6.14 KB
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
import os
import subprocess
import random
import datetime
import shutil
import numpy as np
import torch
import torch.utils.data
import torch.distributed as dist
from config import config_parser
from tensorboardX import SummaryWriter
from loaders.create_training_dataset import get_training_dataset
import time
# from trainer_tcnn import TcnnTrainer
import matplotlib
import imageio
from wis3d import Wis3D
from pathlib import Path
import tqdm
import cv2
import matplotlib.pyplot as plt
from setup_trainer import setup_trainer
def warp_frame_pts(from_id, to_id, trainer, return_canonical=True):
grid0 = trainer.grid[..., 0:2].clone()
depth0 = trainer.get_pred_depth_maps([from_id])[0]
depth0 = depth0[..., None]
pts0 = trainer.unproject(grid0, depth0)
pts0 = pts0.reshape(-1, 3)[None, :, None]
pts1_ptscano = trainer.get_predictions(pts0, [from_id], [to_id], return_canonical=return_canonical)
if return_canonical:
pts1, pts0_canonical = pts1_ptscano
return pts1.reshape(-1, 3), pts0_canonical.reshape(-1, 3)
else:
pts1 = pts1_ptscano
return pts1.reshape(-1, 3)
def vis(args, gap=5):
seq_name = os.path.basename(args.data_dir.rstrip('/'))
now = time.strftime("%y%m%d-%H%M", time.localtime())
out_dir = os.path.join(args.save_dir, '{}_exhaust{}_{}'.format(now, args.expname, seq_name))
os.makedirs(out_dir, exist_ok=True)
# if 'depth' in args.trainer:
# seq_name += "_depth"
print('visualize for {}...\n output is saved in {}'.format(seq_name, out_dir))
args.out_dir = out_dir
wis3d = Wis3D("viscanonical", 'exhaust_'+args.expname+seq_name, "xyz")
print("wis3d dir: ", 'exhaust_'+args.expname+seq_name)
print("vis name", args.expname+'_'+seq_name)
print('=> will save pts to {}'.format(args.expname+'_'+seq_name))
# save the args and config files
f = os.path.join(out_dir, 'args.txt')
with open(f, 'w') as file:
for arg in sorted(vars(args)):
if not arg.startswith('_'):
attr = getattr(args, arg)
file.write('{} = {}\n'.format(arg, attr))
if args.config:
f = os.path.join(out_dir, 'config.txt')
if not os.path.isfile(f):
shutil.copy(args.config, f)
per_frame = True
# get trainer
trainer = setup_trainer(args, eval=True)
N_frames = len(trainer.images)
with torch.no_grad():
for st in tqdm.trange(0, N_frames, gap):
color = trainer.images[st].reshape(-1, 3).cpu().numpy()
mask = trainer.masks[st].reshape(-1).cpu().numpy()
mask_color = plt.cm.hsv(np.linspace(0, 1, mask.sum()))[:, :3]
local_depth = trainer.get_init_depth_maps([st])[0]
local_pts = trainer.unproject(trainer.grid[..., 0:2], local_depth[..., None])
wis3d.set_scene_id(st)
wis3d.add_point_cloud(local_pts.reshape(-1, 3), color, name=f"local_pts")
for to in tqdm.trange(0, N_frames, gap):
try:
wis3d.set_scene_id(to)
pts = warp_frame_pts(st, to, trainer, return_canonical=False)
wis3d.add_point_cloud(pts, color, name=f"pts{st:03d}")
mask_pts = pts[mask]
mask_pts[..., 2] -= 0.001
wis3d.add_point_cloud(mask_pts, mask_color, name=f"mask{st:03d}")
except:
print(f"error in {st} to {to}")
wis3d.set_scene_id(N_frames)
_, pts0_canonical = warp_frame_pts(st, 0, trainer, return_canonical=True)
wis3d.add_point_cloud(pts0_canonical, color, name=f"pts{st:03d}_canonical")
mask_pts = pts0_canonical[mask]
mask_pts[..., 2] -= 0.001
wis3d.add_point_cloud(mask_pts, mask_color, name=f"mask{st:03d}_canonical")
def vis_canonical(args, gap=10):
seq_name = os.path.basename(args.data_dir.rstrip('/'))
now = time.strftime("%y%m%d-%H%M", time.localtime())
out_dir = os.path.join(args.save_dir, '{}_exhaust{}_{}'.format(now, args.expname, seq_name))
os.makedirs(out_dir, exist_ok=True)
# if 'depth' in args.trainer:
# seq_name += "_depth"
print('visualize for {}...\n output is saved in {}'.format(seq_name, out_dir))
args.out_dir = out_dir
wis3d = Wis3D("viscanonical", 'exhaust_'+args.expname+seq_name, "xyz")
print("wis3d dir: ", 'exhaust_'+args.expname+seq_name)
print("vis name", args.expname+'_'+seq_name)
print('=> will save pts to {}'.format(args.expname+'_'+seq_name))
# save the args and config files
f = os.path.join(out_dir, 'args.txt')
with open(f, 'w') as file:
for arg in sorted(vars(args)):
if not arg.startswith('_'):
attr = getattr(args, arg)
file.write('{} = {}\n'.format(arg, attr))
if args.config:
f = os.path.join(out_dir, 'config.txt')
if not os.path.isfile(f):
shutil.copy(args.config, f)
per_frame = True
# get trainer
trainer = setup_trainer(args, eval=True)
N_frames = len(trainer.images)
with torch.no_grad():
for st in tqdm.trange(0, N_frames, gap):
color = trainer.images[st].reshape(-1, 3).cpu().numpy()
mask = trainer.masks[st].reshape(-1).cpu().numpy()
mask_color = plt.cm.hsv(np.linspace(0, 1, mask.sum()))[:, :3]
local_depth = trainer.get_init_depth_maps([st])[0]
local_pts = trainer.unproject(trainer.grid[..., 0:2], local_depth[..., None])
_, pts0_canonical = warp_frame_pts(st, 0, trainer, return_canonical=True)
pts0_canonical[..., 2] *= 0.1
pts0_canonical[..., 0] *= 10
wis3d.add_point_cloud(pts0_canonical, color, name=f"pts{st:03d}_canonical")
if __name__ == '__main__':
args = config_parser()
# vis_gtdepth(Path("dataset/soapbox"))
# vis_flow(args)
args.save_dir = "vis_out"
# vis(args, gap=10)
vis_canonical(args, gap=5)
# vis_eval(args)
# visRT(args)
# vis(args)
# vis_couple(args, frameid = 30)