The launch of the Raspberry Pi 4 could be the right time for new developers to enter the space and use the technology. This tiny computer can be used for a variety of functions, but our focus today will be on using the Pi 4 for image processing in a small package and low power.
The Pi 4 can be used for a vast array of image recognition tasks, and the creators of the device seem to have recognized this. The computer features a dedicated interface for the two different types of cameras that are sold separately alongside the Pi.
One is a traditional camera module that comes with the Pi and can be attached using the Camera Serial Interface port on the board. In addition to this, there is also an infrared camera module known as the NOIR camera. They can be used for daylight and low-light surveillance respectively.
It is recommended to go with a normal camera for this project, as it is vastly superior in image quality to the infrared one. The parts list for this build will be:
- Raspberry Pi 4
- Raspberry Pi Camera
- Raspberry Pi Case
- MicroSD card with NOOBS pre-loaded
- 5.1V 3A USB Type-C Power Supply
- Display and peripherals
Assemble the Pi in its case and connect the peripherals, along with the monitor. Connect the camera to the computer and boot it up for the first time. Go through the NOOBS installer and install the Raspbian OS. The NOOBS installer makes this process easy and seamless.
After setting up the Pi’s OS, it should then be connected to the Internet and set up with SSH so as to allow for remote access. The SSH setup allows the Pi to act as a remote device and be controlled by another machine. This removes the need for keyboard and mouse input.
Launch the configuration option from the Preferences menu, then navigate to the interfaces tab. Here, you can click the Enabled button next to SSH to enable the service.
To get this project up and running, it is first required to install dependencies. Many of them can be installed from the command line, with a few including:
- TensorFlow Lite: A stripped-down and lightweight version of TensorFlow made to run on edge and embedded devices
- CMake: A set of tools used to test, build and package software
- Codecs: Various codecs, such as LibAV and LibXVID need to be installed to understand camera output
After installing the dependencies, we can proceed to install OpenCV libraries for Python. Depending on the version of Python that is being used, the version of OpenCV to be installed varies.
Type the following commands in a terminal and agree to the prompts.
sudo apt-get install python2.7-dev python3-dev
cd ~ && wget -O opencv.zip https://github.com/opencv/opencv/archive/3.3.0.zip && unzip opencv.zip
After downloading and unzipping OpenCV, we can now compile and install the software. Make sure that the camera is added using the sudo raspi-config command, wherein the Camera option needs to be enabled. Now, the computer is ready for running models.
Deploying The Model
After training the model on another computer or the cloud, it has to be adapted for use on TensorFlow Lite. This is to ensure that the model will run on the Raspberry Pi 4. Before accessing the Pi, we must first install the rpi-vision repository on the primary computer from GitHub provided by Leigh Johnson.
Create a virtual session on the non-Pi computer, and then install the package for the rpi-vision repository. This repo will allow us to seamlessly integrate functions such as image scans.
$ pip install virtualenv; virtualenv -p $(which python3) .venv && source .venv/bin/activate && pip install -e .
The next step is to SSH into the Pi. This will allow us to run commands. Using another computer on the same network, type the following command into your terminal.
$ ssh raspberrypi.local
Then, access the virtual machine on the other computer by typing,
pi@raspberryi:~ $ cd ~/rpi-vision && . .venv/bin/activate
After this, the model’s agent process can be started. After initializing the model, it will begin conducting inference on the feed coming from the camera of the Raspberry Pi.
Possible Use Cases
The computing power of the Raspberry Pi 4 is higher compared to previous generations. This means that it can perform inference fairly quickly. It can be used for various types of applications. These include a rock-paper-scissors detection machine, home surveillance through motion detection, object detection for authorized entry (pet vs. animal) or even to give vision to a robot.
Owing to the low computing power required for many computer vision algorithm, it is possible to give a Pi-powered robot sight through CV. By implementing a common shape detection algorithm, it is possible to create a robot that avoids walls and turns corners.
The potential of the Pi 4 for such applications is endless. So deploy your first image processing algorithm to your Pi today.