[#LEAPMOTION] HowTo: Show the number of fingers ever the Leap

Hello!

Today is going to be a quick post, I’ll change the console app for a WPF app to work with the Leap Motion. In this app, I’ll show how a gesture is detected by the sensor in addition to the amount of active fingers over the sensor. For this example I’ve used some information from in this post of the great Iris Classon to create the following listener

   1: using System;

   2: using System.Threading.Tasks;

   3: using Leap;

   4:

   5: namespace LeapWpf01

   6: {

   7:     public class ElBrunoListener : Listener

   8:     {

   9:         public void Sample(){}

  10:         public event Action<FingerList> OnFingersRegistered;

  11:         public event Action<GestureList> OnGestureMade;

  12:         private long _now;

  13:         private long _previous;

  14:         private long _timeDifference;

  15:

  16:         public override void OnInit(Controller controller) { }

  17:

  18:         public override void OnConnect(Controller controller)

  19:         {

  20:             controller.EnableGesture(Gesture.GestureType.TYPECIRCLE);

  21:             controller.EnableGesture(Gesture.GestureType.TYPEKEYTAP);

  22:             controller.EnableGesture(Gesture.GestureType.TYPESCREENTAP);

  23:             controller.EnableGesture(Gesture.GestureType.TYPESWIPE);

  24:         }

  25:

  26:         public override void OnDisconnect(Controller controller) { }

  27:

  28:         public override void OnExit(Controller controller) { }

  29:

  30:         public override void OnFrame(Controller controller)

  31:         {

  32:             var frame = controller.Frame();

  33:             _now = frame.Timestamp;

  34:             _timeDifference = _now - _previous;

  35:

  36:             if (frame.Hands.IsEmpty) return;

  37:

  38:             _previous = frame.Timestamp;

  39:             if (_timeDifference < 1000) return;

  40:

  41:             // Run async

  42:             if (frame.Gestures().Count > 0)

  43:                 Task.Factory.StartNew(() => OnGestureMade(frame.Gestures()));

  44:             if (frame.Fingers.Count > 0)

  45:                 Task.Factory.StartNew(() => OnFingersRegistered(frame.Fingers));

  46:         }

  47:     }

  48: }

The listener is quite simple, public an event with the information captured by the LEAP every 1 second.

In the MainWindow of my WPF app, I have the following code. The only thing that must be taken into consideration, is that the best sensor initialization in the Loaded() event, and always with care for closing the View close the controller and the listener with the sensor.

   1: using System;

   2: using System.ComponentModel;

   3: using System.Diagnostics;

   4: using System.Runtime.CompilerServices;

   5: using System.Windows;

   6: using Leap;

   7: using LeapWpf01.Annotations;

   8:

   9: namespace LeapWpf01

  10: {

  11:     public partial class MainWindow : INotifyPropertyChanged

  12:     {

  13:         private string _gesturesText;

  14:         private Controller _controller;

  15:         private ElBrunoListener _listener;

  16:         private string _fingersText;

  17:

  18:         public MainWindow()

  19:         {

  20:             DataContext = this;

  21:             Loaded += MainWindow_Loaded;

  22:             Closing += MainWindow_Closing;

  23:             InitializeComponent();

  24:         }

  25:

  26:         void MainWindow_Closing(object sender, CancelEventArgs e)

  27:         {

  28:             _controller.RemoveListener(_listener);

  29:             _controller.Dispose();

  30:         }

  31:

  32:         void MainWindow_Loaded(object sender, RoutedEventArgs e)

  33:         {

  34:             _listener = new ElBrunoListener();

  35:             _controller = new Controller();

  36:             _controller.AddListener(_listener);

  37:             _listener.OnFingersRegistered += OnFingersRegistered;

  38:             _listener.OnGestureMade += OnGestureMade;

  39:         }

  40:

  41:         void OnGestureMade(GestureList gestures)

  42:         {

  43:             var gesturesData = string.Empty;

  44:             foreach (var gesture in gestures)

  45:             {

  46:                 gesturesData += gesture.Type + Environment.NewLine;

  47:             }

  48:             GesturesText = gesturesData;

  49:         }

  50:

  51:         void OnFingersRegistered(FingerList fingers)

  52:         {

  53:             FingersText = "Active Fingers:" + fingers.Count;

  54:         }

  55:     }

  56: }

Then, the functionality of the app is pretty straigh forward,

 

The sensor is OK, but in order to have something up and running, you have to work a lot. The SDK gives you raw information , so you have to work with this information to get cool results

Saludos @ Home

El Bruno

image image image Google
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s