We are happy to announce a new release of our Web SDK. It takes a more modern approach to web development and aims to ease the integration with your web apps. This blog post will list all the exciting updates v4.0.0 has to offer.
DeepAR makes it easy to add augmented reality features to any website. From virtual try-on for makeup, sneakers, or glasses to removing background from a video call, augmented reality is fast becoming a staple ingredient to modern web interactions.
Our demo projects can be used to get AR up and running on iOS, Android, MacOS or the web in under 30 minutes. We have hundreds of readymade AR effects you can use to create a variety of experiences to sell, entertain and engage. Using DeepAR Creator Studio, you can create custom AR effects with the features of those on Instagram or SnapChat and more.
NPM is a public repository of many Web frameworks and libraries. Publishing code to NPM will massively increase the availability of the DeepAR Web library and improve the ease of the integration process for new developers.
👉 Yeah, you heard that right. DeepAR Web is now available on NPM.
You are just one command away from installing the most powerful AR framework for the Web.
And 13 lines of code away from running your first demo. It's that easy!
DeepAR Web comes with deepar.esm.js file. It contains all the functionality of the DeepAR SDK in one file. Most web apps compose of many libraries and files. To import DeepAR functionality into your code:
Here is an example of how to use ES modules directly in HTML:
If there is one thing that all developers love, it is when IDE helps them to be more productive. This is the main reason we rewrote our entire Web SDK.
👉 DeepAR Web is now written in Typescript.
📓 transpile - fancy combination of words transform and compile.
tsc - a Typescript compiler.
DeepAR Web comes with a pair of these files:
Extension .d.ts denotes a Typescript definition file. It describes all the classes, methods, and properties of the deepar.js file. How can this help a developer?
💡 In conclusion: with Typescript definition .dt.s files you have API reference and autocomplete inside your IDE.
Webpack and Rollup merge code into a single bundle. By default, they rename and place assets in different directory structures. Previous versions of SDK had problems when integrating with bundlers. The main issue was with fetching the WebAssembly and machine learning models.
In previous versions of SDK, DeepAR assumed the paths for those assets and developers could not change them. This changes with DeepAR v4.0.0.
👉 DeepAR Web now expects paths for all asset files.
A cool example here is using Webpack’s asset modules in tandem with deepar npm package. Add this to your webpack.config.js:
Then you can include DeepAR SDK and its assets:
Then initialize DeepAR:
DeepAR SDK is a multi-platform framework working across all modern platforms:
With support for some major mobile frameworks:
DeepAR core SDK developers have an unthankful job of developing and maintaining all the fun features that clients expect across a range of very different platforms. To make the development and maintenance of DeepAR more manageable and productive, core devs had to do some major refactoring and introduce some upgrades on the Web side of SDK.
🚘 The DeepAR car did not get just a shiny new body and bumpers. It also got a massive engine upgrade!
The core of the DeepAR SDK is written in C++ and we compile it down to WebAssembly with Emscripten. After a few years, we finally managed to upgrade our Emscripten toolchain to the latest version. What held us back from upgrading earlier is the inability to compile some C++ dependencies with the new Emscripten toolchain because we did not have an access to the code of those dependencies.
In addition to some of our internal code optimizations and code size analysis, these are the results:
🔥 Performance and stability improvements → More FPS!
Some web apps need the camera and effects preview from DeepAR even when the browser tab is not focused. A typical example is a video calling app that utilizes background blurring. When you are in a video call, you will at one point need to switch browser tabs to check something on the web. The last thing you want to happen is for your camera preview to freeze.
❄️ The new DeepAR Web SDK can render in the background even if the browser tab is not focused.
A typical app that does some processing and rendering has something called a game loop. In web browsers, the game loop cannot be infinite since the app needs to yield the execution back to the browser for a preview of the web page.
DeepAR renders its preview to an HTML canvas element. Before rendering it needs to do face tracking and calculate the engine state. All this happens on callbacks from requestAnimationFrame by the browser. Unfortunately, requestAnimationFrame stops calling when the tab is not focused to save up on processor consumption.
The natural choice seems to use the setTimeout function and call the game loop update function 30 times a second (if you want 30 FPS). But this also doesn’t yield good results since browsers don’t call setTimeout with given intervals when the tab is not focused. It is called with much longer intervals and you end up with 5 FPS instead of 30 FPS.
So how do we solve this issue? There is one funny hack. What can play in the background even when the tab is not focused? Don’t know? Sound!
We used Web Audio API to connect the Gain node and Oscillator node. We play the oscillator node 30 times a second and attach a callback every time the sound playing ends. The callback is attached to the game loops update function. Now you are asking yourself, will I be hearing a high-frequency beeping when I use DeepAR in the unfocused tab? Of course not, we set the gain of the gain node to 0, you silly!
We used 30 FPS here as an example. DeepAR by default previews in 30 FPS but that can be configured via API.
An example of this in action is our Vonage video calling API integration example. Check it out here. The DeepAR API to enable this is:
Some time ago we transitioned from the native C++ TensorFlow runtime to the TensorFlow.js runtime for performance boost in browsers. This made our code scattered between cumbersome C++ and JS interop. We no longer had calls from C++ code to do inference on the camera image. Rather, all the logic was written in JS and passed results down to C++ which consumed them by awkwardly reading and setting some global flags. This makes the code completely incomprehensible unless you knew exactly what was happening.
We don’t have a problem with writing the logic in JS but communicating with global flags is a big no-no! What we wanted is to call the JS code directly from C++ with standard C functions.
We had the TensorFlow.js library wrapped in our internal tracking JS library that was created with a bundler. Here is how we used that JS library in our Emscripten toolchain and called the JS logic directly from C++.
DeepAR Web is finally created with Webpack. Inside our client-facing JS code, we import the internal library and pass it to Emscripten via module. To enable this you need to enable MODULARIZE and EXPORT_ES6 features of Emscripten (see settings.js).
Then inside your Emscripten pre.js add the next line to make the library exports accessible across all your Emscripten js code.
Now you can access the library anywhere in your JS code like this:
Considering you are generating production-level code, you will try to optimize and reduce emitted JS code size with emcc flags -Os or -Oz. Emscripten will use Google’s closure compiler for this job. The closure will do all sorts of heavy optimizations that you need to keep in mind otherwise your code will not work (see Google Devs). In this instance, closure will rename foo, bar, and baz function calls and in runtime, it would throw an exception saying that no such functions exist. To fix this you can do the following:
1. Call the functions with strings because Closure never renames strings.
2. Create a closure externs file.
Building new technology sometimes means that you have to break the older tech. We are always sad when we have to break backward compatibility with previous versions of SDK. It was a tough decision but we finally made it because it is a step in the right direction.
📌 All changes are made to help developers with easier and more standard/modern integration.
Don't be intimidated! Changes are not big.
We prepared a handy document that lists all the things you need to know when migrating to DeepAR Web v4.0.0 from previous versions.
⏱ Migrating to the new version takes 5-10 minutes.
That's all folks,
DeepAR Team 💙
We write about AR case studies, insights and the newest AR tech we're creating.