Saturday, March 30, 2013

Grasp: A Leap Motion mouse controller for OSX

Check out the code here:

Developing for the Leap Motion controller in Scala

I just got a hold of a Leap Motion controller today and I have to admit it's a neat little device with a lot of potential. The first thing I did was figure out how to interface with the device using Scala. Here are some quick pointers on how to get setup.

Before reading further, entire process I outline below is summed up in the following github repo. You can clone it and run ./install then ./run to produce the following results:


1- Download the SDK here.

2- Assuming you have maven installed, create a project using the following command
mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:generate \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.3 -DgroupId=leap-scala \
-DartifactId=leap-scala -Dversion=1.0.0 -DinteractiveMode=false

3- Install the leap motion sdk jar in your local maven repository (found in the sdk package from step #1 under /LeapSDK/lib/LeapJava.jar). Use the following command:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
    -Dfile=PATH_TO_JAR \
    -DgroupId=com.leapmotion.leap -DartifactId=leapMotion \
    -Dversion=1.0.0 -Dpackaging=jar

4- Add a reference to this jar file in the leap-scala/pom.xml that was created in step #2.

5- Write some code for capturing events from the device. Here are some snippets from the leap provided samples I ported to scala:

6- Once you have some code, you can bundle it along with the leap motion library in a "fat" jar using the maven-assembly-plugin. Check out a sample pom.xml that does this here. You can then bundle the app using the following maven command
mvn install

This will produce a jar file called "leap-scala-1.0.0-jar-with-dependencies.jar" under the target folder.

7- Once your code is compiling, it's time to run. You'll need to point the jvm runtime to the native leap motion libraries for interfacing with your OS. On OSX, this is a file called libLeapJava.dylib found in the same directory of the SDK as the jar file. Here is how you would run your app once it's compiled:
java -Djava.library.path=".:./libs" -jar target/leap-scala-1.0.0-jar-with-dependencies.jar

Using the code above, you should be able to see input directly form the device on your console.

These instructions were generated while developing on OSX. Things might need a little bit of tweaking if running on other platforms.

Thursday, March 7, 2013

How to pretty print JSON from the command line

JSON has become one of the main formats for exchanging data through HTTP APIs. Being able to request and properly print json in your console is useful from time to time. Here are three ways to easily "pretty print" your data.

Option 1 - Simple formatting using python 

If you have access to python on your system, simply add an alias to "python -mjson.tool" to pretty print any well formed json. Execute one of the following commands to add the alias directly (depending on whether you use bash or zsh):
See an example in action:

Option 2 - Prettyjson: a colorized option

prettyjson is a node.js package that you can call form the command line to get colorized and bracket stripped json printed in your CLI.

To install:
Once installed, you can try it out. Here is a before shot:

And after:

As you can see prettyjson does a good job of stripping superflous brackets and quotes. 

Note: If you can't execute prettyjson after installing it, make sure the node package manager bin folder is in your path. You can do so by adding the following to your .bashrc/zshrc:
export PATH=/usr/local/share/npm/bin/:$PATH

Option 3 - underscore-cli

Yet another node.js based option is the underscore-cli package. It doesn't strip some of the less useful json characters form the display like prettyjson, but it does colorize your text and it has other powerful features.

To install:
sudo npm install -g underscore

Underscore pretty formatting in action:

It's not just pretty printing that makes this package neat, you can extract specific portions of it as well:

Head on over to ddposon's github page for more details.

Sunday, March 3, 2013

Babel-hose: Real-time Twitter translations

"Now the whole world had one language and a common speech." -- Genesis 11:1

This was a weekend project of mine which combined Twitters streaming API and Bing translator allowing you to understand what's happening everywhere in the world in real time in your native language. This project leverages the recently released hosebird framework for easy access to the sample and filter streams.

Getting started with the Bing translator API

Bing Translator is the best freely available machine translation API that I know of. If you're interested in getting set up to use the API to make your own translation enabled apps, follow this guide.

Create an Azure marketplace account

1- Go to and login using a Microsoft live account. If you don't have one, you'll need to create one beforehand. You can do that here.

2- Complete the registration:

3- Once logged in, head on over to the bing translator subscription page and select the free 2,000,000 character/month subscription package.

4- If you go back to your account page under "My data" you should see the following:

Create an Azure application

Once you have an Azure marketplace account with a subscription to Bing translator, the last step is to create an application. By doing this, you'll have a client key and secret you can use to call the API. 

1- Head on over to the application registration page. Once there, fill out all the necessary information. For Bing translator, you won't be using a redirect uri (even though it's mandatory). You can put any valid URI you want to complete the form. ** Before hitting submit, copy the Client secret and Client ID. We'll need these later.

2- If you go back to the application registration page, you should see your app show up:

Call the API

Using the client key and secret from above, you have all you need to call the API. If you're going to be implementing a client to call the API yourself, you'll need to first authenticate in order to get an access token, then use that in the header of your requests for authorization. 

I wrote some sample code(here) that retrieves the access token, before realizing there was a really great client for the translator written already called microsoft-translator-java-api. Here is some sample java code using this library which takes care of authentication and translation requests under the covers:

Here is a more involved project that involves Twitters streaming api and bring translator:

Here are some additional resources you might find useful:

Bonne Chance! Que la force soit avec vous.