Skip to content

Image File

Classifies one photo and acts on the label only when the model is confident enough, a common pattern in robotics. Put a photo.jpg next to the model files.

Code

examples/image_file.py
"""Classify one image file and act only when the model is confident enough."""
from teachable_machine_lite import TeachableMachineLite

model = TeachableMachineLite(model_path="model.tflite", labels_file_path="labels.txt")

result = model.classify_image("photo.jpg", top_k=2)

if result["confidence"] > 80:
    print("Detected:", result["label"])
else:
    print("Not sure. Top guesses:", result["predictions"])

How it works

  • top_k=2 puts the two most likely classes in result["predictions"], so it needs a model with at least two classes.
  • confidence is a percentage, so 80 means 80%.