This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
blackbox_extract.py
159 lines (111 loc) · 5.42 KB
/
blackbox_extract.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
from orangebox import Parser
from scipy.spatial.transform import Rotation
import math
import numpy as np
class BlackboxExtractor:
def __init__(self, path):
print("Opening {}".format(path))
try:
self.parser = Parser.load(path)
except RuntimeError:
raise RuntimeError("Couldn't parse the raw blackbox file. Try converting to a CSV file using blackbox explorer.")
self.n_of_logs = self.parser.reader.log_count
self.gyro_scale = self.parser.headers["gyro_scale"] #should be already scaled in the fc
self.acc_scale = 1/self.parser.headers["acc_1G"]
self.final_gyro_data = []
self.final_acc_data = []
self.extracted = False
self.camera_angle = None
self.gyro_rate = 0
self.max_data_gab = 10 # seconds
def get_gyro_data(self,cam_angle_degrees=0):
if self.extracted:
return np.array(self.final_gyro_data)
self.camera_angle = cam_angle_degrees
r = Rotation.from_euler('x', self.camera_angle, degrees=True)
for lg in range(1,self.n_of_logs+1):
self.parser.set_log_index(lg)
t = self.parser.field_names.index('time')
gx = self.parser.field_names.index('gyroADC[1]')
gy = self.parser.field_names.index('gyroADC[2]')
gz = self.parser.field_names.index('gyroADC[0]')
data_frames = []
last_t = 0
for frame in self.parser.frames():
#print(len(frame.data))
if len(frame.data) > gy and ((0 < (frame.data[t] - last_t) < 1000000 * self.max_data_gab) or (last_t == 0)):
to_rotate = [-math.radians(frame.data[gx]),
math.radians(frame.data[gy]),
-math.radians(frame.data[gz])]
rotated = r.apply(to_rotate)
f = [frame.data[t]/1000000,
rotated[0],
rotated[1],
rotated[2]]
last_t = frame.data[t]
#f = [frame.data[t]/1000000,
# math.radians(frame.data[gx]),
# math.radians(frame.data[gz]),
# -math.radians(frame.data[gy])]
data_frames.append(f)
if len(data_frames) < 2:
return False
self.final_gyro_data.extend(data_frames)
self.final_gyro_data = np.array(self.final_gyro_data)
# rough gyro rate assumed to be constant
self.gyro_rate = self.final_gyro_data.shape[0]/(self.final_gyro_data[-1,0] - self.final_gyro_data[0,0])
self.extracted = True
return self.final_gyro_data
def get_untransformed_imu_data(self):
if self.extracted:
return np.array(self.final_gyro_data), np.array(self.final_acc_data)
r = Rotation.from_euler('x', self.camera_angle, degrees=True)
for lg in range(1,self.n_of_logs+1):
self.parser.set_log_index(lg)
t = self.parser.field_names.index('time')
gx = self.parser.field_names.index('gyroADC[1]')
gy = self.parser.field_names.index('gyroADC[2]')
gz = self.parser.field_names.index('gyroADC[0]')
data_frames = []
max_index = gy
acc_index = None
if "accSmooth[0]" in self.parser.field_names:
acc_index = self.parser.field_names.index("accSmooth[0]")
max_index = max(acc_index + 2, gy)
acc_frames = []
last_t = 0
for frame in self.parser.frames():
if len(frame.data) > max_index and ((0 < (frame.data[t] - last_t) < 1000000 * self.max_data_gab) or (last_t == 0)):
data_frames.append([frame.data[t], frame.data[gx], frame.data[gy], frame.data[gz]])
if acc_index:
acc_frames.append([frame.data[t], frame.data[acc_index+1], frame.data[acc_index+2], frame.data[acc_index]])
last_t = frame.data[t]
self.final_gyro_data.extend(data_frames)
if acc_index:
self.final_acc_data.extend(acc_frames)
self.final_gyro_data = np.array(self.final_gyro_data, dtype=np.float64)
#if self.final_gyro_data.shape[0] < 2:
# print("No valid motion data")
# return False, False
self.final_gyro_data[:,0] /= 1000000
self.final_gyro_data[:,1:] *= np.pi/180
if acc_index:
self.final_acc_data = np.array(self.final_acc_data, dtype=np.float64)
self.final_acc_data[:,0] /= 1000000
self.final_acc_data[:,1:] *= self.acc_scale
else:
self.final_acc_data = None
# rough gyro rate assumed to be constant
self.gyro_rate = self.final_gyro_data.shape[0]/(self.final_gyro_data[-1,0] - self.final_gyro_data[0,0])
self.extracted = True
return self.final_gyro_data, self.final_acc_data
#testing
if __name__ == "__main__":
with open("test_clips/GX010010.MP4.bbl") as f:
#print(f.readline())
pass
#exit()
bbe = BlackboxExtractor("test_clips/GX010010.MP4.bbl") # btfl_all.bbl
gyro_data = bbe.get_gyro_data()
print(gyro_data)
print(bbe.n_of_logs)