Last active
January 25, 2017 19:33
-
-
Save ohpauleez/4630671 to your computer and use it in GitHub Desktop.
The Leap Motion example code using the clojure-leap library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns sample | |
(:require [clojure-leap.core :as leap] | |
[clojure-leap.hand :as l-hand] | |
[clojure-leap.pointable :as l-pointable :refer [tip-position]])) | |
(defn process-frame [frame] | |
(let [_ (println "Frame id:" (.id frame) "timestamp:" (.timestamp frame) | |
"hands:" (leap/hands frame) "fingers:" (leap/fingers frame) "tools:" (leap/tools frame))] | |
(when-let [hand (and (leap/hands? frame) (leap/hand frame 0))] | |
(let [fingers (leap/fingers hand) | |
avg-pos (/ (apply + (map tip-position fingers)) | |
(count fingers)) | |
palm (l-hand/palm hand) | |
sphere (l-hand/sphere hand)] | |
(println "The hand has" (count fingers) "fingers, with an average tip position of:" avg-pos) | |
(println "Sphere as a radius of" (:radius sphere) "millimeters, with a position of:" (:position palm)) | |
(println "The hand is facing" (:direction palm) "and moving with a velocity of:" (:velocity palm) "\n"))))) | |
(defn -main [& args] | |
(let [;[controller listener] (leap/controller {:frame (fn [{:keys [frame]}] (process-frame frame))}) | |
; The above is totally valid, but we can spell it out too | |
listener (leap/listener :frame #(process-frame (:frame %)) | |
:default #(println "Toggling" (:state %))) | |
[controller _] (leap/controller listener)] | |
(println "Any enter to quit") | |
(read-line) | |
(leap/remove-listener! controller listener))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. # | |
# Leap Motion proprietary and confidential. Not for distribution. # | |
# Use subject to the terms of the Leap Motion SDK Agreement available at # | |
# https://developer.leapmotion.com/sdk_agreement, or another agreement # | |
# between Leap Motion and you, your company or other organization. # | |
################################################################################ | |
import Leap, sys | |
class SampleListener(Leap.Listener): | |
def on_init(self, controller): | |
print "Initialized" | |
def on_connect(self, controller): | |
print "Connected" | |
def on_disconnect(self, controller): | |
print "Disconnected" | |
def on_exit(self, controller): | |
print "Exited" | |
def on_frame(self, controller): | |
# Get the most recent frame and report some basic information | |
frame = controller.frame() | |
print "Frame id: %d, timestamp: %d, hands: %d, fingers: %d, tools: %d" % ( | |
frame.id, frame.timestamp, len(frame.hands), len(frame.fingers), len(frame.tools)) | |
if not frame.hands.empty: | |
# Get the first hand | |
hand = frame.hands[0] | |
# Check if the hand has any fingers | |
fingers = hand.fingers | |
if not fingers.empty: | |
# Calculate the hand's average finger tip position | |
avg_pos = Leap.Vector() | |
for finger in fingers: | |
avg_pos += finger.tip_position | |
avg_pos /= len(fingers) | |
print "Hand has %d fingers, average finger tip position: %s" % ( | |
len(fingers), avg_pos) | |
# Get the hand's sphere radius and palm position | |
print "Hand sphere radius: %f mm, palm position: %s" % ( | |
hand.sphere_radius, hand.palm_position) | |
# Get the hand's normal vector and direction | |
normal = hand.palm_normal | |
direction = hand.direction | |
# Calculate the hand's pitch, roll, and yaw angles | |
print "Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees\n" % ( | |
direction.pitch * Leap.RAD_TO_DEG, | |
normal.roll * Leap.RAD_TO_DEG, | |
direction.yaw * Leap.RAD_TO_DEG) | |
def main(): | |
# Create a sample listener and controller | |
listener = SampleListener() | |
controller = Leap.Controller() | |
# Have the sample listener receive events from the controller | |
controller.add_listener(listener) | |
# Keep this process running until Enter is pressed | |
print "Press Enter to quit..." | |
sys.stdin.readline() | |
# Remove the sample listener when done | |
controller.remove_listener(listener) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment