Building Gesture Recognition Web Apps with Intel RealSense SDK

Published Date
03 - Dec - 2015
| Last Updated
08 - Feb - 2016
Building Gesture Recognition Web Apps with Intel RealSense SDK


In this article, we will show you how to build a web application that can detect various types of gestures using the Intel® RealSense™ SDK and front facing (F200) camera. Gesture recognition will give users of your application another innovative means for navigation and interface interaction. You will need basic knowledge of HTML, JavaScript*, and jQuery in order to complete this tutorial.

Hardware Requirements

  • 4th generation (or later) Intel® CoreTM processor
  • 150 MB free hard disk space
  • 4 GB RAM
  • Intel® RealSense™ camera (F200)
  • Available USB3 port for the Intel® RealSense camera (or dedicated connection for integrated camera)

Software Requirements

  • Microsoft Windows* 8.1 (or later)
  • A web browser such as Microsoft Internet Explorer*, Mozilla Firefox*, or Google Chrome*
  • The Intel® RealSense Depth Camera Manager (DCM) for the F200, which includes the camera driver and service, and the Intel ®RealSense SDK. Go here to download components.
  • The Intel® RealSense SDK Web Runtime. Currently, the best way to get this is to run one of the SDK’s JavaScript samples, which can be found in the SDK install directory. The default location is C:\Program Files (x86)\Intel\RSSDK\framework\JavaScript. The sample will detect that the web runtime is not installed, and prompt you to install it.



Please make sure that you complete the following steps before proceeding further.
1. Plug your F200 camera into a USB3 port on your computer system.
2. Install the DCM.
3. Install the SDK.
4. Install the Web Runtime.
5. After installing the components, navigate to the location where you installed the SDK (we’ll use the default path):

C:\Program Files (x86)\Intel\RSSDK\framework\common\JavaScript
You should see a file called realsense.js. Please copy that file into a separate folder. We will be using it in this tutorial. For more information on deploying JavaScript applications using the SDK, click here.

Code Overview

For this tutorial, we will be using the sample code outlined below. This simple web application displays the names of gestures as they are detected by the camera. Please copy the entire code below into a new HTML file and save this file into the same folder as the realsense.js file. Alternatively, you can download the complete web application by clicking on the code sample link at the top of the article. We will go over the code in detail in the next section.

The Intel® RealSense SDK relies heavily on the Promise object. If you are not familiar with JavaScript promises, please refer to this documentation for a quick overview and an API reference.

Refer to the Intel® RealSense SDK documentation to find more detail about SDK functions referenced in this code sample. The SDK is online, as well as in the doc directory of your local SDK install.

01 <html>

02 <head>

03 <title>RealSense Sample Gesture Detection App</title>

04  <script type="text/javascript" src=""></script>

05 <script type="text/javascript" src=""></script>

06  <script type="text/javascript" src=""></script>

07  <script type="text/javascript" src="realsense.js"></script>

08  <script>

09  var sense, hand_module, hand_config

10  var rs = intel®.realsense


12  function DetectPlatform() {

13 rs.SenseManager.detectPlatform(['hand'], ['front']).then(function (info) {

14  if (info.nextStep == 'ready') {

15 Start()

16  }

17    else if (info.nextStep == 'unsupported') {

18  $('#info-area').append('<b> Platform is not supported for Intel(R) RealSense(TM) SDK: </b>')

19    $('#info-area').append('<b> either you are missing the required camera, or your OS and browser are not supported </b>')

20   }

21   else if (info.nextStep == 'driver') {

22    $('#info-area').append('<b> Please update your camera driver from your computer manufacturer </b>')

23   }

24   else if (info.nextStep == 'runtime') {

25  $('#info-area').append('<b> Please download the latest web runtime to run this app, located <a href="">here</a> </b>')

26  }

27  }).catch(function (error) {

28  $('#info-area').append('Error detected: ' + JSON.stringify(error))

29  })

30   }


32  function Start() {

33  rs.SenseManager.createInstance().then(function (instance) {

34   sense = instance

35 return rs.hand.HandModule.activate(sense)

36  }).then(function (instance) {

37  hand_module = instance

38   hand_module.onFrameProcessed = onHandData

39   return sense.init()

40    }).then(function (result) {

41 return hand_module.createActiveConfiguration()

42  }).then(function (result) {

43  hand_config = result

44  hand_config.allAlerts = true

45   hand_config.allGestures = true

46  return hand_config.applyChanges()

47   }).then(function (result) {

48   return hand_config.release()

49   }).then(function (result) {

50  sense.captureManager.queryImageSize(rs.StreamType.STREAM_TYPE_DEPTH)

51   return sense.streamFrames()

52 }).catch(function (error) {

53   console.log(error)

54  })

55   }


57 function onHandData(sender, data) {

58  for (g = 0; g < data.firedGestureData.length; g++) {

59  $('#gesture-area').append(data.firedGestureData[g].name + '<br />')

60  }

61 }


