使用Leap motion和python的问题

时间:2015-09-05 15:14:31

标签: leap-motion

我正在尝试使用跳跃动作编写基本程序,我只想继续运行on_frame方法,但该方法只运行一次,断开设备不会调用on_disconnect方法。程序在我输入之前不会运行,我做错了什么?谢谢你的帮助:

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()

1 个答案:

答案 0 :(得分:0)

您的代码在命令行中运行良好。即:

> python scriptname.py

你是从空闲中跑出来的吗?如果是这样,这部分:

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

不起作用。 (参见Python 3.2 Idle vs terminal)其他IDE中可能存在类似的问题。

以下内容应在空闲时使用,但您必须使用Ctrl-C退出。

# 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)

在Idle和命令行中退出任何键击时,我在前一段时间尝试时似乎非常困难和复杂 - 但是,我是一个Python duffer。