Image recognition
Get into machine learning and train your Pi to recognise and classify other Pis, without having to write a line of code.
YOU’LL NEED
• RaspberryPi3,4or400 • 8GB(orlarger)microSDcard • RaspberryPiCamera
oraUSBwebcam • Powersupplyforyour
RaspberryPi • Smartphonefortakingphotos
• SelectionofPisorother objectsforclassification
WE’RE GOING TO TRAIN our Raspberry Pi to identify other Raspberry Pis (or other objects) with machine learning (ML). Why is this important? An example of an industrial application for this type of ML is identifying defects in circuit boards. As circuit boards exit the assembly line, a machine can be trained to identify a defective circuit board for troubleshooting by a human.
Other neat applications of machine learning and artificial intelligence could include facial recognition and face mask identification. For these types of projects, you can store the training images locally on the Raspberry Pi, however, the training process may take longer if performed on the Pi. So, for this tutorial, we’ll use a web platform called Edge Impulse, one advantage of which is the ease of uploading training images. This can be done from a smartphone, without having to involve an app.
We’ll use BalenaCloudOS instead of the standard Raspberry Pi OS, since the folks at Balena have pre-built an API call to Edge Impulse. Some facial recognition and face mask identification tutorials also require tedious command line package installs and Python code. This project eliminates all terminal commands and uses an intuitive GUI instead.
TRAINING ON THE EDGE
Go to https://edgeimpulse.com and create a free account (or login), from a browser window on your desktop or laptop. Select Data Acquisition from the menu bar on the left. You can either choose to upload photos from your desktop or scan a QR code with your smartphone and take photos. In this tutorial, we’ll opt for taking photos with our smartphone. Select Show QR code and a QR code should pop up on your screen. Scan it and select Open in browser and you’ll be taken to a data collection site. You won’t need to download an app to collect images.
Accept permissions on your smartphone and tap Collecting images? in your phone’s browser screen. Tap Label and enter a tag for the object that you take photos of. Take 30-50 photos at various angles. Some photos will be used for training and other photos will be used for testing the model. Edge Impulse automatically splits photos between training and testing. Repeat the process of Entering a label for the next object and taking 30-50 photos per object until you have at least three objects. We recommend three to five identified objects for your initial model. You’ll have a chance to re-train the model with more photos and/or objects later on.
From the Data Acquisition tab in the Edge Impulse browser window, you should now see the total number of photos taken (or uploaded) and the number of labels (type of objects) you have classified. You can click any of the collected data samples to view the uploaded photo.
IMPULSE DESIGN
Click Create impulse from Impulse design in the left column menu. Click Add a processing block and select Image to add an image to the second column from the left. Click Add a learning block and select Transfer Learning. Click the Save Impulse button on the far right. Click Image under Impulse design in the left menu column. Select Generate features to the right of Parameters near the top of the page. Click the Generate features button in the lower part of the Training set box. This could take five to 10 minutes (or longer) depending on how many images you have uploaded.
Select Transfer learning within Impulse design, set your Training settings (keep the defaults, check Data augmentation box), and click Start training. This step will take five minutes or more, depending on the amount of data. After running the training algorithm, you can view the predicted accuracy of the model. For example, in this model, the algorithm can only identify a Raspberry Pi 3 correctly 64.3 percent of the time and will misidentify a Pi 3 as a Pi Zero 28.6 percent of the time.
Select Model testing in the left column menu. Click the top check box to select all and press Classify selected to test your data. The output of
this action will be a percentage accuracy of your model. If the level of accuracy is low, we suggest going back to the Data Acquisition step and adding more images or removing a set of images. Select Deployment in the left menu column and select WebAssembly for your library. Scroll down (the Quantized option should be selected by default) and click the Build button. This step may also take three minutes or more, depending on the amount of data.
SETTING UP BALENACLOUD
Instead of the standard Raspberry Pi OS, we’ll flash BalenaCloudOS to our microSD card. The BalenaCloudOS is pre-built with an API interface to Edge Impulse and eliminates the need to attach a monitor, mouse, and keyboard to our Raspberry Pi.
Create a free BalenaCloud account at https://dashboard.balena-cloud.com/signup and then go to https://dashboard.balena-cloud.com/deploy to open the Create and Deploy page and create a balena-cam-tinyml application. Click Deploy to Application. After creating your application, you’ll land on the Devices page. Don’t create a device yet!
In Balena Cloud, select Service Variables and add two variables. First, add to the service edgeimpulse-inference a variable named EI_ API_ KEY and in the Value field paste the API key from the Keys section of the Edge Impulse Dashboard. Add a second variable to the service named EI_
PROJECT_ID and paste the Project ID value from the Dashboard. Select Devices from the left column menu in BalenaCloud, and click Add device. Select your Device type (Pi 4, Pi 400, or Pi 3).
Select the radio button for Development. If using Wi-Fi, select the radio button for Wi-Fi+Ethernet and enter your credentials. Download your customized BalenaOS image and write it to an SD card (using our guide to Balena Etcher on the first page)
CONNECT THE HARDWARE
Remove the microSD card from your computer and insert it into your Raspberry Pi. Attach your webcam or Pi Camera and then power up your Pi. Allow 15 to 30 minutes for your Pi to boot up and BalenaOS to update. You can check the status of your Pi Balena Cloud OS in the BalenaCloud dashboard.
Identify your internal IP address from your BalenaCloud dashboard device. Enter this IP address in a new browser Tab or Window. Place an object in front of the camera. You should start seeing a probability rating for your object in your browser window (with your internal IP address). Try various objects that you entered into the model and perhaps even objects you didn’t use to train the model.