-
Notifications
You must be signed in to change notification settings - Fork 0
/
leap_api.py
212 lines (171 loc) · 7.19 KB
/
leap_api.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import leap
import numpy as np
import cv2
_TRACKING_MODES = {
leap.TrackingMode.Desktop: "Desktop",
leap.TrackingMode.HMD: "HMD",
leap.TrackingMode.ScreenTop: "ScreenTop",
}
class Canvas:
def __init__(self):
self.name = "Python Gemini Visualiser"
self.screen_size = [500, 700]
self.hands_colour = (255, 255, 255)
self.font_colour = (0, 255, 44)
self.hands_format = "Skeleton"
self.output_image = np.zeros(
(self.screen_size[0], self.screen_size[1], 3), np.uint8
)
self.tracking_mode = None
def set_tracking_mode(self, tracking_mode):
self.tracking_mode = tracking_mode
def toggle_hands_format(self):
self.hands_format = "Dots" if self.hands_format == "Skeleton" else "Skeleton"
print(f"Set hands format to {self.hands_format}")
def get_joint_position(self, bone):
if bone:
return int(bone.x + (self.screen_size[1] / 2)), int(
bone.z + (self.screen_size[0] / 2)
)
else:
return None
def render_hands(self, event):
# Clear the previous image
self.output_image[:, :] = 0
cv2.putText(
self.output_image,
f"Tracking Mode: {_TRACKING_MODES[self.tracking_mode]}",
(10, self.screen_size[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
self.font_colour,
1,
)
if len(event.hands) == 0:
return
for i in range(0, len(event.hands)):
hand = event.hands[i]
for index_digit in range(0, 5):
digit = hand.digits[index_digit]
for index_bone in range(0, 4):
bone = digit.bones[index_bone]
if self.hands_format == "Dots":
prev_joint = self.get_joint_position(bone.prev_joint)
next_joint = self.get_joint_position(bone.next_joint)
if prev_joint:
cv2.circle(
self.output_image, prev_joint, 2, self.hands_colour, -1
)
if next_joint:
cv2.circle(
self.output_image, next_joint, 2, self.hands_colour, -1
)
if self.hands_format == "Skeleton":
wrist = self.get_joint_position(hand.arm.next_joint)
elbow = self.get_joint_position(hand.arm.prev_joint)
if wrist:
cv2.circle(
self.output_image, wrist, 3, self.hands_colour, -1
)
if elbow:
cv2.circle(
self.output_image, elbow, 3, self.hands_colour, -1
)
if wrist and elbow:
cv2.line(
self.output_image, wrist, elbow, self.hands_colour, 2
)
bone_start = self.get_joint_position(bone.prev_joint)
bone_end = self.get_joint_position(bone.next_joint)
if bone_start:
cv2.circle(
self.output_image, bone_start, 3, self.hands_colour, -1
)
if bone_end:
cv2.circle(
self.output_image, bone_end, 3, self.hands_colour, -1
)
if bone_start and bone_end:
cv2.line(
self.output_image,
bone_start,
bone_end,
self.hands_colour,
2,
)
if ((index_digit == 0) and (index_bone == 0)) or (
(index_digit > 0) and (index_digit < 4) and (index_bone < 2)
):
index_digit_next = index_digit + 1
digit_next = hand.digits[index_digit_next]
bone_next = digit_next.bones[index_bone]
bone_next_start = self.get_joint_position(
bone_next.prev_joint
)
if bone_start and bone_next_start:
cv2.line(
self.output_image,
bone_start,
bone_next_start,
self.hands_colour,
2,
)
if index_bone == 0 and bone_start and wrist:
cv2.line(
self.output_image,
bone_start,
wrist,
self.hands_colour,
2,
)
class TrackingListener(leap.Listener):
def __init__(self, canvas):
self.canvas = canvas
def on_connection_event(self, event):
pass
def on_tracking_mode_event(self, event):
self.canvas.set_tracking_mode(event.current_tracking_mode)
print(
f"Tracking mode changed to {_TRACKING_MODES[event.current_tracking_mode]}"
)
def on_device_event(self, event):
try:
with event.device.open():
info = event.device.get_info()
except leap.LeapCannotOpenDeviceError:
info = event.device.get_info()
print(f"Found device {info.serial}")
def on_tracking_event(self, event):
self.canvas.render_hands(event)
def main():
canvas = Canvas()
print(canvas.name)
print("")
print("Press <key> in visualiser window to:")
print(" x: Exit")
print(" h: Select HMD tracking mode")
print(" s: Select ScreenTop tracking mode")
print(" d: Select Desktop tracking mode")
print(" f: Toggle hands format between Skeleton/Dots")
tracking_listener = TrackingListener(canvas)
connection = leap.Connection()
connection.add_listener(tracking_listener)
running = True
with connection.open():
connection.set_tracking_mode(leap.TrackingMode.Desktop)
canvas.set_tracking_mode(leap.TrackingMode.Desktop)
while running:
cv2.imshow(canvas.name, canvas.output_image)
key = cv2.waitKey(1)
if key == ord("x"):
break
elif key == ord("h"):
connection.set_tracking_mode(leap.TrackingMode.HMD)
elif key == ord("s"):
connection.set_tracking_mode(leap.TrackingMode.ScreenTop)
elif key == ord("d"):
connection.set_tracking_mode(leap.TrackingMode.Desktop)
elif key == ord("f"):
canvas.toggle_hands_format()
if __name__ == "__main__":
main()