vision_classify · Classifier models

Classifies images with a model taught in the classifier screen.

Class: TeachableMachine CustomClassifier

Does not use TensorFlow. Image models run on the TFLite runtime (LiteRT / tflite_runtime); hand, face and pose models run on MediaPipe.

class openpibo.vision_classify.TeachableMachine[source]

Bases: object

Functions: load() predict()

Uses the Teachable Machine feature with piBo’s camera.

  • Uses the Standard image model of an Image project.

  • Applies a model trained in Teachable Machine for inference.

  • Download the trained model in Tensorflow Lite format.

example:

from openpibo.vision_classify import TeachableMachine

tm = TeachableMachine()
# 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
load(model_path, label_path)[source]

Loads a Tflite model. Both (floating point/quantized) are supported

example:

tm.load('model_unquant.tflite', 'labels.txt')
Parameters:
  • model_path (str) – Teachable Machine model file

  • label_path (str) – Teachable Machine label file

predict(img)[source]

Runs inference with the Tflite model.

example:

cm = Camera()
img = cm.read()
tm.predict(img)
Parameters:

img (numpy.ndarray) – Image object

Returns:

Class name with the highest probability, result (raw data)

class openpibo.vision_classify.CustomClassifier[source]

Bases: object

Functions: load() predict() draw()

Uses a model trained in piBo’s classifier screen.

  • You can teach four kinds: image · hand · face · pose.

  • Saving in the classifier screen creates the folder /home/pi/mymodel/<model name>/. Pass that folder name (or path) to load.

  • Keras models (model.keras) from the old classifier can no longer be used. Train and save again.

example:

from openpibo.vision_classify import CustomClassifier

cf = CustomClassifier()
# 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
load(model_path)[source]

Loads a model saved in the classifier screen. The class names are stored in the model folder.

example:

cf.load('/home/pi/mymodel/과일')
cf.load('과일')                   # /home/pi/mymodel/과일 과 같습니다
Parameters:

model_path (str) – Model folder (/home/pi/mymodel/<model name>) or model name

predict(img, threshold=0.0, draw=False)[source]

Classifies with the loaded model.

example:

cm = Camera()
img = cm.read()
name, probs = cf.predict(img)
print(name)                           # 사과
print(dict(zip(cf.classes, probs)))   # {'사과': 0.97, '바나나': 0.03}
Parameters:
  • img (numpy.ndarray) – Image object (camera image, BGR)

  • threshold (float) – If the highest probability is lower than this value, None is returned instead of the name (0~1)

  • draw (bool) – If True, draws directly on img while classifying (same as draw()). Points and skeleton for hand and pose, points for face, and the class name and probability for all models

Returns:

(class name, per-class probabilities) Hand, face and pose models return (None, None) if no hand, face or body is visible in the image.

draw(img, label=True)[source]

Draws what the previous predict() saw onto the image. Modifies img in place and returns it.

  • Hand — 21 points and skeleton per hand · Pose — 33 points and skeleton · Face — face points

  • If label=True, also writes the class name and probability (image models draw only this)

  • Draws nothing if no hand, face or body was visible

example:

img = cm.read()
name, probs = cf.predict(img)
cf.draw(img)
cm.imwrite('/home/pi/result.jpg', img)

name, probs = cf.predict(img, draw=True)   # 분류하면서 바로 그리기
Parameters:
  • img (numpy.ndarray) – Image passed to predict() (must be the same size so the points land in the right place)

  • label (bool) – Whether to write the class name and probability

Returns:

img

close()[source]

Unloads the loaded model. (Called automatically when you load another model)