-
Notifications
You must be signed in to change notification settings - Fork 29
/
keyframe_buffer.py
129 lines (108 loc) · 5.92 KB
/
keyframe_buffer.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
from collections import deque
import numpy as np
from dvmvs.utils import is_pose_available, pose_distance
class KeyframeBuffer:
def __init__(self, buffer_size, keyframe_pose_distance, optimal_t_score, optimal_R_score, store_return_indices):
self.buffer = deque([], maxlen=buffer_size)
self.keyframe_pose_distance = keyframe_pose_distance
self.optimal_t_score = optimal_t_score
self.optimal_R_score = optimal_R_score
self.__tracking_lost_counter = 0
self.__store_return_indices = store_return_indices # mostly required for simulation of the frame selection
def calculate_penalty(self, t_score, R_score):
degree = 2.0
R_penalty = np.abs(R_score - self.optimal_R_score) ** degree
t_diff = t_score - self.optimal_t_score
if t_diff < 0.0:
t_penalty = 5.0 * (np.abs(t_diff) ** degree)
else:
t_penalty = np.abs(t_diff) ** degree
return R_penalty + t_penalty
def try_new_keyframe(self, pose, image, index=None):
if self.__store_return_indices and index is None:
raise ValueError("Storing and returning the frame indices is requested in the constructor, but index=None is passed to the function")
if is_pose_available(pose):
self.__tracking_lost_counter = 0
if len(self.buffer) == 0:
if self.__store_return_indices:
self.buffer.append((pose, image, index))
else:
self.buffer.append((pose, image))
return 0 # pose is available, new frame added but buffer was empty, this is the first frame, no depth map prediction will be done
else:
if self.__store_return_indices:
last_pose, last_image, last_index = self.buffer[-1]
else:
last_pose, last_image = self.buffer[-1]
combined_measure, R_measure, t_measure = pose_distance(pose, last_pose)
if combined_measure >= self.keyframe_pose_distance:
if self.__store_return_indices:
self.buffer.append((pose, image, index))
else:
self.buffer.append((pose, image))
return 1 # pose is available, new frame added, everything is perfect, and we will predict a depth map later
else:
return 2 # pose is available but not enough change has happened since the last keyframe
else:
self.__tracking_lost_counter += 1
if self.__tracking_lost_counter > 30:
if len(self.buffer) > 0:
self.buffer.clear()
return 3 # a pose reading has not arrived for over a second, tracking is now lost
else:
return 4 # we are still very lost
else:
return 5 # pose is not available right now, but not enough time has passed to consider lost, there is still hope :)
def get_best_measurement_frames(self, n_requested_measurement_frames):
buffer_array = list(self.buffer)
if self.__store_return_indices:
reference_pose, reference_image, reference_index = buffer_array[-1]
else:
reference_pose, reference_image = buffer_array[-1]
n_requested_measurement_frames = min(n_requested_measurement_frames, len(buffer_array) - 1)
penalties = []
for i in range(len(buffer_array) - 1):
measurement_pose = buffer_array[i][0]
combined_measure, R_measure, t_measure = pose_distance(reference_pose, measurement_pose)
penalty = self.calculate_penalty(t_measure, R_measure)
penalties.append(penalty)
indices = np.argpartition(penalties, n_requested_measurement_frames - 1)[:n_requested_measurement_frames]
measurement_frames = []
for index in indices:
measurement_frames.append(buffer_array[index])
return measurement_frames
class SimpleBuffer:
def __init__(self, buffer_size, store_return_indices):
self.buffer = deque([], maxlen=buffer_size + 1)
self.__tracking_lost_counter = 0
self.__store_return_indices = store_return_indices # mostly required for simulation of the frame selection
def try_new_keyframe(self, pose, image, index=None):
if self.__store_return_indices and index is None:
raise ValueError("Storing and returning the frame indices is requested in the constructor, but index=None is passed to the function")
if is_pose_available(pose):
self.__tracking_lost_counter = 0
if len(self.buffer) == 0:
if self.__store_return_indices:
self.buffer.append((pose, image, index))
else:
self.buffer.append((pose, image))
return 0 # pose is available, new frame added but buffer was empty, this is the first frame, no depth map prediction will be done
else:
if self.__store_return_indices:
self.buffer.append((pose, image, index))
else:
self.buffer.append((pose, image))
return 1 # pose is available, new frame added, everything is perfect, and we will predict a depth map later
else:
self.__tracking_lost_counter += 1
if self.__tracking_lost_counter > 30:
if len(self.buffer) > 0:
self.buffer.clear()
return 2 # a pose reading has not arrived for over a second, tracking is now lost
else:
return 3 # we are still very lost
else:
return 4 # pose is not available right now, but not enough time has passed to consider lost, there is still hope :)
def get_measurement_frames(self):
measurement_frames = list(self.buffer)[:-1]
return measurement_frames