forked from ElvinC/gyroflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_integrate_gyro.py
177 lines (123 loc) · 5.37 KB
/
example_integrate_gyro.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import json
from gyro_integrator import *
import GPMF_gyro
from blackbox_extract import BlackboxExtractor
extrac = GPMF_gyro.Extractor("test_clips/GX016017.MP4")
realGyroData = extrac.get_gyro(True)
#bb = BlackboxExtractor("test_clips/GX015563.MP4_emuf_004.bbl")
#realGyroData = bb.get_gyro_data(cam_angle_degrees=2)
print(realGyroData.shape)
from matplotlib import pyplot as plt
FPS = 29.97 # framerate of video to stabilize
SAMPLE_RATE = 400 # sample per second
dat = list(realGyroData[:,2])
times = list(FPS * realGyroData[:,0])
from scipy import fftpack
# sampling rate
f_s = 400
X = fftpack.fft(dat)
freqs = fftpack.fftfreq(len(dat)) * f_s
#plt.plot(freqs, X)
#plt.plot(times,dat)
#plt.show()
integrator = GyroIntegrator(realGyroData)
plt.plot(integrator.get_raw_data("t") * FPS,integrator.get_raw_data("y"))
plt.show()
time_list, orientation_list = integrator.integrate_all()
time_list, orientation_list = integrator.get_orientations()
output_data = np.column_stack((time_list, orientation_list))
CSV_header = "\t".join(["Time","q0","q1","q2","q3"])
orientation_list = orientation_list[::10,:]
#print(output_data)
# save orientation data as CSV file
#np.savetxt('hero5_orientation.csv',output_data ,delimiter='\t',header=CSV_header,comments='')
#print(realGyroData)
# Visualize motion during testing
# Adapted from https://github.com/jerabaul29/IntegrateGyroData
import sys
import pygame
from operator import itemgetter
import Quaternions_temp as qt
class Point3D:
"""A class used for describing a point in 3D."""
def __init__(self, x=0, y=0, z=0):
self.x, self.y, self.z = float(x), float(y), float(z)
self.v = qt.Vector(self.x, self.y, self.z)
def project(self, win_width, win_height, fov, viewer_distance):
""" Transforms this 3D point to 2D using a perspective projection. """
factor = fov / (viewer_distance + self.z)
x = self.x * factor + win_width / 2
y = -self.y * factor + win_height / 2
return Point3D(x, y, self.z)
def rotateQ(self, q):
"""Apply rotation described by quaternion q to this 3D point"""
v_rotated = qt.apply_rotation_on_vector(q, self.v)
return Point3D(v_rotated.vx, v_rotated.vy, v_rotated.vz)
class RenderGyroIntegration:
"""A class for rendering gyro integration as a 3D cube display."""
def __init__(self, win_width=640, win_height=480):
pygame.init()
self.screen = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Rendering of 3D cube")
self.clock = pygame.time.Clock()
self.vertices = [
Point3D(-1, 1, -1),
Point3D(1, 1, -1),
Point3D(1, -1, -1),
Point3D(-1, -1, -1),
Point3D(-1, 1, 1),
Point3D(1, 1, 1),
Point3D(1, -1, 1),
Point3D(-1, -1, 1)
]
# Define the vertices that compose each of the 6 faces.
self.faces = [(0, 1, 2, 3), (1, 5, 6, 2), (5, 4, 7, 6),
(4, 0, 3, 7), (0, 4, 5, 1), (3, 2, 6, 7)]
# Define colors for each face
self.colors = [(255, 0, 255), (255, 0, 0), (0, 255, 0),
(0, 0, 255), (0, 255, 255), (255, 255, 0)]
self.angle = 0
def run(self):
""" Main Loop: run until window gets closed."""
iteration = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.clock.tick(54)
self.screen.fill((0, 32, 0))
# It will hold transformed vertices.
t = []
# perform one gyro integration: read, update quaternion
itemas = orientation_list[iteration]
q = qt.Quaternion(itemas[0],itemas[1],itemas[2],itemas[3])
iteration += 1
for v in self.vertices:
# rotate point according to integrated gyro
r = v.rotateQ(q)
# Transform the point from 3D to 2D
p = r.project(self.screen.get_width(), self.screen.get_height(), 256, 4)
# Put the point in the list of transformed vertices
t.append(p)
# Calculate the average Z values of each face.
avg_z = []
i = 0
for f in self.faces:
z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0
avg_z.append([i, z])
i = i + 1
# Draw the faces using the Painter's algorithm:
# Distant faces are drawn before the closer ones.
for tmp in sorted(avg_z, key=itemgetter(1), reverse=True):
face_index = tmp[0]
f = self.faces[face_index]
pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y),
(t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y),
(t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y),
(t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)]
pygame.draw.polygon(self.screen, self.colors[face_index], pointlist)
self.angle += 1
pygame.display.flip()
test = RenderGyroIntegration()
test.run()