Sei sulla pagina 1di 21

Leap Motion Developer Portal

12/28/12 12:36 AM

DEVELOPER
News Resources

Community

Contact

IIICTECH

Search

Home Resources Understanding the Python Sample Application

Resources
Understanding the Python
Sample Application

DOWNLOAD
SDK

Notice: This developer release of Leap Motion,

DOWNLOADS

Inc. software is confidential and intended for

Latest SDK

very limited distribution. Parties using this


software must accept the SDK Agreement

DOCUMENTATION

prior to obtaining this software and related

Overview

documentation. This software and

API Reference

documentation are subject to copyright.

UX Guidelines
Release Notes

This article discusses the Python sample

for SDK

application included with the Leap SDK.

Leap Visualizer

After reading this article, you should be

Leap

ready to access Leap hand tracking data


from your own Python applications.

Application
Setting up a
Java project
Setting up a

https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 1 of 21

Leap Motion Developer Portal

Topics:
Overview
Creating a Controller object
Subclassing the Listener class
Getting a Frame of data
Running the sample

12/28/12 12:36 AM

Unity project
LANGUAGE
TUTORIALS
C++
C#
Java

In the Leap SDK folder, you can find the

Python

following files used for this article:

JavaScript

Leap_SDK/sample/Sample.py
Python sample application
Leap_SDK/lib/Leap.py Leap
Python API class definitions
Leap_SDK/lib/x86/_LeapPython.pyd
32-bit Leap Python library for
Windows
Leap_SDK/lib/x64/_LeapPython.pyd
64-bit Leap Python library for
Windows
Leap_SDK/lib/_LeapPython.so
Leap Python library for Mac
Leap_SDK/lib/x86/Leap.dll 32bit Leap library for Windows
Leap_SDK/lib/x64/Leap.dll 64https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 2 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

bit Leap library for Windows


Leap_SDK/lib/libLeap.dylib
Leap library for Mac
Also note that you can find the Leap API
reference documentation at
Leap_SDK/docs/API_Reference/annotated.html.

Overview
In a nutshell, the Leap motion tracking
device detects and tracks hands and
fingers placed within its field of view. The
Leap captures this data one frame at a
time. Your applications can use the Leap
API to access this data.
The sample application demonstrates how
to use the Leap API to listen for frame
events dispatched by the Leap and how to
access the hand and finger data in each
frame. The application is a small
command-line program that prints
information about detected hands and
fingers to standard output. The application
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 3 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

is contained in a single file, Sample.py.


The sample application uses most of the
key classes in the Leap API, including:
Leap.Controller the interface
between the Leap and your application
Leap.Listener used to handle
events dispatched by the Leap
Leap.Frame contains a set of hand
and finger tracking data
Leap.Hand contains tracking data for
a detected hand
Leap.Finger contains tracking data
for a detected finger
Leap.Vector represents a 3D
position or directional vector
For more detailed information about these
classes, pleases refer to the Leap API
reference documentation.

Creating a Controller object


The Controller class provides the main
interface between the Leap and your
application. When you create a Controller
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 4 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

object, it connects to the Leap software


running on the computer and makes hand
tracking data available through Frame
objects. You can access these Frame
objects by instantiating a Controller object
and calling the Controller.frame
function.
To receive notification when a new frame
is available, you can set up an event
listener by passing an instance of a
subclass of the Listener class to the
Controller constructor. When a listener is
provided, the controller calls the relevant
callback function implemented in your
Listener subclass when the Leap
dispatches an event. The most interesting
is the on_frame event, which is
dispatched when a new frame of hand
tracking data is available.
The sample application creates a
Controller object in its main function and
adds an instance of a Listener subclass to
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 5 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

the Controller using the


Controller.add_listener() method:
def main():
# Create a sample listener and
controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener rec
eive events from the controller
controller.add_listener(listen
er)
# Keep this process running un
til Enter is pressed
print "Press Enter to quit..."
sys.stdin.readline()
# Remove the sample listener w
hen done
controller.remove_listener(lis
tener)

The application then runs until you press


the Enter key. In the meantime, the
controller calls the appropriate listener
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 6 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

callback functions when events occur.


Before running the program, though, you
must create your own subclass of the
Leap.Listener class.

Subclassing the Listener


class
The sample application defines a
Listener subclass, SampleListener, which
implements callback functions to handle
events dispatched by the Leap. The events
include:
on_init dispatched once, when the
controller to which the listener is
registered is initialized.
on_connect dispatched when the
controller connects to the Leap and is
ready to begin sending frames of
motion tracking data.
on_disconnect dispatched if the
controller disconnects from the Leap
(for example, if you unplug the Leap
device or shut down the Leap software).
on_frame dispatched when a new
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 7 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

