Leap Motion 开发——Ubuntu 16.04.02 LTS + Python 2.7: Hello World

来源:互联网 发布:学生数据字典 样例 编辑:程序博客网 时间:2024/04/24 00:55

System: Ubuntu 16.04.02
Leap Motion SDK: V2 Tracking
Codes: Python 2.7

下面简要介绍下在Ubuntu 16.04.02下用Python开发Leap Motion的基本方法。更多信息请参考官网文档。

第1步 下载安装V2 Tracking 工具包,地址在这。

第2步 在下载到的LeapSDK文件夹内,找到对应Python开发的相关模块、库文件:

  1. lib/Leap.py
  2. lib/x64/LeapPython.so
  3. lib/x64/libLeap.so

注意跟进你的系统选择相关的库文件,64位系统按照上述方式选择即可,32位系统注意选择x86文件夹下的相关文件。

第3步 构建工作空间。构建工作空间最简单的方式就是首先新建一个文件夹,例如:

mkdir leap_ws

随后将第2步中的相关文件复制到该文件夹下,并添加你的.py文件。

第4步 编写Hello World。官网提供了相关教程。
简述如下,新建一个名为Sample.py的文件,输入如下代码:

import sys, thread, timeimport Leapfrom Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesturedef main():    print "Press Enter to quit..."    try:        sys.stdin.readline()    except KeyboardInterrupt:        passif __name__ == "__main__":    main()

代码内容此处不作具体解释。上述代码无实际意义,仅仅用来检查环境配置是否正确。

运行即可:

python Sample.py

第5步 运行leapd服务。使用Leap Motion的数据需要运行leapd服务,关于leapd服务的具体安装方法请参考安装配置Leap Motion SDK。具体操作方法如下:

用USB线连接Leap Motion, 启动leapd:

sudo service leapd start

查看服务状态:

sudo service leapd status

如果成功连接Leap Motion服务。则可以看到当前运行状态为active,并且有一个绿色的圆圈。下一步运行控制台:

LeapControlPanel 

该控制台可以提供一些参数配置,若成功连接Leap Motion服务,则可以看到左上角的绿色灯亮起:
控制台指示灯
当然,如果你用完后想关闭Leap Motion 服务,运行:

sudo service leapd stop

如果连接工程中有异常,运行:

sudo service leapd restart

第6步 使用Leap Motion服务。若上述步骤无误,则修改上述代码:

import sys, thread, timeimport Leapfrom Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesturedef main():    controller = Leap.Controller()    print "Press Enter to quit..."    try:        sys.stdin.readline()    except KeyboardInterrupt:        passif __name__ == "__main__":    main()

其中controller对象用于连接Leap Motion硬件模块。此时连接好Leap Motion,运行上述代码。

无误后,继续添加代码:

import sys, thread, timeimport Leapfrom Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGestureclass SampleListener(Leap.Listener):    def on_connect(self, controller):        print "Connected"        controller.enable_gesture(Leap.Gesture.TYPE_SWIPE)    def on_frame(self, controller):        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()))def main():    listener = SampleListener()    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()

运行上述代码,别忘了连接Leap Motion模块,用手在其上方活动,此时可以观察到数据变化。

原创粉丝点击