Web inference.js

Run realtime predictions at the edge, on the browser, with inference.js

inferencejs is a JavaScript package that enables real-time inference via the browser using models trained on Roboflow.

circle-info

See the inferencejs reference here

For most business applications, the Hosted API is suitable. But for many consumer applications and some enterprise use cases, having a server-hosted model is not workable (for example, if your users are bandwidth constrained or need lower latency than you can achieve using a remote API).

Learning Resources

Supported Models

inferencejs currently supports these model architectures:

Installation

To add inference to your project, simply install using npm or add the script tag reference to your page's <head> tag.

Initalizing inferencejs

Authenticating

You can obtain your publishable_key from the Roboflow workspace settings.

circle-exclamation

Start by importing InferenceEngine and creating a new inference engine object

circle-info

inferencejs uses webworkers so that multiple models can be used without blocking the main UI thread. Each model is loaded through the InferenceEngine our webworker manager that abstracts the necessary thread management for you.

Now we can load models from roboflow using your publishable_key and the model metadata (model name and version) along with configuration parameters like confidence threshold and overlap threshold.

inferencejs will now start a worker that runs the chosen model. The returned worker id corresponds with the worker id in InferenceEngine that we will use for inference. To infer on the model we can invoke the infer method on the InferenceEngine.

Let's load an image and infer on our worker.

circle-info

This can take in a variety of image formats (HTMLImageElement, HTMLVideoElement, ImageBitmap, or TFJS Tensor).

This returns an array of predictions (as a class, in this case RFObjectDetectionPrediction )

Configuration

If you would like to customize and configure the way inferencejs filters its predictions, you can pass parameters to the worker on creation.

Or you can pass configuration options on inference

Last updated

Was this helpful?