Skip to content

API Reference

Everything is in one class, imported from teachable_machine_lite:

from teachable_machine_lite import TeachableMachineLite

The module also has teachable_machine_lite.RUNTIME, the name of the TensorFlow Lite runtime in use: "ai-edge-litert", "tflite-runtime" or "tensorflow".

TeachableMachineLite

TeachableMachineLite(model_path: str = 'model.tflite', labels_file_path: str = 'labels.txt', num_threads: int = None)

Classify images with a TensorFlow Lite model exported from Google's Teachable Machine.

Load an exported Teachable Machine TensorFlow Lite model and its labels.

Both the quantized and the floating point TensorFlow Lite exports are supported.

Parameters:

Name Type Description Default
model_path str

Path to the .tflite model file. Default is 'model.tflite'.

'model.tflite'
labels_file_path str

Path to the labels.txt file exported with the model. Default is 'labels.txt'.

'labels.txt'
num_threads int

Number of CPU threads the interpreter may use. Setting it to the number of cores (4 on Raspberry Pi 4/5) speeds up inference. Default is None (the runtime's default).

None

Raises:

Type Description
FileNotFoundError

If the model or labels file is not found.

RuntimeError

If the model cannot be loaded by the TensorFlow Lite runtime.

classify_image

classify_image(image_path, calc_time: bool = False, top_k: int = 1, color_order: str = 'BGR')

Classify an image and return the classification results.

Parameters:

Name Type Description Default
image_path (str, PathLike, Image or ndarray)

The image to classify: a file path, a PIL image, or a NumPy array such as an OpenCV frame or a Picamera2 capture_array() result. Passing frames directly avoids writing every frame to disk.

required
calc_time bool

If True, measures and returns the time taken for the classification. Default is False.

False
top_k int

How many of the most likely classes to include in "predictions". Default is 1.

1
color_order str

Channel order of a NumPy array input, "BGR" (OpenCV, Picamera2 "RGB888"/"XBGR8888") or "RGB". Ignored for file paths and PIL images. Default is "BGR".

'BGR'

Returns:

Type Description
dict

The classification results, with these keys:

  • "id" (int): index of the predicted label.
  • "label" (str): the predicted label.
  • "time" (float or None): classification time in seconds (3 decimals), or None if calc_time is False.
  • "confidence" (float): confidence of the prediction as a percentage.
  • "highest_class_id" (int): same as "id".
  • "highest_class_prob" (float): same as "confidence".
  • "predictions" (list of dict): the top_k most likely classes, best first, each with "id", "label" and "confidence".

Raises:

Type Description
ValueError

If top_k is out of range, color_order is invalid, or a NumPy array has an unsupported shape.

TypeError

If a NumPy array is not of dtype uint8.

show_prediction_on_image

show_prediction_on_image(image_path, classification_result=None, calc_time: bool = False, convert_to_bgr: bool = True, color_order: str = 'BGR')

Draw the prediction results on the input image and return the modified image.

Parameters:

Name Type Description Default
image_path (str, PathLike, Image or ndarray)

The input image, as a file path, a PIL image, or a NumPy array (see classify_image).

required
classification_result dict

Pre-computed classification result. If not provided, the image is classified with classify_image.

None
calc_time bool

Whether to calculate the time taken to classify the image. Default is False.

False
convert_to_bgr bool

Whether to convert the output image from RGB to BGR format, for use with OpenCV. Default is True.

True
color_order str

Channel order of a NumPy array input, "BGR" or "RGB". Default is "BGR".

'BGR'

Returns:

Type Description
ndarray or Image

The image with the prediction results drawn on it: a NumPy array in BGR format if convert_to_bgr is True, otherwise a PIL image in RGB format.

classify_and_show

classify_and_show(image_path, convert_to_bgr: bool = True, top_k: int = 1, color_order: str = 'BGR')

Classify an image and show the prediction results on the image.

Parameters:

Name Type Description Default
image_path (str, PathLike, Image or ndarray)

The input image, as a file path, a PIL image, or a NumPy array (see classify_image).

required
convert_to_bgr bool

Whether to convert the image to BGR format for OpenCV. If False, the image is returned in RGB format. Default is True.

True
top_k int

How many of the most likely classes to include in the result's "predictions". Default is 1.

1
color_order str

Channel order of a NumPy array input, "BGR" or "RGB". Default is "BGR".

'BGR'

Returns:

Type Description
tuple

(classification_result, image_with_prediction), where classification_result is the dict returned by classify_image and image_with_prediction is the image with the results drawn on it (see show_prediction_on_image).