-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeapDirection.py
94 lines (74 loc) · 2.32 KB
/
LeapDirection.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
import Leap, sys, math, time
from Leap import SwipeGesture
# CircleGesture, KeyTapGesture, ScreenTapGesture
# create a list to store the msgs
s=[]
import pyttsx
engine = pyttsx.init()
engine.say('Greetings!')
engine.say('How are you today?')
engine.runAndWait()
class LeapMotionListener(Leap.Listener):
def on_connect(self,controller):
print "Motion Sensor Connected"
engine.say('Motion Sensor is Connected')
engine.runAndWait()
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE);
def main():
listener = LeapMotionListener()
controller = Leap.Controller()
controller.add_listener(listener)
while True:
global s, engine
frame = controller.frame()
for gesture in frame.gestures():
# check if gesture is swipe
if gesture.type == Leap.Gesture.TYPE_SWIPE:
swipe = SwipeGesture(gesture)
swipeDir = swipe.direction
# if x>0 and x>y (right)
if(swipeDir.x > 0 and math.fabs(swipeDir.x) > math.fabs(swipeDir.y)):
# if length is 0 or last element is not left, add right
if len(s)==0 or s[len(s)-1]!="right":
# add "right" to end of the list
s.append("right")
# else x<0 and x>y (left)
elif(swipeDir.x < 0 and math.fabs(swipeDir.x) > math.fabs(swipeDir.y)):
if len(s)==0 or s[len(s)-1]!="left":
s.append("left")
# else y>0 and x<y (up)
elif(swipeDir.y > 0 and math.fabs(swipeDir.x) < math.fabs(swipeDir.y)):
if len(s) == 0 or s[len(s) - 1] != "up":
s.append("up")
# else y<0 and x<y (down)
elif(swipeDir.y < 0 and math.fabs(swipeDir.x) < math.fabs(swipeDir.y)):
if len(s) == 0 or s[len(s) - 1] != "down":
s.append("down")
# make list into str(join)
print s, ": ", "".join(s)
# if s is right and up
if("".join(s)=="rightup"):
engine.say('Hello!')
engine.runAndWait()
s=[]
# pause for a sec
time.sleep(1)
if ("".join(s)=="leftup"):
engine.say("Bye")
engine.runAndWait()
s=[]
time.sleep(1)
# if length of s is more than 5, clear
if(len(s)>=2):
s=[]
time.sleep(1)
print "clear"
print "Press enter to quit"
try:
sys.stdin.readline()
except KeyboardInterrupt:
pass
finally:
controller.remove_listener(listener)
if __name__ == "__main__":
main()