Skip to content

Webcam Classifier

Classifies frames from a webcam and shows the result on the video, using classify_and_show.

Code

examples/webcam.py
"""Classify webcam frames and show the result on the video. Press Esc to quit."""
import cv2 as cv

from teachable_machine_lite import TeachableMachineLite

cap = cv.VideoCapture(0)
model = TeachableMachineLite(model_path="model.tflite", labels_file_path="labels.txt")

while True:
    ok, frame = cap.read()
    if not ok:
        break

    result, annotated = model.classify_and_show(frame)
    print("result:", result)

    cv.imshow("Camera", annotated)
    if cv.waitKey(1) == 27:  # Esc
        break

cap.release()
cv.destroyAllWindows()

How it works

  • The model is loaded once, before the loop.
  • Each frame goes straight to the model, without being saved to a file.
  • classify_and_show returns the result and an annotated copy of the frame.