-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleap_interface.py
323 lines (250 loc) · 11.7 KB
/
leap_interface.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#################################################################################
# Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
# Leap Motion proprietary and confidential. Not for distribution. #
# Use subject to the terms of the Leap Motion SDK Agreement available at #
# https://developer.leapmotion.com/sdk_agreement, or another agreement #
# between Leap Motion and you, your company or other organization. #
#################################################################################
#################################################################################
# Altered LEAP example by Florian Lier, you need to have the LEAP SDK installed #
# for this to work properly ;) #
# This interface provides access to the LEAP MOTION hardware, you will need to #
# have the official LEAP MOTION SDK installed in order to load the shared #
# provided with the SDK. #
#################################################################################
import sys
import time
# Set (append) your PYTHONPATH properly, or just fill in the location of your LEAP
# SDK folder, e.g., $HOME/LeapSDK/lib where the Leap.py lives and /LeapSDK/lib/x64 or
# x86 where the *.so files reside.
# Below, you can see the "dirty" version - NOT RECOMMENDED!
sys.path.append("/home/edessale/Desktop/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/lib")
sys.path.append("/home/edessale/Desktop/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/lib/x64")
import threading
import Leap
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture
class LeapInterface(Leap.Listener):
def on_init(self, controller):
# These variables as probably not thread safe
# TODO: Make thread safe ;)
self.hand = [0,0,0]
self.right_hand = False
self.left_hand = False
self.hand_direction = [0,0,0]
self.hand_normal = [0,0,0]
self.hand_palm_pos = [0,0,0]
self.hand_pitch = 0.0
self.hand_yaw = 0.0
self.hand_roll = 0.0
self.hand2 = [0,0,0]
self.hand_direction2 = [0,0,0]
self.hand_normal2 = [0,0,0]
self.hand_palm_pos2 = [0,0,0]
self.hand_pitch2 = 0.0
self.hand_yaw2 = 0.0
self.hand_roll2 = 0.0
print "Initialized Leap Motion Device"
def on_connect(self, controller):
print "Connected to Leap Motion Controller"
# Enable gestures
controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE);
controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP);
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
print "Disconnected Leap Motion"
def on_exit(self, controller):
print "Exited Leap Motion Controller"
def on_frame(self, controller):
# Get the most recent frame and report some basic information
frame = controller.frame()
# print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d, gestures: %d" % (
# frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools), len(frame.gestures()))
if not frame.hands.is_empty: #recently changed in API
# Get the first hand
#we are seeking one left and one right hands
there_is_right_hand=False
there_is_left_hand=False
i = 0
for hand in frame.hands:
if hand.is_right:
there_is_right_hand=True
self.right_hand=hand
if len(frame.hands) == 2:
self.hand = hand
i = 1
elif hand.is_left:
there_is_left_hand=True
self.left_hand=hand
if len(frame.hands) == 2:
self.hand2 = hand
i = 0
if not there_is_right_hand:
self.right_hand=False
if not there_is_left_hand:
self.left_hand=False
if len(frame.hands) == 1:
self.hand = frame.hands[0]
# Check if the hand has any fingers
#fingers = self.hand.fingers
#if not fingers.empty:
# Calculate the hand's average finger tip position
#avg_pos = Leap.Vector()
#for finger in fingers:
#avg_pos += finger.tip_position
# avg_pos /= len(fingers)
# print "Hand has %d fingers, average finger tip position: %s" % (len(fingers), avg_pos)
# Get the hand's sphere radius and palm position
# print "Hand sphere radius: %f mm, palm position: %s" % (self.hand.sphere_radius, hand.palm_position)
# Get the hand's normal vector and direction
normal = self.hand.palm_normal
direction = self.hand.direction
pos = self.hand.palm_position
if len(frame.hands) == 2:
normal2 = self.hand2.palm_normal
direction2 = self.hand2.direction
pos2 = self.hand2.palm_position
self.hand_direction[0] = direction.x
self.hand_direction[1] = direction.y
self.hand_direction[2] = direction.z
self.hand_normal[0] = normal.x
self.hand_normal[1] = normal.y
self.hand_normal[2] = normal.z
self.hand_palm_pos[0] = pos.x
self.hand_palm_pos[1] = pos.y
self.hand_palm_pos[2] = pos.z
self.hand_pitch = direction.pitch
self.hand_yaw = normal.yaw
self.hand_roll = direction.roll
if len(frame.hands) == 2:
self.hand_direction2[0] = direction2.x
self.hand_direction2[1] = direction2.y
self.hand_direction2[2] = direction2.z
self.hand_normal2[0] = normal2.x
self.hand_normal2[1] = normal2.y
self.hand_normal2[2] = normal2.z
self.hand_palm_pos2[0] = pos2.x
self.hand_palm_pos2[1] = pos2.y
self.hand_palm_pos2[2] = pos2.z
self.hand_pitch2 = direction2.pitch
self.hand_yaw2 = normal2.yaw
self.hand_roll2 = direction2.roll
# Calculate the hand's pitch, roll, and yaw angles
#print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (self.hand_pitch, self.hand_roll, self.hand_yaw)
'''
# Gestures
for gesture in frame.gestures():
if gesture.type == Leap.Gesture.TYPE_CIRCLE:
circle = CircleGesture(gesture)
# Determine clock direction using the angle between the pointable and the circle normal
if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
clockwiseness = "clockwise"
else:
clockwiseness = "counterclockwise"
# Calculate the angle swept since the last frame
swept_angle = 0
if circle.state != Leap.Gesture.STATE_START:
previous_update = CircleGesture(controller.frame(1).gesture(circle.id))
swept_angle = (circle.progress - previous_update.progress) * 2 * Leap.PI
print "Circle id: %d, %s, progress: %f, radius: %f, angle: %f degrees, %s" % (
gesture.id, self.state_string(gesture.state),
circle.progress, circle.radius, swept_angle * Leap.RAD_TO_DEG, clockwiseness)
if gesture.type == Leap.Gesture.TYPE_SWIPE:
swipe = SwipeGesture(gesture)
print "Swipe id: %d, state: %s, position: %s, direction: %s, speed: %f" % (
gesture.id, self.state_string(gesture.state),
swipe.position, swipe.direction, swipe.speed)
if gesture.type == Leap.Gesture.TYPE_KEY_TAP:
keytap = KeyTapGesture(gesture)
print "Key Tap id: %d, %s, position: %s, direction: %s" % (
gesture.id, self.state_string(gesture.state),
keytap.position, keytap.direction )
if gesture.type == Leap.Gesture.TYPE_SCREEN_TAP:
screentap = ScreenTapGesture(gesture)
print "Screen Tap id: %d, %s, position: %s, direction: %s" % (
gesture.id, self.state_string(gesture.state),
screentap.position, screentap.direction )
if not (frame.hands.empty and frame.gestures().empty):
print ""
def state_string(self, state):
if state == Leap.Gesture.STATE_START:
return "STATE_START"
if state == Leap.Gesture.STATE_UPDATE:
return "STATE_UPDATE"
if state == Leap.Gesture.STATE_STOP:
return "STATE_STOP"
if state == Leap.Gesture.STATE_INVALID:
return "STATE_INVALID"
'''
def get_hand_direction(self):
return self.hand_direction
def get_hand_normal(self):
return self.hand_normal
def get_hand_palmpos(self):
return self.hand_palm_pos
def get_hand_yaw(self):
return self.hand_yaw
def get_hand_pitch(self):
return self.hand_pitch
def get_hand_roll(self):
return self.hand_roll
def get_hand_direction2(self):
return self.hand_direction2
def get_hand_normal2(self):
return self.hand_normal2
def get_hand_palmpos2(self):
return self.hand_palm_pos2
def get_hand_yaw2(self):
return self.hand_yaw2
def get_hand_pitch2(self):
return self.hand_pitch2
def get_hand_roll2(self):
return self.hand_roll2
def getNumHands(self):
if self.right_hand and self.left_hand:
return 2
if self.right_hand:
return 1
if self.left_hand:
return 0
return -1
class Runner(threading.Thread):
def __init__(self,arg=None):
threading.Thread.__init__(self)
self.arg=arg
self.listener = LeapInterface()
self.controller = Leap.Controller()
self.controller.add_listener(self.listener)
def __del__(self):
self.controller.remove_listener(self.listener)
def get_hand_direction(self):
return self.listener.get_hand_direction()
def get_hand_normal(self):
return self.listener.get_hand_normal()
def get_hand_palmpos(self):
return self.listener.get_hand_palmpos()
def get_hand_roll(self):
return self.listener.get_hand_roll()
def get_hand_pitch(self):
return self.listener.get_hand_pitch()
def get_hand_yaw(self):
return self.listener.get_hand_yaw()
def getNumHands(self):
return self.listener.getNumHands()
def get_hand_direction2(self):
return self.listener.get_hand_direction2()
def get_hand_normal2(self):
return self.listener.get_hand_normal2()
def get_hand_palmpos2(self):
return self.listener.get_hand_palmpos2()
def get_hand_roll2(self):
return self.listener.get_hand_roll2()
def get_hand_pitch2(self):
return self.listener.get_hand_pitch2()
def get_hand_yaw2(self):
return self.listener.get_hand_yaw2()
def run (self):
while True:
# Save some CPU time
time.sleep(0.001)