Skip to content

Cloudservermodel

CloudServerModel

This class handles interactions with a cloud server for running inference.
It uses socket.io for communication with the server and uses internal queues to provide results in order.
Use this class to send image data to the server, retrieve predictions, and manage model parameters.
Internally, it connects to the server using socketio upon first inference. It disconnects when predict() calls have finished, predict_batch() generator stopped, or when the cleanup method is called.

Kind: global class

new CloudServerModel(options, [additionalParams])

Do not call the constructor directly. Use the loadModel method of an AIServerZoo instance to create an AIServerModel.

Param Type Default Description
options Object

Options for initializing the model.

options.modelName string

The name of the model to load.

options.serverUrl string

The URL of the server to connect to.

options.modelParams Object

The parameters for the model.

options.token string

The authentication token for the model.

options.systemDeviceTypes Array.<string>

A list of device types available in the system.

[options.max_q_len] number 80

The maximum length of the internal queues.

[options.callback] function

The callback function to call when results are available.

[options.labels] Array.<string>

The labels for the model.

[options.debugLogsEnabled] boolean true

Whether to enable detailed console logs.

[additionalParams] Object

Additional parameters for the model.

Example (Usage:)

let model = zoo.loadModel('model_name', {});
- Use the `predict` method for inference with individual data items or `predict_batch` for multiple items.
let result = await model.predict(someImage);
for await (let result of model.predict_batch(someDataGeneratorFn)) { ... }

cloudServerModel.predict(imageFile, [info], [bypassPreprocessing]) ⇒ Promise.<Object>

Predicts the result for a given image. This method overrides the base implementation to add cloud-specific logic, such as closing the socket connection after a single prediction (if no callback is used).

Kind: instance method of CloudServerModel
Returns: Promise.<Object> -

The prediction result.

Param Type Default Description
imageFile Blob | File | string | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | ArrayBuffer | TypedArray | ImageBitmap

The image data to process.

[info] string "performance.now()"

Unique frame information provided by user (such as frame num). Used for matching results back to input images within callback.

[bypassPreprocessing] boolean false

If true, sends Blob data directly to the socket without any SDK-side preprocessing.

Example (If callback is provided:)

// onResult handler will invoke the callback directly when the result arrives.
model.predict(someImage);
Example (If callback is not provided:)
// The function waits for the resultQ to get a result, then returns it.
let result = await model.predict(someImage);

cloudServerModel.predict_batch(data_source, [bypassPreprocessing])

Predicts results for a batch of data. Will yield results if a callback is not provided.

Kind: instance method of CloudServerModel

Param Type Default Description
data_source AsyncIterable | ReadableStream

Either an async iterable or a ReadableStream.

[bypassPreprocessing] boolean false

Whether to bypass preprocessing.

Example (The function asynchronously processes results. If a callback is not provided, it will yield results.)

for await (let result of model.predict_batch(data_source)) {
  console.log(result);
}

cloudServerModel.displayResultToCanvas(combinedResult, outputCanvasName, [justResults])

Overlay the result onto the image frame and display it on the canvas.

Kind: instance method of CloudServerModel

Param Type Default Description
combinedResult Object

The result object combined with the original image frame. This is directly received from predict or predict_batch.

outputCanvasName string | HTMLCanvasElement | OffscreenCanvas

The canvas to draw the image onto. Either the canvas element or the ID of the canvas element.

[justResults] boolean false

Whether to show only the result overlay without the image frame.

cloudServerModel.emitPayload(payload, info)

Implements the payload sending logic for the Socket.IO transport.

Kind: instance method of CloudServerModel

Param Type Description
payload Blob

The preprocessed image data to send.

info string

The frame info

cloudServerModel.ensureTransportReady()

Ensures the Socket.IO connection is established before sending data.

Kind: instance method of CloudServerModel

cloudServerModel.getModelParameters() ⇒ Promise.<Object>

Returns the entire model parameters object.

Kind: instance method of CloudServerModel
Returns: Promise.<Object> -

A JSON object containing the model's current parameters.


cloudServerModel.setModelParameter(key, value) ⇒ Promise.<void>

Updates a single parameter in the model's parameters. Use this function to set arbitrary parameters in the model's configuration. Get a list of available parameters using model.modelInfo()

Kind: instance method of CloudServerModel

Param Type Description
key string

The name of the parameter to set.

value *

The new value for the parameter.

cloudServerModel.getModelParameter(key) ⇒ *

Retrieves the value of a single parameter from the model's configuration.

Kind: instance method of CloudServerModel
Returns: * -

The value of the specified parameter.

Param Type Description
key string

The name of the parameter to get.

cloudServerModel.modelInfo() ⇒ Object

Returns a read-only copy of the model's parameters.

Kind: instance method of CloudServerModel
Returns: Object -

A deep copy of the model parameters object.


cloudServerModel.labelDictionary() ⇒ Array.<string>

Returns the label dictionary for the model.

Kind: instance method of CloudServerModel
Returns: Array.<string> -

An array of label strings.


cloudServerModel.internalSocketReset()

Kind: instance method of CloudServerModel

cloudServerModel.resetTimeStats()

Resets the internal performance statistics dictionary.

Kind: instance method of CloudServerModel

cloudServerModel.getTimeStats() ⇒ string

Returns the internal performance statistics as a string.

Kind: instance method of CloudServerModel
Returns: string -

A string representation of the performance statistics.


cloudServerModel.cleanup()

Cleans up resources and closes the server connection. Does so by following a destructor-like pattern which is manually called by the user.
Call this whenever switching models or when the model instance is no longer needed!

Kind: instance method of CloudServerModel