Image ClassificationΒΆ

When you see a rabbit, you will directly say its name. The computer will tell you that 91% of it may be a rabbit, 4% of it may be a cat, 3% of it may be a dog, and 2% of it may be something else.

We use image classification to reveal the cognitive model of computer vision. Image classification is a basic task. A trained model is used to identify images representing various objects, and then the images are assigned to specific tags to classify the images.


Run the Code

cd /home/pi/pan-tilt-hat/examples
sudo python3

View the Image

After the code runs, the terminal will display the following prompt:

No desktop !
* Serving Flask app "vilib.vilib" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on (Press CTRL+C to quit)

Then you can enter http://<your IP>:9000/mjpg in the browser to view the video screen. such as:



from vilib import Vilib

def main():

if __name__ == "__main__":

How it works?

The use of this feature is very simple. You only need to pay attention to the following three lines of code:

  • Vilib.image_classify_set_model(path) : Load the trained model file.

  • Vilib.image_classify_set_labels(path) : Load the corresponding label file.

  • Vilib.image_classify_switch(True) : Start the image classifier.

Here, we directly use Tensorflow pre-trained model, which is an image classification model that includes thousands of objects. You can open the label file (/home/pi/pan-tilt-hat/models/labels_mobilenet_quant_v1_224.txt) to see which objects are included.

In addition to the built-in models in this article, you can also download the pre-trained image classification model on TensorFlow Hub. It should be noted that these models may not be suitable for your project, please use them as appropriate.

If you want to try to create your own model. We strongly recommend that you use Teachable Machine. It is a web-based tool that allows everyone to create machine learning models quickly, easily, and accessible. Please click Get start on the webpage to start training your model.


Raspberry Pi may not be able to use Teachable Machine smoothly. You will need to prepare a PC or laptop equipped with a camera.

Model Training

  1. Open Teachable Machine, you will see an obvious Get Start on the web page, click on it.

  2. Select Image Project (Audio Project and Pose Project are not applicable here). You will be prompted to choose Standard image model or Embedded image model. The former has a higher accuracy rate and the latter has a faster speed. We recommend choosing the first one.

    ../_images/tm2.png ../_images/tm3.png
  3. Train the model. Teachable Machine provides a detailed video step-by-step explanation, please see:


    The video after 0:55 is the content of the other two projects and is not applicable here.


    The export settings applicable to this project are shown in the figure:

  4. Unzip the downloaded zip file, you will be able to see the model file and label file, their formats are .tflite and .txt respectively. Use Filezilla Software to copy them to the /home/pi/pan-tilt-hat/models/ directory of the Raspberry Pi.

  5. Modify the two lines of the sample code in this article, and change them to your model and label.

  6. Re-run the example. It will recognize the objects in your training model.