Introduction: Image Recognition With TensorFlow on Raspberry Pi
Google TensorFlow is an Open-Source software Library for Numerical Computation using data flow graphs. It is used by Google on its various fields of Machine Learning and Deep Learning Technologies. TensorFlow was originally developed by Google Brain Team and it is published on the public domain like GitHub.
For more tutorials visit our blog. Get Raspberry Pi from FactoryForward - Approved Reseller in India.
Read this tutorial on our blog here.
Support My Blog Tedizen.
Step 1: Machine Learning
Machine Learning and Deep Learning will come under Artificial Intelligence (AI). A Machine Learning will observe and analyze the available data and improves it results over time.
Example: YouTube Recommended videos feature. It shows related videos that you viewed before. The prediction is limited to text-based results only. But deep learning can go deeper than this.
Step 2: Deep Learning
The deep learning is almost similar to that, but it makes more accurate decision on its own by collecting various information of an object. It has many layers of analysis and takes a decision according to it. To fasten the process, it uses Neural Network and provides us more exact result that we needed (means better prediction than ML). Something like how a human brain thinks and makes decisions.
Example: Object detection. It detects what is available in an image. Something similar that you can differentiate an Arduino and Raspberry Pi by its appearance, size and Colors.
It is a wide topic and has various applications.
Step 3: Pre-Requisites
The TensorFlow announced official support for Raspberry Pi, from Version 1.9 it will support Raspberry Pi using pip package installation. We will see how to install it on our Raspberry Pi in this tutorial.
- Python 3.4 (recommended)
- Raspberry Pi
- Power Supply
- Raspbian 9 (Stretch)
Step 4: Update Your Raspberry Pi and Its Packages
Step 1: Update Your Raspberry Pi and its packages.
sudo apt-get update
sudo apt-get upgrade
Step 2: Test that you have latest python version, using this command.
python3 –-version
It is recommended to have at least Python 3.4.
Step 3: We need to install libatlas library (ATLAS - Automatically Tuned Linear Algebra Software). Because TensorFlow uses numpy. So, install it using the following command
sudo apt install libatlas-base-dev
Step 4: Install TensorFlow using Pip3 install command.
pip3 install tensorflow
Now TensorFlow is installed.
Step 5: Predicting an Image Using Imagenet Model Example:
TensorFlow has published a model to predict images. You need to download the model first then run it.
Step 1: Run the following command to download the models. You might need to have git installed.
git clone https://github.com/tensorflow/models.git
Step 2: Navigate to imagenet example.
cd models/tutorials/image/imagenet
Pro Tip: On the new Raspbian Stretch, you can find the ‘classify_image.py’ file manually and then ‘Right Click’ on it. Choose ‘Copy Path(s)’. Then paste it in terminal after the ‘cd’ and press enter. By this way you can navigate faster without any errors (in case of spelling mistake or the file name is changed in new updates).
I used ‘Copy Path(s)’ method so it will include the exact path on the image (/home/pi).
Step 3: Run the example using this command. It will take about 30 seconds to show the predicted result.
python3 classify_image.py
Step 6: Custom Image Prediction
You can also download an image from the internet or use your own image shot on your camera for predictions. For better results use less memory images.
To use custom images, use the following way. I have the image file at the location ‘/home/pi/Downloads/TensorImageTest1.jpg’. Just replace this with your file location and name. Use ‘Copy Path(s)’ for easier navigation.
python3 classify_image.py --image_file=/home/pi/Downloads/TensorImageTest1.jpg
You can try out other examples too. But you need to install necessary packages before execution. We will cover some interesting TensorFlow topics in the upcoming tutorials.
Check out our FactoryForward blog for more tutorials on Arduino, Raspberry Pi, microBit,etc.
Shop at FactoryForward - Approved Raspberry Pi Reseller in India .