frame of motion tracking data is


available.
In the three lifecycle event callbacks,
on_init, on_connect, and
on_disconnect, the sample application
simply prints a message to standard
output. For the onFrame event, the
listener callback does a bit more work.
When the controller calls the on_frame
callback, the function gets the latest frame
of motion tracking data and prints
information about the detected objects to
standard output.

Getting a Frame of data


The Controller calls the on_frame callback
function when the Leap generates a new
frame of motion tracking data. You can
access the new data by calling the
Controller.frame function, which
returns the newest Frame object. (A
reference to the Controller object is
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 8 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

passed to the callback as a parameter.) A


Frame object contains an ID, a timestamp,
and a list containing a Hand object for
each physical hand in view.
The following code from the sample
applications on_frame implementation
gets the most recent Frame object from
the controller, retrieves the list of Hands
from the Frame and then prints out the
Frame ID, timestamp, and the number of
Hands in the list:
# Get the most recent frame and re
port some basic information
frame = controller.frame()
hands = frame.hands()
numHands = len(hands)
print "Frame id: %d, timestamp: %d
, hands: %d" % (
frame.id(), frame.timestamp(),
numHands)

The function goes on to examine the first


Hand in the list:
if numHands >= 1:
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 9 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

# Get the first hand


hand = hands[0]

A Hand object contains an ID, properties


representing the hand's physical
characteristics, and a list of Finger objects.
Each Finger object contains an ID and
properties representing the characteristic
of the finger.
Once it has retrieved a hand, the function
checks it for fingers and then averages the
finger tip positions, printing the result and
the number of fingers:
# Check if the hand has any finger
s
fingers = hand.fingers()
numFingers = len(fingers)
if numFingers >= 1:
# Calculate the hand's average
finger tip position
pos = Leap.Vector()
for finger in fingers:
pos += finger.tip_position
()

https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 10 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

pos /= numFingers
print "Hand has", numFingers,
"fingers with average tip position
", pos

To print the hand's palm position, the


on_frame uses the hand.palm_position()
function:
# Get the palm position
palm = hand.palm_position()
print "Palm position:", palm

Next, the on_frame function calculates the


hand's pitch, roll, and yaw angles using the
hand normal vector and the direction
vector:
# Get the palm normal vector and d
irection
normal = hand.palm_normal()
direction = hand.direction()
# Calculate the hand's pitch, roll
, and yaw angles
print "Pitch: %f degrees, Roll: %
f degrees, Yaw: %f degrees" % (
direction.pitch() * Leap.RAD_T
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 11 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

O_DEG,
normal.roll() * Leap.RAD_TO_DE
G,
direction.yaw() * Leap.RAD_TO_
DEG)

Finally, the function prints out the radius


of a sphere fit to the hand's curvature:
print "Hand curvature radius: %f m
m" % hand.sphere_radius()

Running the sample


To run the sample application:
1. Plug the Leap device into a USB port
and place it in front of you.
2. If you havent already, install the Leap
software.
3. Start the Leap software. If you havent
already, enter your registered email
address and password when prompted.
The Leap icon appears in the
notification area of the task bar (on
Windows) or finder bar (on Mac) and
turns green when ready.
4. Run the sample application:

https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 12 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

On Windows, make sure that


Sample.py, Leap.py,
_LeapPython.pyd, and
Leap.dll are in the current
directory. If you are using a 32bit version of Python, use the
.pyd and .dll files from the
lib\x86 folder of the SDK. Use
the .pyd and .dll files from
lib\x64 with 64-bit Python.
Run the following command in
a command-line prompt:
python Sample.py
On Mac, make sure that
Sample.py, Leap.py,
_LeapPython.so, and
libLeap.dylib are in the
current directory and run the
following command in a
terminal window:
python Sample.py

https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 13 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

You should see the messages "Initialized"


and "Connected" printed to standard
output when the application initializes and
connects to the Leap. You should then see
frame information printed each time the
Leap dispatches the onFrame event. When
you place a hand above the Leap, you
should also see finger and palm position
information printed.
Now that you have seen how to access
motion tracking data from the Leap, you
can begin developing your own Python
applications that integrate the Leap.
Copyright 2012 Leap Motion, Inc. All
rights reserved.

7 comments
Python developer on Mac here. Has
anybody tested this on a machine besides
the one that compiled the SWiG bindings?
Because it looks pretty broken from where
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 14 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

