Created
October 8, 2016 21:17
-
-
Save hedinasr/213a4a9e99ae1c0f1c728ee17847e6b9 to your computer and use it in GitHub Desktop.
Atomistic code for leap motion control with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import sys | |
#sys.path.insert(0, "../lib") # utile pour avoir la lib en locale | |
import Leap | |
class MyListener(Leap.Listener): | |
def __init__(self): | |
super(MyListener, self).__init__() | |
self.controller = Leap.Controller() | |
self.controller.add_listener(self) | |
def find_direction(self, position): | |
if position.x > 90: # right | |
return 'right' | |
elif position.x < -90: # left | |
return 'left' | |
elif position.z < -90: # forward | |
return 'forward' | |
elif position.z > 90: # backward | |
return 'backward' | |
else: | |
return 'undefined' # undefined | |
def on_connect(self, controller): | |
print 'Connected' | |
def on_frame(self, controller): | |
frame = controller.frame() | |
fingers = frame.fingers.extended() | |
hand = frame.hands[0] | |
if hand.is_valid and len(fingers) == 5: | |
direction = self.find_direction(hand.palm_position) | |
print str(direction) | |
def __del__(self): | |
self.controller.remove_listener(self) | |
super(MyListener, self).__del__() | |
def main(): | |
listener = MyListener() | |
# Keep this process running until Enter is pressed | |
print "Press Enter to quit..." | |
try: | |
sys.stdin.readline() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
print 'bye' | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment