-1

I use Leap motion controller for hand gesture data acquisition. I use below python code but faced some problems. First, Leap library only works for python 2 therefor, I made an environment of version 2 in my anaconda to use my code. secondly, it just save a frame.data file which I think is empty. because it does not takes any time. also I don't think that my program even connects to LMC device.

import sys
import os
import Leap
import ctypes

controller = Leap.Controller()
frame = controller.frame()
hands = frame.hands
pointables = frame.pointables
fingers = frame.fingers
tools = frame.tools
serialized_tuple = frame.serialize
serialized_data = serialized_tuple[0]
serialized_length = serialized_tuple[1]
data_address = serialized_data.cast().__long__()
buffer = (ctypes.c_ubyte * serialized_length).from_address(data_address)
with open(os.path.realpath('frame.data'), 'wb') as data_file:
       data_file.write(buffer)

4

1 回答 1

0

我建议通读我们的开发人员站点上提供的 Python 文档,查看 Python 绑定中可用的示例,这将帮助您开始使用 Python 访问手部跟踪数据。

要解决您的问题,您需要等到控制器告诉您框架已准备好。这可以通过扩展 Leap.Listener 类并使用controller.add_listener(my_listener).

class SampleListener(Leap.Listener):
    def on_init(self, controller):
        pass
    def on_connect(self, controller):
        pass
    def on_disconnect(self, controller):
        # Note: not dispatched when running in a debugger.
        pass
    def on_exit(self, controller):
        pass
    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
        frame = controller.frame()
        # do things with the frame
...
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)

如果您不想使用侦听器和事件,那么我建议您查看文档的这一部分,显示了与上面相同的代码片段,但表明程序应该“等到 Controller.isConnected() 评估为 true ”。

于 2021-06-24T10:58:48.470 回答