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 |
'model.tflite'
|
labels_file_path
|
str
|
Path to the |
'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
|
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
|
1
|
color_order
|
str
|
Channel order of a NumPy array input, |
'BGR'
|
Returns:
| Type | Description |
|---|---|
dict
|
The classification results, with these keys:
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 |
required |
classification_result
|
dict
|
Pre-computed classification result. If not
provided, the image is classified with |
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'
|
Returns:
| Type | Description |
|---|---|
ndarray or Image
|
The image with the prediction results drawn on it: a NumPy
array in BGR format if |
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 |
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 |
1
|
color_order
|
str
|
Channel order of a NumPy array input, |
'BGR'
|
Returns:
| Type | Description |
|---|---|
tuple
|
|