3D camera streaming with Javascript and Chrome

Depth cameras such as Microsoft’s Kinect or Primesense’s ASUZ are devices that provides environment information in different channels, mainly RGB and depth distance. To enabled users to utilize these devices, developers must provide installable controllers for the specific user’s operating system. In some cases, these controllers are bounded to vendor’s frameworks and therefor not available to modification or portability. So this becomes the first challenge. How can we enable this technology into users’s personal computer in the simplest and compatible way?

_ Google's Chrome Apps API

Chrome allow web developers to securely access some of the operating system components that are not available through standard commercial web browsers: file system, network sockets, USB connections, to mention a few. Connecting a USB 3D camera via Chrome Apps will be the first approach to eliminate the user’s need to install drivers per device, per operating system.

Opening a connection to a USB device through Chrome’s API can be as simple as:

There are different ways to send and to send-receive data from a USB device:

Control Transfers

Isochronous Transfers

Bulk Transfers

Deciding which one to use is completely up to the vendor’s specifications. For example: Kinect use control transfers to stream the RGB and Depth data, and Primesense ASUZ use bulk transfers for the same purpose.

After working with different devices it was possible to identify a common communication process:

Device connection

Camera information recollection

Configuration setup

Streaming (initialization and finalization)

So it is possible to design a framework with an abstract definition of each step, and then implement each one for every device. This will allow future development to incorporate more devices under the same framework.

_ PrimeSense Firmware Hack > 5.0

Once we opened the device with Chrome USB API lets get the firmware version and initialize the configuration. To establish a clear communication with the firmware we need to prepare the host protocol. In this case USB Control Transfers packages will do the work with two main sequences, one for sending data, and another for asking data back from the device.

This is an example of a control transfer using the PrimeSense firmware's host protocol, and it will ask to the camera the VERSION of the firmware.

The direction, requestType and recipient can be masked out from one single byte, rtype. the request, value and index must be provided. Finally, if the request type is OUT then set the data attribute, otherwise, if receiving data then set the length attribute. If the resultCode is zero then we can unpack the data.

After obtaining dozens of parameters from the firmware and setting up the necessary configuration parameters to the device we are ready to initialize the streaming using Bulk Transfers.

Requesting a Bulk Transfer from the a camera device can be tricky. Let's recall that this USB operations are all made asynchronously by Chrome. As you may notice already, we are using RSVP. In order to keep the speed up to the real time streaming speed, say latency, we need to attack the device with multiple bulk requests as fast as we can; care about the data later. Wait, do we need threads? No. well, not necessary. We are going to take advantage of the asynchronous calls and setup a limited number of buffers to request the data.

A simple algorithm will setup multiple buffers, and continue recursively until error or manual stop.

_ Depth and Video Streaming

We have the buffers filled up with data and its time for some heavy processing. This are some of the algorithms we need to apply to the raw data:

1Depth and image raw data decompression.
It is a good idea to setup data compression on the firmware. This way, a single thread can keep up with small bulk transfers.

2Depth registration.
We need to match each pixel position from the depth data with the image data. Building a registration table once and only shifting the frame pixels will accelerate the process.

3YUV to RGB image conversion.
The firmware can be setup with a YUV image format. A simple algorithm can be applied for each pixel to do the conversion.

4Frame synchronization.
If we setup multiple bulk buffers per each streaming channel, we should expect to receive depth and image frames randomly, but only on frame with depth and image data should be triggered at a time.

_ HTML Canvas Render

Using a simple canvas we can render the RGB data, after adding a forth byte for the alpha channel, and also we can show the depth data building a RGBA per pixel.

Depth and image channels were blended.

Registration is perfect (depth and image pixels match together).

The 3D camera has its own limitations over the blind spots.

_ Three.js and PointCloud

Lets take this information to the next level and build a 3D PointCloud render using Three.js. This is how we first initialize our PointCloud viewer.

After filling the geometry vertices with the depth channel, and the geometry colors with the rgb channel, we can render the 3D input frame by frame. Also adding an orbit controller will make it even better for navigation.

Lets try a live streaming processing using up to 3 javascript workers to process all the streaming data per channel. There is still a visible lag on the streaming so a better improvement can be done.

Now think about the applications over the web that javascript developers can implement if we turn this into an opened source library available for everyone. The topics are broad wide: hand and face recognition, 3D video conference, 3D scenery preservation, web interaction, indoor exploration, and more. Do you have any ideas? Share us some applications you can think of using this kind of web technologies.