63 $(document).ready(DetectPlatform)

64  </script>

65 </head>


67 <body>

68  <div id="info-area"></div>

69  <div id="gesture-area"></div>

70 </body>

71 </html>

The screenshot below is what the app looks like when you run it and present different types of gestures to the camera.

Detecting the Intel® RealSense™ Camera on the System

Before we can use the camera for gesture detection, we need to see if our system is ready for capture. We use the detectPlatform function for this purpose. The function takes two parameters: the first is an array of runtimes that the application will use and the second is an array of cameras that the application will work with. We pass in ['hand'] as the first argument since we will be working with just the hand module and ['front'] as the second argument since we will only be using the F200 camera.

The function returns an info object with a nextStep property. Depending on the value that we get, we can determine if the camera is ready for usage. If it is, we call the Start function to begin gesture detection. Otherwise, we output an appropriate message based on the string we receive back from the platform.

If there were any errors during this process, we output them to the screen.

01 rs.SenseManager.detectPlatform(['hand'], ['front']).then(function (info) {

02  if (info.nextStep == 'ready') {

03  Start()

04  }

05  else if (info.nextStep == 'unsupported') {

06   $('#info-area').append('<b> Platform is not supported for Intel(R) RealSense(TM) SDK: </b>')

        $('#info-area').append('<b> either you are missing the required camera, or your OS and browser are not supported </b>')

08  }

09  else if (info.nextStep == 'driver') {

10  $('#info-area').append('<b> Please update your camera driver from your computer manufacturer </b>')

11  }

12  else if (info.nextStep == 'runtime') {

13   $('#info-area').append('<b> Please download the latest web runtime to run this app, located <a href="">here</a> </b>')

14  }

15 }).catch(function (error) {

16 $('#info-area').append('Error detected: ' + JSON.stringify(error))

17 })

Setting Up the Camera for Gesture Detection

1 rs.SenseManager.createInstance().then(function (instance) {

2 sense = instance

3  return rs.hand.HandModule.activate(sense)

4 })

You need to follow a sequence of steps to set up the camera for gesture detection. First, create a newSenseManager instance and enable the camera to detect hand movement. The SenseManager is used to manage the camera pipeline.

To do this, we will call the createInstance function. The callback returns the instance that we just created, which we store in the sense variable for future use. We then call the activate function to enable the hand module, which we will need for gesture detection.

1 .then(function (instance) {

2  hand_module = instance

3  hand_module.onFrameProcessed = onHandData

4  return sense.init()

5 })

Next, we need to save the instance of the hand tracking module that was returned by the activatefunction into the hand_module variable. We then assign the onFrameProcessed property to our own custom callback function called onHandData whenever new frame data is available. Finally, we initialize the camera pipeline for processing by calling the Init function

1  .then(function (result) {

2 return hand_module.createActiveConfiguration()

3 })

To configure the hand tracking module for gesture detection, you have to create an active configuration instance. This is done by calling the createActiveConfiguration function.

1 .then(function (result) {

2 hand_config = result

3  hand_config.allAlerts = true

4 hand_config.allGestures = true

5 return hand_config.applyChanges()

6 })

The CreateActiveConfiguration function returns the instance of the configuration, which is stored in the hand_config variable. We then set the allAlerts property to true to enable all alert notifications. The alert notifications give us additional details such as the frame number, timestamp, and the hand identifier that triggered the alert. We also set the allGestures property to true, which is needed for gesture detection. Finally, we call the applyChanges function to apply all parameter changes to the hand tracking module. This makes the current configuration active.

1 .then(function (result) {

2 return hand_config.release()

3 })

We then call the release function to release the configuration.

1 .then(function (result) {

2 sense.captureManager.queryImageSize(rs.StreamType.STREAM_TYPE_DEPTH)

3 return sense.streamFrames()

4 }).catch(function (error) {

5 console.log(error)

6 })

Finally, the next sequence of functions sets up the camera to start streaming frames. When new frame data is available, the onHandData function will be invoked. If any errors were detected, we catch them and log all errors to the console.

The onHandData function

1 function onHandData(sender, data) {

2  for (g = 0; g < data.firedGestureData.length; g++) {

3 $('#gesture-area').append(data.firedGestureData[g].name + '<br />')

4  }

5 }

The onHandData callback is the main function where we check to see if a gesture has been detected. Remember this function is called whenever there is new hand data and that some of the data may or may not be gesture-related data. The function takes in two parameters, but we use only the data parameter. If gesture data is available, we iterate through the firedGestureData array and get the gesture name from the name property. Finally, we output the gesture name into the gesture-area div, which displays the gesture name on the web page.

Note that the camera remains on and continues to capture gesture data until you close the web page.

For more such Android resources and tools from Intel®, please visit the Intel® RealSense™ Developer Zone

Source: Protection Status