-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
173 lines (127 loc) · 4.15 KB
/
predict.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
from myo_api import Myo, emg_mode
import leap as lp
import matplotlib.pyplot as plt
from matplotlib import animation
import plot as leapplot
from utils import (
get_anchor_points,
get_joint_angles,
get_bone_lengths,
get_points_from_angles,
digit_labels,
)
import time
import multiprocessing as mp
import numpy as np
import pandas as pd
import tensorflow as tf
import json
import math
model = tf.keras.saving.load_model("model.h5")
sample_rate = 50
angle_labels = [
"thumb_tm,f/e",
"thumb_tm,aa",
"thumb_mcp,f/e",
"thumb_mcp,aa",
"index_mcp,f/e",
"index_mcp,aa",
"index_pip",
"middle_mcp,f/e",
"middle_mcp,aa",
"middle_pip",
"ring_mcp,f/e",
"ring_mcp,aa",
"ring_pip",
"pinky_mcp,f/e",
"pinky_mcp,aa",
"pinky_pip",
]
def get_angles_from_prediction(angles):
joint_angles = {}
for i in range(angles.shape[1]):
digit, angle = angle_labels[i].split("_")
if digit not in joint_angles:
joint_angles[digit] = {}
joint_angles[digit][angle] = math.radians(angles[0, i])
return joint_angles
def leap_process_data(data, leap_data, myo_data):
if not len(data.hands) or not len(myo_data):
return
hand = data.hands[0]
myo_samples = np.zeros((1000, 8))
myo_array = np.array(myo_data[:1000])
myo_samples[0 : myo_array.shape[0], 0:8] = myo_array
# https://stackoverflow.com/a/61075207
# https://stackoverflow.com/a/51926373
myo_samples = myo_samples.reshape((1, 1000, 8, 1))
anchor_points = get_anchor_points(hand)
# https://stackoverflow.com/a/72601148
prediction = model.predict(myo_samples, verbose=False)
joint_angles = get_angles_from_prediction(prediction)
bone_lengths = get_bone_lengths(hand)
x, y, z = get_points_from_angles(
anchor_points,
bone_lengths,
joint_angles,
)
leap_data["input_points"] = leapplot.get_bone_points(hand)
leap_data["predicted_points"] = np.array([x, y, z])
def leap_collect(callback, leap_data, myo_data):
class TrackingListener(lp.Listener):
def __init__(self, callback):
self.callback = callback
def on_tracking_event(self, event):
self.callback(event, leap_data, myo_data)
leap_connection = lp.Connection()
leap_connection.set_tracking_mode(lp.TrackingMode.Desktop)
leap_connection.add_listener(TrackingListener(callback))
with leap_connection.open():
leap_connection._poll_loop()
def myo_collect(myo_samples):
myo = Myo(None, mode=emg_mode.RAW)
myo.connect()
myo.add_emg_handler(lambda x, _: myo_samples.append(x))
running = True
while running:
myo.run()
def plot(leap_data):
fig = plt.figure()
ax = fig.add_subplot(
121, projection="3d", xlim=(-300, 300), ylim=(-200, 400), zlim=(-300, 300)
)
ax2 = fig.add_subplot(
122, projection="3d", xlim=(-300, 300), ylim=(-200, 400), zlim=(-300, 300)
)
ax.view_init(elev=45.0, azim=122)
ax2.view_init(elev=45.0, azim=122)
def animate(frame):
leapplot.reset_plot(ax)
leapplot.reset_plot(ax2)
if not ("input_points" in leap_data) or not ("predicted_points" in leap_data):
return
# First plot
x, y, z = leap_data["input_points"]
ax.scatter(x, y, z, s=[10] * len(x), alpha=1)
# Second plot (reconstructed from joint angles)
x, y, z = leap_data["predicted_points"]
ax2.scatter(x, y, z, s=[10] * len(x), alpha=1)
anim = animation.FuncAnimation(fig, animate, blit=False, interval=20)
plt.show()
if __name__ == "__main__":
with mp.Manager() as manager:
rows = manager.list()
leap_data = manager.dict()
myo_data = manager.list()
leap_thread = mp.Process(
target=leap_collect,
args=(leap_process_data, leap_data, myo_data),
)
myo_thread = mp.Process(target=myo_collect, args=(myo_data,))
plot_thread = mp.Process(target=plot, args=(leap_data,))
leap_thread.start()
myo_thread.start()
plot_thread.start()
running = True
while running:
time.sleep(0)