Tag Archives: opencv

Machine Vision with Raspberry Pi using TensorFlow and OpenCV4

artificial vision with raspberry pi tensorflow and opencv

Do you want to implement a CCTV camera that can identify several different elements? Or maybe optimize your robot and give it the ability to detect objects?

The Raspberry Pi 4 offers enough performance to perform machine learning. In this tutorial you will see the steps to get TensorFlow working on your Raspberry Pi 4, as well as a demonstration of detecting objects with a PI Camera or USB Webcam.

Required Hardware/software

  • Raspberry Pi 4.
  • 3000mA power supply with its USB-C cable.
  • micro-SD card (minimum 32 GB).
  • Picamera (or USB webcam).

What is TensorFlow ?

TensorFlow is an open source machine learning framework for solving complex mathematical problems. It allows researchers to develop experimental learning architectures and turn them into software. TensorFlow was developed by Google Brain team in 2011 and made open source in 2015. Since then it has gone through more than 21000 changes and upgraded to version 1.0 in February 2017. Currently version 2.8.0 is available on the TensorFlow website.

This library notably makes it possible to train and run neural networks for the classification of handwritten digits, image recognition, models for machine translation, and natural language processing. An application’s code is written in Python but executed in high-performance C++. Here, the raspberry and its camera will be able to detect a hundred different objects! In addition, good results are obtained at night with an IR camera equipped with LEDs.

On the software side, the micro-SD has been flashed with the latest version of Raspbian Buster downloadable from the official website. Python3 is installed there by default, to launch a program from the command line, you will have to type in a terminal:

$ python3 my_program.py

Here, the Object_detection_picamera.py program will need several elements to work:

  • The mscoco_label_map.pbtext file corresponding to the list of 100 detectable objects, it is located in the data folder.
  • The “ssdlite_mobilenet_v2_coco_2018_05_09” folder where the training model files are stored.
  • The utils folder (utilities) containing the classification modules for the various objects.

Installing TensorFlow

Start by updating the Raspberry Pi:

sudo apt-get update
sudo apt-get dist-upgrade

Next, install TensorFlow with the pip3 command.

sudo pip3 install tensorflow

TensorFlow needs a library named Atlas, type the following command:

sudo apt-get install libatlas-base-dev

Finally, it will be necessary to install the dependencies necessary for the detection of objects.

sudo pip3 install pillow lxml jupyter matplotlib cython
sudo apt-get install python-tk

That’s it for TensorFlow. Let’s move on to OpenCV.

Installing OpenCV

TensorFlow’s object detection examples typically use matplotlib to display images, but OpenCV is preferred because it’s easier to use and less error-prone. The object detection scripts in the GitHub repository in this guide use OpenCV. To make OpenCV work on the Raspberry Pi, many dependencies need to be installed via apt-get.

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev 
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev 
sudo apt-get install libxvidcore-dev libx264-dev 
sudo apt-get install qt4-dev-tools libatlas-base-dev
sudo pip3 install opencv-python

If all went well, OpenCV is installed on your Raspberry.

You can check if the installation is successful and the installed version. Open a terminal and launch the Python3 interpreter.

The TensorFlow object detection API uses Protobuf, a package that implements Google’s Protocol Buffer data format. Before, you had to compile the Protobuf source code (a bit tedious), but now the installation is very simple, thanks to the multiple contributions of the community.

sudo apt-get install protobuf-compiler

In order to check the version installed, run the command:

protoc --version

You should have in response libprotoc v3.20.0 (latest version.)
Create a TensorFlow directory and navigate to it (/home/pi/tensorflow).

mkdir tensorflow
cd tensorflow

Download the TensorFlow repository from GitHub by typing:

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

Next, you need to modify the PYTHONPATH environment variable to point to certain directories in the TensorFlow repository you just downloaded. PYTHONPATH needs to be set each time a terminal is opened, so the .bashrc file needs to be edited.

sudo nano ~/.bashrc

Go to the end of the file, and add this line:

export PYTHONPATH=$PYTHONPATH:/home/pi/tensorflow/models/research:/home/pi/tensorflow/models/research/slim

Now we need to use protoc to compile the Protocol Buffer (.proto) files used by the Object Detection API. The .proto files are in /research/object_detection/protos, but we need to run the command from the /research directory.

cd /home/pi/tensorflow1/models/research
protoc object_detection/protos/*.proto --python_out=.

Next, move into the object_detection directory:

cd /home/pi/tensorflow/models/research/object_detection

Now download the SSD_Lite model. Google teams have developed a series of pre-trained object detection files, with varying levels of speed and accuracy. The Raspberry Pi has a relatively weak processor for the recognition application, so a model should be used that takes less processing power. Here we will use SSDLite-MobileNet, which remains well suited for our Raspberry Pi. Download the file and unzip it into the object_detection directory.

wget http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz
tar -xzvf ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz

Finally, copy/paste the python program from this Github link in a file named Object_detection_picamera.py

If you want to port the application to other Raspberry Pi with OpenCV and TensorFlow installed, simply download the .ZIP of the necessary files available on GitHub.

Your application is now ready to work!

Conclusion

Once your application is operational, all possibilities are open. For an autonomous application, based on solar panels, it would suffice to perform a calculation of the power consumed by the entire installation, and to determine the appropriate type of battery.

Still at the battery level, the Lion-i technology seems well suited, because of its small size and its autonomy, the price is higher than a lead battery, but offers good results in an environment subject to cold or heat. heat.