Get Started
This page provides a step-by-step guide for setting up and using the DeGirumJS JavaScript AI Inference SDK.
Last updated
Was this helpful?
This page provides a step-by-step guide for setting up and using the DeGirumJS JavaScript AI Inference SDK.
Last updated
Was this helpful?
Welcome to the DeGirumJS: a JavaScript AI Inference SDK! This guide will help you get started with integrating AI inference capabilities into your web application. Follow the steps below to set up your environment, connect to an AI server or the cloud, and run inference.
The JavaScript AI Inference SDK allows you to connect to AI Server or Cloud Zoo instances, load AI models, and perform inference on various data types. This guide provides a step-by-step tutorial on how to use the SDK effectively.
To start using the SDK, include the following script tag in your HTML file:
DeGirumJS allows you to load models from an AI server or Cloud Zoo and perform inference on the AI Server hardware or in the cloud. For AI server inference (local or LAN server), you need to have an AI Server running with http protocol enabled :
For running cloud inference or to be able to load a model from the cloud, you need to specify your cloud token.
Instantiate the dg_sdk
class and connect to the AI server using the connect
method:
For running AI Server inference on cloud models, include the URL of the cloud zoo and your token:
For running Cloud inference, specify 'cloud' as the first argument, and include the URL of the cloud zoo and your token:
Now, you can load a model using the zoo class instance's loadModel
method:
Use the predict
method to perform inference on an input image:
You can display prediction results to a HTMLCanvasElement
:
This will draw the inference results onto the canvas.
Understanding the Result Object Structure
The result
object contains the predictions made by the model, such as detected objects, classes, probabilities, bounding boxes, and more.
To get started with a simple example page, we need the following HTML elements on the page:
Here is a HTML page that will perform inference on uploaded images and display the results:
When loading a model, you can specify various options to customize its behavior:
inputCropPercentage
: Set the percentage to crop the input image for crop-first
and crop-last
padding methods.
inputLetterboxFillColor
: Set the color for letterbox background.
inputPadMethod
: Set the padding method for input. Options: letterbox
(default), stretch
, crop-first
, crop-last
.
labelBlacklist
: Specify labels to exclude.
labelWhitelist
: Specify labels to include.
outputConfidenceThreshold
: Set the confidence threshold for outputs.
outputMaxClassesPerDetection
: Set the maximum number of classes per detection.
outputMaxDetections
: Set the maximum number of detections.
outputMaxDetectionsPerClass
: Set the maximum number of detections per class.
outputNmsThreshold
: Set the non-maximum suppression threshold.
outputPoseThreshold
: Set the pose threshold.
outputPostprocessType
: Set the post-process type.
outputTopK
: Set the top K results to output.
outputUseRegularNms
: Use regular non-maximum suppression.
overlayAlpha
: Set the transparency of the overlay.
overlayColor
: Set the color for overlay. Can be one RGB color or an array of RGB colors.
overlayFontScale
: Set the font scale for overlay text.
overlayLineWidth
: Set the width of the overlay lines.
overlayShowLabels
: Show or hide labels in the overlay.
overlayShowProbabilities
: Show or hide probabilities in the overlay.
saveModelImage
: Flag to enable attaching the model input image to the results.
autoScaleDrawing
: Flag to enable auto-scaling of the inference results drawn by displayResultToCanvas()
based on a target display resolution. By default, it uses a reference size of 1920x1080
for optimal viewing. If targetDisplayWidth
or targetDisplayHeight
are specified, they will override the defaults and be used as the reference resolution for scaling. This ensures that visual elements like labels, bounding boxes, and keypoints appear consistent across various input image sizes and display resolutions.
targetDisplayWidth
: (Optional) Set the target display width for auto-scaling the results overlay (if autoScaleDrawing is enabled). Defaults to 1920
if not specified. This allows you to customize the reference width for scaling drawn elements.
targetDisplayHeight
: (Optional) Set the target display height for auto-scaling the results overlay (if autoScaleDrawing is enabled). Defaults to 1080
if not specified. This allows you to customize the reference height for scaling drawn elements.
measureTime
: Flag to enable collection of max, min, count, and average of time taken for inference duration and preprocessing duration in milliseconds. Off by default.
To destroy / clean up a model instance, use the cleanup
method:
This will stop all running inferences and clean up resources used by the model instance.
Both AIServerModel
and CloudServerModel
classes offer flexible ways to manage device types, allowing you to configure and switch between devices dynamically.
Each model has a set of SupportedDeviceTypes
, which indicates the runtime/device combinations that are compatible for inference. The format for device types is "RUNTIME/DEVICE"
, where:
RUNTIME refers to the AI engine or runtime used for inference (e.g., TENSORRT
, OPENVINO
).
DEVICE refers to the hardware type (e.g., CPU
, GPU
, NPU
).
In the AIServerModel
and CloudServerModel
classes, device management is integrated into both the initialization and runtime phases of the model lifecycle. Below are key scenarios and examples:
Default Device Type Selection: When you load a model without specifying a device type, the default device type specified in the model parameters is selected.
Switching Device Types After Initialization: You can change the device type even after the model has been initialized. The model will validate the requested device type against the system’s supported device types.
If the requested device type is not valid, an error will be thrown.
Specifying a Device Type During Initialization: You can specify a device type when loading the model. The model will start with the specified device type if it’s available.
Handling Multiple Device Types: The SDK allows you to provide a list of device types. The first available option in the list will be selected.
Fallback and Error Handling: If none of the specified device types are supported, the model will throw an error, ensuring that only valid configurations are used.
Supported Device Types: You can check the supported device types for a model using the supportedDeviceTypes
property.
System Supported Device Types You can check the system’s list of supported devices for inference using the getSupportedDevices()
method of the dg_sdk
class.
Device management in both AIServerModel
and CloudServerModel
is designed to be flexible, allowing you to fine-tune the inference environment. You can easily switch between device types, handle fallbacks, and ensure that your models are always running on supported configurations.
Enabling the measureTime
flag will create a timeStats
object within the model which holds various statistics (max, min, count, average) to track how long certain operations took.
ImagePreprocessDuration_ms
: Time taken for preprocessing the input image.
CorePreprocessDuration_ms
: Duration of server-side pre-processing step.
CoreInferenceDuration_ms
: Time taken for the actual inference operation on the server (between sending frame and receiving results).
CoreLoadResultDuration_ms
: Duration of server-side data movement step.
CorePostprocessDuration_ms
: Duration of server-side post-processing step.
PythonPreprocessDuration_ms
: Duration of client-side pre-processing step including data loading and data conversion time.
FrameTotalDuration_ms
: Total duration from calling the predict()
or predict_batch()
method to receiving the results.
DeviceInferenceDuration_ms
: (Orca models only) Duration of AI inference computations on AI accelerator hardware (DeGirum Orca).
DeviceTemperature_C
: (Orca models only) Internal temperature of AI accelerator hardware in Celsius (DeGirum Orca).
DeviceFrequency_MHz
: (Orca models only) Working frequency of AI accelerator hardware in MHz (DeGirum Orca).
getTimeStats()
: Use this method to return a formatted string of all the statistics collected so far.
resetTimeStats()
: Use this method to delete all your old statistics and create a fresh timeStats
object to collect more statistics with.
To access the timeStats
object directly, you can use modelName.timeStats.stats["statName"]
, where the statName
is one of the operations tracked.
deviceType
: Specify the runtime and hardware for inference. See .
For detailed information on the SDK's classes, methods, and properties, refer to the .