Leap Motion get Frame Data Getting Frame Data

Source: Internet
Author: User

Leap Motion translation series Article http://52coding.com/leap-motion-official-doc-translation

Get Frame Data Getting Frame Data

The Leap Motion API provides Motion tracking data for your program using a series of snapshots called Frame Objects. Each frame of the tracing data contains the measurement position and other information about the detected object in the snapshot. This article details how to capture Frame objects (Frame objects) from the Leap Motion Controller ).

Overview

Obtain an object containing tracing information from the linked controller. Whenever your application is ready to use the frame () method in the Controller class, you can get a frame object:

if( controller.isConnected()) //controller is a Controller object{Frame frame = controller.frame(); //The latest frameFrame previous = controller.frame(1); //The previous frame}

Input the history parameter in the frame () method to indicate the number of previous frames that can be retrieved. [It should be the cache space size] in general, the past 60 frames of content are stored in this historical cache.

Get frames through polling

The method of getting Frame Objects by polling the Controller object is the simplest and usually best strategy when your application runs at the natural frame rate. When your device processes a frame of data, you only need to call the frame () function of the controller.

However, if you use the round-robin method, you may obtain the content of the same frame in the same row (if you apply a frame rate faster than the Leap frame rate, or skip one frame (when the Leap frame rate is faster than the frame rate you applied ). In most cases, lost or repeated frame objects are not important. For example, if you move an object by hand in front of the screen, the motion of the object is still smooth (assuming that the overall frame rate you apply is high enough ). [I don't know what it means, but I think it's okay if its data changes smoothly.]

To check whether you have processed a processed frame object, compare the ID value of the last processed frame that has been saved with the current frame:

int64_t lastFrameID = 0;void processFrame( Leap::Frame frame ){if( frame.id() == lastFrameID ) return;//...lastFrameID = frame.id();}

If your application loses some frames, you can use the historical parameters in the frame () function to access the lost frames (as long as the frame object is still in the History cache ):

int64_t lastProcessedFrameID = 0;void nextFrame( Leap::Controller controller ){int64_t currentID = controller.frame().id();for( int history = 0; history < currentID - lastProcessedFrameID; history++){processFrame( controller.frame(history) );}lastProcessedFrameID = currentID;}void processNextFrame( Leap::Frame frame ){if( frame.isValid() ){//...}}
Get frames using the callback function

Similarly, you can use a Listener object to obtain the frame object of the Leap Motion Controller. When a new frame arrives, the Controller object calls the listener's onFrame () function. In onFrame processing, you can call the frame () function of the Controller to obtain its own Frame object.

 

Using the listener callback function is complicated because the callback is multi-threaded. Each callback comes from an independent thread. You must ensure that data from multiple threads is obtained in thread-safe mode.

The following example defines a minimum listener subclass, which can obtain new data frames:

class FrameListener : Leap::Listener{void onFrame(Controller &controller){Frame frame = controller.frame(); //The latest frameFrame previous = controller.frame(1); //The previous frame//...}};

As you can see, retrieving tracing data from a listener object is almost the same as polling mode.

Note that it is necessary to skip a frame, even when we use the listener callback function. If your onFrame callback function takes too much time to complete execution, the next frame will be added to the history, but the onFrame callback function will skip. If a Leap software cannot process a frame of data in time, the frame will be discarded and will not be added to history. This problem occurs when the computer stops running too many tasks.

Get data from frames

The frame class defines some functions for accessing data in frames. For example, the following code describes how to obtain basic objects tracked by the Leap Motion System.

Leap::Controller controller = Leap::Controller();// wait until Controller.isConnected() evaluates to true//...Leap::Frame frame = controller.frame();Leap::HandList hands = frame.hands();Leap::PointableList pointables = frame.pointables();Leap::FingerList fingers = frame.fingers();Leap::ToolList tools = frame.tools();

 

All objects returned from the frame object are read-only. You can save them securely and use them in the future. They are thread-safe. Internally, these objects use the C ++ Boost library to share pointer classes.

Use IDs to track inter-frame entities

If you get an object ID from different frames, you can get the description of the object in the current frame. Pass the ID to the frame function of the appropriate type:

Hand hand = frame.hand(handID);Pointable pointable = frame.pointable(pointableID);Finger finger = frame.finger(fingerID);Tool tool = frame.tool(toolID);

If an object with the same ID cannot be found-maybe one hand or finger removes the Leap field of view. ---- Replace it with a special Invalid object. Invalid objects are also class instances, but all their members return 0 values, 0 arrays, or other invalid objects. This technology makes it easier to connect methods and call them together. For example, the following code calculates the average position of the fingertip under multiple frames.

int count = 0;Leap::Vector average = Leap::Vector();Leap::Finger fingerToAverage = frame.fingers()[0];for( int i = 0; i < 10; i++ ){Leap::Finger fingerFromFrame = controller.frame(i).finger(fingerToAverage.id());if( fingerFromFrame.isValid() ){average += fingerFromFrame.tipPosition();count++;}}average /= count;

If no invalid object exists, this code needs to detect this frame object every time the finger object is returned.

 

For more information, visit the http://52coding.com/category/leap-motion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.