The machine learning module allows for the custom creation of models based on robot sensor data. Once classes are created, trained, and tested, they can then be added to your program to use as event triggers or in other creative ways.
Before programming with a machine learning model, you must first create a model in the Machine Learning Lab. When the data classes are created and trained, you can then add the model to the program for use.
Conditional logic called when the selected class is detected by the program's machine learning model. The On Class Detected event needs to first be registered with a Register ML Event function before it can be called in code. See the examples below:
JavaScript:
async function onMLShake() {
if (getMLModelBypass()) return;
// code to execute on detected class
}
registerMLEvent(MLClass.Shake, onMLShake);
Python:
async def onMLShake():
if get_ml_model_bypass():
return
#code to execute on detected class
register_ml_event(MLClass['Shake'], onMLShake)
The Register ML Event function has the following parameters:
className {object} — A class from the selected ML model (e.g. MLClass.Rest)
callback {string} — Callback to execute when the class is detected
Checks if the selected data class is detected and returns 'true' if it is and 'false' if it isn't. This allows for execution of conditional logic based on the detection status.
Javascript: getCurrentMLClass()
Python: get_current_ml_class()
Returns:
className {object} - The class label from your machine learning model (for example, shake or still)
Bypasses the machine learning model detection when turned on. This can be used to conditionally disable the model if it's not needed for a part of a program. When bypass is enabled, machine learning event callbacks return early without running their code.
To enable and disable model bypass with JavaScript, use:
On: setMLModelBypass(true);
Off: setMLModelBypass(false);
To enable and disable model bypass with Python, use:
On: set_ml_model_bypass(True)
Off: set_ml_model_bypass(False)
Parameters:
enable {boolean} - A boolean value to bypass the machine learning model (true = bypass on, false = bypass off)
Sets the detection threshold (1–100%) for the selected class. A higher threshold requires more confidence from the machine learning model to detect the class. A lower threshold means the model will detect the class with less confidence, which can result in detection errors. The default value (if it isn't modified in the model itself) is 71%.
Use the following examples to set the detection threshold to a confidence of 71% for the selected model:
JavaScript: setMLDetectionThreshold(MLClass.Bright, 71);
Python: set_ml_detection_threshold(MLClass.Bright, 71)
Parameters:
label {object} - The class label from your machine learning model (for example, shake or still)
value {number} - Detection threshold confidence value as a percentage 1–100 (e.g. 71 for 71%)
Returns the latest available confidence value (0-100%) that the machine learning model detects for the selected class.
JavaScript: getMLConfidence(MLClass.Bright);
Python: get_ml_confidence(MLClass.Bright)
Parameters:
label {object} - The class label from your machine learning model (for example, shake or still)
Return:
{number} - Latest model confidence in range 0–100% (0 if no inference yet or unknown label)