Está en la página 1de 8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

Get started with XBOX Kinect 2 JavaScript


development
Published last year by Mate Marschalko
Ive received many emails asking for help setting up the Xbox Kinect sensor to work
with JavaScript so I decided to publish a complete guide. This will let anyone with a
Microsoft Kinect develop JavaScript applications tracking full body movements and
gestures in real-time.

Kinect for Windows


Its important to note that there are multiple versions of the Kinect sensor and not
all of them are recognised by personal computers therefore couldnt be used for
JavaScript development.

XBOX Kinect, rst generation


http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

1/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

The rst generation of the Kinect sensor introduced in 2010 was called Xbox 360
Kinect and was intended to be used with the Xbox 360 gaming console. Later,
Microsoft released another version called Kinect for Windows. This sensor looked
exactly the same but had improved features for kiosk and application development
like Near Mode. The biggest dierence is that the Kinect for Windows was licensed
for commercial app distribution, and the 360 one is for development work only.
Both of these sensors have a USB connector and are compatible with PCs so can be
used for JavaScript development.

Kinect for Windows, second generation


In 2013 a new Kinect sensor was released with the XBOX One console as well as a
new Kinect for Windows so both rst generation sensors are now outdated.
Unfortunately the XBOX One Kinect that comes with the console can no longer be
used for development work like you can with the XBOX 360 Kinect.
In case you have a rst generation Kinect and you wish to develop JavaScript
applications on a Macintosh, try using the Zigfu browser plugin. I tried it with my
Xbox 360 Kinect and it worked perfectly! Alternatively, on a Windows machine you
can use the ocial JavaScript SDK.
In this introduction we will be dealing with the new generation of the Kinect for
Windows sensor.

Kinect for Windows V2


http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

2/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

XBOX and the Kinect are Microsoft products so its understandable that they want to
restrict development to Windows PCs only. There are a few libraries like Libfreenect2
that, in theory, could enable Macs to work with the Kinect but they are not very
reliable. I couldnt get it working possibly due to this issue.
From my experience after weeks and weeks of researching, testing and talking to at

Back

Blog Resources

least a dozen developers, it seems that the most reliable way of working with the
Kinect cross-platform, and with JavaScript most importantly, is this: Connect the

Kinect to a Windows machine running Node.js and the Kinect2 library. This Node.js
application on the Windows machine can receive the sensor data with no issues from
the ocial SDK. From there we can publish and stream the data through web
sockets to any other machine: Windows, Mac or Linux. So essentially we are only
using the Windows machine as a low level server that reads pure data from the
Kinect and publishes it for other computers to use and interpret.

Visual representation of the tracking data in the Kinect SDK


For the simple task of streaming data you probably dont want to maintain a full
desktop machine so I was looking at options to go as cheap and small as possible. It
would be interesting to see if the new Raspberry PI 2 running Windows 10 IoT could
handle this task? Or maybe one of these Stick PCs?
On the other hand you might just want to consider writing the whole application on
that Windows machine as you already have the Node.js JavaScript environment set
up. I personally dont want to so we will denitely look into streaming the data over
to my Macbook Pro.

Installation
http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

3/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

Lets setup the Windows machine rst, the one that is going to receive and handle
the sensor data in the Node.js application. The reason why the Kinect 2 Node.js
library we are going to use only works on Windows machines is that it relies on the
ocial Kinect 2 SDK that can only be installed to Windows machines. Go ahead, and
install it rst from this page. You might want to restart your machine after this step.
Please
Windows
8 or
Backalso note that in order to install the SDK you will need a 64bitBlog
Resources
higher.

Download
FREE installed
Ebook: Introduction
to JavaScript
Next, you
need Node.js
in the Windows
commandElectronics
line. The ocial
Windows Installer can be downloaded from nodejs.org. If the installation was
successful entering nodev into the command line will give you the version number
of Node installed. You need at least version 0.11.3 for the Kinect 2 module to work.
The installation will depend on node-gyp so install that too: npminstallgnode
gyp .
If that wasnt enough, you will need to install Microsoft Visual Studio C++ 2013 for
Windows Desktops (Express version works too) and if you have a 64-bit build then
the Windows 7 64-bit SDK. Yes, even if you are not on Windows 7.
After that you can type npminstallkinect2 and the library will be installed to your
machine

Reading skeleton data


Now that everything is installed, lets test and extract the skeleton tracking data!
Create a new folder and a new JavaScript le for your Kinect application. Name it
output.js for instance. We need to start o this le by loading the Kinect 2 library
and initialising it:

varKinect2=require('kinect2');
varkinect=newKinect2();

After that we power up the Kinect with the .open() method and test if it returned true
in the same line which means that the start up was successful:

if(kinect.open()){
console.log("Kinectopened!");
}

http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

4/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

Inside this if condition we can now add an asynchronous event listener that waits for
the skeleton data to arrive:

kinect.on('bodyFrame',function(bodyFrame){
for(vari=0;i<bodyFrame.bodies.length;i++){

Back

Blog Resources

if(bodyFrame.bodies[i].tracked){
console.log(bodyFrame.bodies[i]);
Download FREE Ebook: Introduction to JavaScript Electronics
}
}
});
//requestbodyframes
kinect.openBodyReader();
//closethekinectafter1minute
setTimeout(function(){
kinect.close();
console.log("KinectClosed");
},60000);

Running this js le from the project folder with nodeoutput.js will result in
outputting the pure sensor data in a JSON format.

http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

5/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

Data we get back from the Kinect


Here's the whole script in one, again:

varKinect2=require('kinect2');
Back

Blog Resources

varkinect=newKinect2();
Download FREE Ebook: Introduction to JavaScript Electronics
if(kinect.open()){
console.log("KinectOpened");
//listenforbodyframes
kinect.on('bodyFrame',function(bodyFrame){
for(vari=0;i<bodyFrame.bodies.length;i++){
if(bodyFrame.bodies[i].tracked){
console.log(bodyFrame.bodies[i]);
}
}
});
//requestbodyframes
kinect.openBodyReader();
//closethekinectafter5seconds
setTimeout(function(){
kinect.close();
console.log("KinectClosed");
},60000);
}

Now you have the Kinect 2 skeleton data available in Node.js and JavaScript!
Next week Im going to publish a new post in which we will carry on and:
- Analyse the skeleton data and see what properties of the body can we extract from
it
- Create a JavaScript web socket server on the Windows machine and stream the
skeleton data for browsers running any operating system on any machine
- Connect to the web socket server from Chrome on a Macintosh and visualise the
skeleton data with HTML and CSS

http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

6/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

- Dene and interpret gestures from the pure data and create a full screen photo
gallery that can be swiped back and forth with arm movements
Follow me on Twitter or Facebook in case you are interested and don't want to miss
the new post.

Back
Back to all

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

Leave a Reply
You must be logged in to post a comment.

Free Ebook
Step up your web developer career and learn hardware prototyping.
This ebook will get you started with JavaScript Arduino electronics development in a
couple of hours.
Email address

Send me the PDF

Web on Devices
Electronics Hacking with JavaScript and other Web Technologies
http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

7/8

22/9/2016

GetstartedwithXBOXKinect2JavaScriptdevelopment

Twitter

Facebook

Back

Blog Resources

Download FREE Ebook: Introduction to JavaScript Electronics

Mate Marschalko
Front-end Web Developer, Creative Technologist and Maker. Builds Internet connected devices for the Internet of
Things.

All rights reserved | Contact at hello@webondevices.com

http://www.webondevices.com/getstartedwithxboxkinect2javascriptdevelopment/

8/8

También podría gustarte