MATLAB is a powerful tool used by data scientists and artificial intelligence researchers, among others. Although there is a learning curve, it's worth diving in, because once you understand it well enough to get things done, you'll likely find it efficient and elegant. Their libraries and tools are hard to give up once you've had the chance to experience them. As a bonus, they offer support packages to generate and run code on a variety of platforms, including Raspberry Pi!
INSTALLStarting out, you'll need a PC with Matlab installed. I'm using version 2020a and I'll be doing the setup via my Macbook Pro running Catalina.
Install the MATLAB Raspberry Pi support packageFirst, install the support package in MATLAB.
- Open Matlab and on the home tab, select 'Add-Ons" > "Get Add-Ons."
- Search for "MATLAB Raspberry Pi support package" and select "Install."
- Accept the agreement.
- On the final screen of setup, choose "Setup now." That will open the board setup wizard.
Note: If you already closed it the installation wizard, you can find the image setup by selecting "Add-Ons" > "Manage Add-Ons" and then selecting the gear button (setup) next to the Raspberry Pi support package.
MATLAB interfaces with Raspberry Pi by way of a server running on the Raspberry Pi hardware. This is a custom server that interprets commands send by MATLAB.
The board setup wizard will guide you through downloading MATLAB's raspbian image that includes the server. Then it will walk you through configuring it to work with whichever networking option you choose.
- On the first screen, select your type of Raspberry Pi. Click "next."
- Select "Setup hardware with MathWorks Raspian image."
- Download the deep learning image.
- Find your downloaded image and unzip it.
- Validate the image by using the "validate" button.
Burn the image
Matlab was not able to burn the image on Mac, so I ended up simply using BalenaEtcher to burn the image instead.
On the "Write Firmware" page, MATLAB checks the SD card to make sure it has been burned. I reinserted the SD card so that the burned image was visible to the program. That way I was able to select "Next" and skip to the next step.
There are four different connection options to connect your Pi to MatLab:
1. Connect to LAN or home network
2. Connect to wireless network
3. Connect directly to host computer
4. Manually enter network settings
- In Matlab, choose "Connect to wireless network." Fill in the SSID and password that both the board and PC are connected to (note - my Raspi 3 B+ is not able to connect to 2.4GHz wifi networks. Apparently it's a known issue)
- Select "Automatically get IP address." Click "next."
Normally, MatLab sets the network connection up on your board automatically, but since we had to skip the previous imaging step, we'll have to set up the WIFI on the Pi manually. To set up WIFI on the Pi, you may wish to connect a keyboard, mouse and HDMI monitor, although you can also do it via SSH.
- Remove the SD card from your computer and insert it in your Raspberry Pi.
- Connect the Pi to a power supply of at least 5V 2A via the micro-USB.
- Connect the camera to the board by inserting the MIPI-CSI cable with blue side facing the pi's ethernet connectors, and the other blue side facing away from the camera lens.
- Click "next" in MATLAB to verify your connection.
- You should see the screen that says "Test Hardware Connection successful". If not, double check that your pi and PC running laptop are connected to the same network and try running "test connection" again.
You can now connect your Raspberry Pi to Matlab using the Matlab console by simply typing:
mypi = raspi
That brings up the board's information, and you will also see it in Workspace. The board's info will contain a list of the available digital pins, leds, SPI channels, I2C buses and their speed.
Try turning the pi's yellow 'ACT' LED on and off by typing the following:
writeLED(mypi,'led0',1)
writeLED(mypi,'led0',0)
Connect to the camera
You can connect to the camera by typing:
cam=cameraboard(mypi, 'Resolution', '1280x720')
The resolution is optional, and can also be set to a different resolution. Default is 640x480. The available resolutions are listed in the output when you set up your camera.
You can find more info on working with the camera here.
RUN THE FACE DETECTION PROGRAMFirst type
clear
to clear the connection to the board that we created previously, since the face detection code creates a new connection.
Make sure that your Pi camera is positioned so that it can see your face (or anyone's face).
Create a new script in Matlab, and copy and paste the included code into the script. Name it "facedetection.m." Make sure your current directory is the one with facedetection.m.
With facedetection.m open, select "run" on the editor tab (or type "facedetection" in the console). Once again, you will see a cascade of pixel values, and the "Figure 1" window will open and begin detecting faces. It will run until you select "pause" and then "end debugging" to quit.
As you can see, there are only a few lines of code needed to run face detection. The code is running the cascade object detector, which uses the Viola-Jones algorithm to detect people’s faces, noses, eyes, mouth, or upper body.
fD = vision.CascadeObjectDetector
creates a detector to detect objects using the Viola-Jones algorithm.
I found the Matlab face detector to be both fast and accurate on Raspberry Pi 3 B+. It's also possible to train the Cascade Object Detector to recognize other objects, like stop signs or cars.
Comments