I stand.
zoidberg:sandbox matt$ ls -l
total 4000
-rw-r--r--@ 1 matt staff
21386
Nov 29 20:43 Leap.py
-rw-r--r-- 1 matt staff
46560
Dec 19 14:49 Leap.pyc
-rw-r--r--@ 1 matt staff
3309
Dec 19 14:49 Sample.py
-rwxr-xr-x@ 1 matt staff
648888
Nov 29 20:43 _LeapPython.so
-rwxr-xr-x@ 1 matt staff 1318056
Nov 29 20:43 libLeap.dylib
zoidberg:sandbox matt$ python Samp
le.py
Fatal Python error: Interpreter no
t initialized (version mismatch?)
Abort trap: 6
zoidberg:sandbox matt$ which pytho
n
/usr/local/bin/python
zoidberg:sandbox matt$ python --ve
rsion
Python 2.7.3

Strings on the .so indicate that it is


interested in at least the following libraries
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 15 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

and locations:
/ Library/
/Frameworks/
/Python.framewo
rk/
/Versions/
/2.7/
/Python
/ usr/
/lib/
/libstdc++
++.6.dylib
/ usr/
/lib/
/libSystem.B.dylib
/ usr/
/lib/
/libgcc_s.1.dylib

First guess is that it was hardcoded to use


a Python installed in the system location (I
use homebrew), so let's fix that:
#!/bin/bash
# The Leap Motion SDK libraries ap
pear to demand that the Python bin
ary exist at a very specific locat
ion.
EXPECTED_PYTHON_LOCATION=
="/Library
/Frameworks/Python.framework/Versi
ons/2.7/Python"
# Let's make it happy.
$EXPECTED_PYTHON_LOCATION --versio
n >/dev/null 2>&1
if [ "$?" -ne "0" ] ; then
mkdir -p `dirname $EXPECTED_PYTH
ON_LOCATION`
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 16 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

if [ "$?" -ne "0" ] ; then


sudo !!
fi
ln -s `which python2.7` $EXPECTE
D_PYTHON_LOCATION
if [ "$?" -ne "0" ] ; then
sudo !!
fi
fi

But no dice:
zoidberg:sandbox matt$ /Library/Fr
ameworks/Python.framework/Versions
/2.7/Python Sample.py
Fatal Python error: Interpreter no
t initialized (version mismatch?)
Abort trap: 6

Double check the system libraries:


zoidberg:sandbox matt$ ls -l /usr/
lib/libstdc++.6.dylib /usr/lib/lib
System.B.dylib /usr/lib/libgcc_s.1
.dylib
-r-xr-xr-x 1 root wheel 486336
May 22 2012 /usr/lib/libSystem.B.
dylib
lrwxr-xr-x 1 root wheel
17
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 17 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

May 22 2012 /usr/lib/libgcc_s.1.d


ylib -> libSystem.B.dylib
lrwxr-xr-x 1 root wheel
21
May 22 2012 /usr/lib/libstdc++.6.
dylib -> libstdc++.6.0.9.dylib

Everything seems to be in order there too.


Anybody successfully using the Python
SDK?
By Matt Bornski on 2012-12-19 23:29:54
UTC
I got the sotware to run just fine so far
without errors (Mac OS X 10.7.5), but I
don't have a LEAP to plug in yet. So this is
as far as I have gotten:
$ python Sample.py
Initialized
Press Enter to quit...

By LonnyEachus on 2012-12-20 01:07:19


UTC
I should have mentioned that I am using
the OS X native python install. I use
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 18 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

Homebrew, but I am not using it for


python.
By LonnyEachus on 2012-12-20 03:17:06
UTC
Thanks for the datapoint, Lonny. I'll see if I
can restore a native OS X python install
somehow.
By Matt Bornski on 2012-12-20 20:02:16
UTC
Thanks for the help, Lonny. A vanilla install
from the DMG available at
http://www.python.org/getit/ doesn't crash
Sample.py.
Leap guys, any reason the Python SDK
needs to be so brittle?
By Matt Bornski on 2012-12-20 21:21:00
UTC
Do you have to use version 2.7 of python?
I got 3.3 and am getting
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 19 of 21

Leap Motion Developer Portal

12/28/12 12:36 AM

ImportError: DLL load failed: The


specified module could not be foun
d.

I'm in windows using eclipse/pydev.


By 3DJ on 2012-12-23 10:30:50 UTC
I used dependency walker to find out what
I was missing: PYTHON27.DLL. Looks like
you do in fact need to use python 2.7.
Installed 2.7, switched my interpreter and
now it works!
By 3DJ on 2012-12-23 10:52:53 UTC

Add your comment here

PUBLISH COMMENT
https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 20 of 21

Leap Motion Developer Portal

Terms & Conditions

12/28/12 12:36 AM

Privacy Policy
SDK Agreement
Copyright
2012, Leap Motion, Inc

https://developer.leapmotion.com/documentation/guide/Sample_Python_Tutorial

Page 21 of 21

Potrebbero piacerti anche