GithubHelp home page GithubHelp logo

imageclassification's Introduction

TensorFlow Lite Python image classification example with Raspberry Pi.

This example uses TensorFlow Lite with Python on a Raspberry Pi to perform real-time image classification using images streamed from the camera.

At the end of this page, there are extra steps to accelerate the example using the Coral USB Accelerator, which increases the inference speed by ~10x.

Set up your hardware

Before you begin, you need to set up your Raspberry Pi with Raspberry Pi OS (preferably updated to Buster).

You also need to connect and configure the Pi Camera if you use the Pi Camera. This code also works with USB camera connect to the Raspberry Pi.

And to see the results from the camera, you need a monitor connected to the Raspberry Pi. It's okay if you're using SSH to access the Pi shell (you don't need to use a keyboard connected to the Pi)—you only need a monitor attached to the Pi to see the camera stream.

Install the TensorFlow Lite runtime

In this project, all you need from the TensorFlow Lite API is the Interpreter class. So instead of installing the large tensorflow package, we're using the much smaller tflite_runtime package.

To install this on your Raspberry Pi, follow the instructions in the Python quickstart.

You can install the TFLite runtime using this script.

sh setup.sh

Download the example files

First, clone this Git repo onto your Raspberry Pi like this:

git clone https://github.com/tensorflow/examples --depth 1

Then use our script to install a couple Python packages, and download the TFLite model:

cd examples/lite/examples/image_classification/raspberry_pi

# Run this script to install the required dependencies and download the TFLite models.
sh setup.sh

Run the example

python3 classify.py
  • You can optionally specify the model parameter to set the TensorFlow Lite model to be used:
    • The default value is efficientnet_lite0.tflite
    • TensorFlow Lite image classification models with metadatafrom (including models from TensorFlow Hub or models trained with TensorFlow Lite Model Maker are supported.)
  • You can optionally specify the maxResults parameter to limit the list of classification results:
    • Supported value: A positive integer.
    • Default value: 3.
  • Example usage:
python3 classify.py \
  --model efficientnet_lite0.tflite \
  --maxResults 5

Speed up the inferencing time (optional)

If you want to significantly speed up the inference time, you can attach an ML accelerator such as the Coral USB Accelerator—a USB accessory that adds the Edge TPU ML accelerator to any Linux-based system.

If you have a Coral USB Accelerator, you can run the sample with it enabled:

  1. First, be sure you have completed the USB Accelerator setup instructions.

  2. Run the image classification script using the Edge TPU TFLite model and enable the Edge TPU option.

python3 classify.py \
  --model efficientnet_lite0_edgetpu.tflite \
  --enableEdgeTPU

You should see significantly faster inference speeds.

For more information about creating and running TensorFlow Lite models with Coral devices, read TensorFlow models on the Edge TPU.

For more information about executing inferences with TensorFlow Lite, read TensorFlow Lite inference.

imageclassification's People

Contributors

williamlim16 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.