forked from Jarrome/IMT_Mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pose_fmt.py
executable file
·143 lines (111 loc) · 3.66 KB
/
pose_fmt.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
import os
import numpy as np
from itertools import chain
from collections import defaultdict
from pyquaternion import Quaternion
import glob
import time
import ifr.ifr_main as ifr
from ifr.ifr_main import vis_param
import pdb
# namespace
args = vis_param.args
sequence = vis_param.sequence
# scene name
scene = args.scene
# where the pose stream is
elasticfusion_pose_folder = args.pose_folder
# where we store the incremental result for demonstration
os.makedirs(args.outdir,exist_ok=True)
contains = glob.glob(elasticfusion_pose_folder+'poses_*.txt')
idxs = [int(ct.split('.')[-2].split('_')[-1]) for ct in contains]
contains_dict = {}
for i,idx in enumerate(idxs):
contains_dict[idx] = contains[i]
def read_elasticfusion_file(pose_id, kf_idx):
with open(contains_dict[pose_id]) as f:
lines = f.readlines()
poses = []
for line_id, line in enumerate(lines):
if line_id in kf_idx:
vs = [float(v) for v in line.strip().split(' ')]
v_t = vs[1:4]
#v_q = vs[4:] # xyzw
v_q = Quaternion(vs[-1],*vs[4:-1])
pose = v_q.transformation_matrix
pose[:3,3] = np.array(v_t)
poses.append(pose)
return poses
if __name__ == '__main__':
import os
import sys
from dataset_ptam import TUMRGBDDataset, ICLNUIMDataset, ReplicaRGBDDataset
dataset = args.dataset_type
if 'tum' in dataset.lower():
dataset = TUMRGBDDataset(sequence.path)
elif 'replica' in dataset.lower():
dataset = ReplicaRGBDDataset(sequence.path)
else:
assert "Not supported data type"
'''
load gt traj to check correctness
'''
GT = args.use_gt
if GT:
gt_traj = np.genfromtxt(str(sequence.path)+'/livingRoom'+scene+'.gt.freiburg')
gt_poses = []
durations = []
data_i = 0
kf_idx = []
def run_algo(vis):
global data_i
i = data_i#data_next()
data_i += 1
if i % 20 == 0:#
is_keyframe = True
kf_idx.append(i)
else:
is_keyframe = False
if dataset.timestamps is None:
timestamp = i / 20.
else:
timestamp = dataset.timestamps[i]
time_start = time.time()
# 0. check if current keyframe
if is_keyframe:
gt_pose = gt_traj[i,:] if GT else None
# 1. prepare current frame to get torch frame_data
frame_data = (dataset.rgb[i],dataset.depth[i])
# 2. get all the poses of keyframe
new_poses = []
if not GT:
poses = read_elasticfusion_file(i, kf_idx)
new_poses= poses
else:
gt_poses.append(gt_pose)
new_poses = gt_poses
# 3.2 if some pose changed, update map
ifr.refresh(frame_data, new_poses, frame_id = i, vis=vis, ptam_p = not GT, scene_name = 'lrkt'+scene)
else:
return
duration = time.time() - time_start
durations.append(duration)
print('duration', duration)
print()
print()
if ifr.engine:
ifr.engine.register_animation_callback(callback_func = run_algo)
vis_ph = ifr.vis_util.wireframe_bbox([-4., -4., -4.], [4., 4., 4.])
ifr.engine.add_geometry(vis_ph)
ifr.engine.remove_geometry(vis_ph, reset_bounding_box=False)
ifr.engine.run()
ifr.engine.destroy_window()
else:
try:
while True:
run_algo(None)
except Exception as e:
print(e)
print('num frames', len(durations))
print('num keyframes', len(kf_idx))
print('average time', np.mean(durations))