1

I am trying to write a basic program using leap motion, I just want to constantly run the on_frame method but the method only runs once and disconnecting the device does not call the on_disconnect method. The program will not run until I hit enter, What am I doing wrong? Thanks for the help :

import Leap, sys, thread
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture,SwipeGesture

class LeapMotionListener(Leap.Listener):
    state_names = ["STATE_INVAILD","STATE_START","STATE_UPDATE", "STATE_END"]

    def on_init(self, controller):
        print "Initialized"
    def on_connect(self, controller):
        print "Connected"

        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);
        print "All gestures enabled"
    def on_disconnect(self, controller):
        print "Disconnected"
    def on_exit(self, controller):
        print "Exit"
    def on_frame(self, controller):
        frame= controller.frame()
        print "\n Frame ID"+ str(frame.id)
        print "num of hands:  " +str(len(frame.hands)) 
        print "num of gestures:  " +str(len(frame.gestures()))
        for gesture in frame.gestures():
            if gesture.type is Leap.Gesture.TYPE_CIRCLE:
                circle = Leap.CircleGesture(gesture)
            elif gesture.type is Leap.Gesture.TYPE_SWIPE:
                swipe = Leap.SwipeGesture(gesture)
            elif gesture.type is Leap.Gesture.TYPE_KEY_TAP:
                key_tap = Leap.KeyTapGesture(gesture)
            elif gesture.type is Leap.Gesture.TYPE_SCREEN_TAP:
                screen_tap = Leap.ScreenTapGesture(gesture)

def main():
    listener= LeapMotionListener()
    controller = Leap.Controller()
    controller.add_listener(listener)

    print "Press enter to quit: " 

    try:
       sys.stdin.readline() 
    except KeyboardInterrupt: 
        pass        
    finally:
        controller.remove_listener(listener)

if __name__ == "__main__":
    main()
sideways
  • 11
  • 1

1 Answers1

0

Your code runs fine from the command line. I.e:

> python scriptname.py

Are you running from Idle? if so, this part:

try:
   sys.stdin.readline() 
except KeyboardInterrupt: 
    pass   

doesn't work. (See Python 3.2 Idle vs terminal) A similar issue might exist in other IDEs.

The following should work in Idle, but you have to use Ctrl-C to exit.

# Keep this process running until a Ctrl-C is pressed
print "Press Ctrl-C to quit..."

try:
    while True:
        time.sleep(1)

except:
    print "Quiting"
finally:
    # Remove the sample listener when done
    controller.remove_listener(listener)

Quitting on any key stroke in both Idle and the command line seemed surprisingly difficult and complex when I tried it some time ago -- but then, I'm a Python duffer.

Community
  • 1
  • 1
Charles Ward
  • 1,388
  • 1
  • 8
  • 13