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.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.
[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.

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
[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 Whether to bypass preprocessing. Used to send Blob data directly to the socket without any preprocessing.

Example

If callback is provided:
Onresult handler will invoke the callback directly when the result arrives.
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 An async iterable data source.
[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 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.cleanup()

Cleanup the model and release all resources. Internally, it does the following: - Sets the poison flag to stop further inferences - Disconnects the socket - Clears the async queues - Nullifies references - Resets internal states and flags Call this method when you are done using the model to free up resources

Kind: instance method of CloudServerModel

cloudServerModel.resetTimeStats()

Resets the stats dict to an empty dict

Kind: instance method of CloudServerModel

cloudServerModel.getTimeStats()

Returns the stats dict to the user

Kind: instance method of CloudServerModel