Image Recognition using TensorFlow for Raspberry Pi
TensorFlow now Officially Supports Raspberry Pi (from TensorFlow V1.9). 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. Using TensorFlow we can develop projects like Image Recognition, Object Detection, Automated Vehicles with Traffic Signal Detection and Facial Recognition Projects.
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.
The deep learning is almost similar to that, but it makes the 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.
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.
Pre-Requisites:
- Python 3.4 (recommended)
- Raspberry Pi
- Power Supply
- Raspbian 9 (Stretch)
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.
Image Recognition using TensorFlow Imagenet Model Example:
TensorFlow has published an example 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 the terminal after the ‘cd’ and press enter. By this way, you can navigate faster without any errors (in case of any 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
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 fewer memory sized 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.
Comment (1)
I have done all the steps. But I could not find the classify_image.py file. I think the file name has been changed.
Now please let me know what should I do after that to complete the project.