There are lots of applications for image recognition but what I had in mind when developing this application was a solution for vision impaired people scanning fruit and vegetables at a self-service checkout.
- 1. Image Classification with Azure IoT Edge
- 1.1. Solution Overview
- 2. What is Azure IoT Edge
- 2.1. Azure IoT Edge in Action
- 2.2. Solution Architectural Considerations
- 3. Azure Services
- 3.1. Creating the Fruit Classification Model
- 3.2. Exporting an Azure Custom Vision Model
- 3.3. Azure Speech Services
- 4. How to install, build and deploy the solution
- 4.1. Understanding the Project Structure
- 4.2. Building the Solution
- 4.3. Deploying the Solution
- 4.4. Monitoring the Solution on the IoT Edge Device
- 4.5. Monitoring the Solution from the Azure IoT Edge Blade
- 5. Done!
The system identifies the item scanned against a pre-trained machine learning model, tells the person what they have just scanned, then sends a record of the transaction to a central inventory system.
The solution runs on Azure IoT Edge and consists of a number of services.
- The Camera Capture Module handles scanning items using a camera. It then calls the Image Classification module to identify the item, a call is then made to the “Text to Speech” module to convert item label to speech, and the name of the item scanned is played on the attached speaker.
- The Image Classification Module runs a Tensorflow machine learning model that has been trained with images of fruit. It handles classifying the scanned items.
- The Text to Speech Module converts the name of the item scanned from text to speech using Azure Speech Services.
- A USB Camera is used to capture images of items to be bought.
- A Speaker for text to speech playback.
- Azure IoT Hub (Free tier) is used for managing, deploying, and reporting Azure IoT Edge devices running the solution.
- Azure Speech Services (free tier) is used to generate very natural speech telling the shopper what they have just scanned.
- Azure Custom Vision service was used to build the fruit model used for image classification.
The solution is built on Azure IoT Edge which is part of the Azure IoT Hub service and is used to define, secure and deploy a solution to an edge device. It also provides cloud-based central monitoring and reporting of the edge device.
The main components for an IoT Edge solution are:-
- The IoT Edge Runtime which is installed on the local edge device and consists of two main components. The IoT Edge “hub”, responsible for communications, and the IoT Edge “agent”, responsible for running and monitoring modules on the edge device.
- Modules. Modules are the unit of deployment. Modules are docker images pulled from a registry such as the Azure Container Registry, or Docker Hub. Modules can be custom developed, built as Azure Functions, or as exported services from Azure Custom Vision,Azure Machine Learning, or Azure Stream Analytics.
- Routes. Routes define message paths between modules and with Azure IoT Hub.
- Properties. You can set “desired” properties for a module from Azure IoT Hub. For example, you might want to set a threshold property for a temperature alert.
- Create Options. Create Options tell the Docker runtime what options to start the module with. For example, you may wish to open ports for REST APIs or debugging ports, define paths to devices such as a USB Camera, set environment variables, or enable privilege mode for certain hardware operations. For more information see the Docker API documentation.
- Deployment Manifest. The Deployment Manifest pulls everything together and tells the Azure IoT Edge runtime what modules to deploy, from where, plus what message routes to set up, and what create options to start each module with.
So, with that overview of Azure IoT Edge here were my considerations and constraints for the solution.
- The solution should scale from a Raspberry Pi (running Raspbian Linux) on ARM32v7, to my desktop development environment, to an industrial capable IoT Edge device such as those found in the Certified IoT Edge Catalog.
- The solution needs camera input, I used a USB Webcam for image capture as it was supported across all target devices.
- The camera capture module needed Docker USB device pass-through (not supported by Docker on Windows) so that plus targeting Raspberry Pi meant that I need to target Azure IoT Edge on Linux.
- I wanted my developer experience to mirror the devices I was targeting plus I needed Docker support for the USB webcam, so I developed the solution on my Ubuntu 18.04 developer desktop. See my Ubuntu for Azure Developers guide.
- As a workaround, if your development device is locked to Windows then use Ubuntu in Virtual Box which allows USB device pass-through which you can then pass-through to Docker in the Virtual Machine. A bit convoluted but it does work.
The Azure Custom Vision service is a simple way to create an image classification machine learning model without having to be a data science or machine learning expert. You simply upload multiple collections of labelled images. For example, you could upload a collection of banana images and label them as ‘banana’.
To create your own classification model read How to build a classifier with Custom Vision for more information. It is important to have a good variety of labelled images so be sure to read How to improve your classifier.
This “Image Classification” module includes a simple fruit classification model that was exported from Azure Custom Vision. For more information read how to Export your model for use with mobile devices. It is important to select one of the “compact” domains from the project settings page otherwise you will not be able to export the model.
Follow these steps to export your Custom Vision project model.
- From the Performance tab of your Custom Vision project click Export.
- Select Dockerfile from the list of available options
- Then select the Linux version of the Dockerfile.
- Download the docker file and unzip and you have a ready-made Docker solution with a Python Flask REST API. This was how I created the Azure IoT Edge Image Classification module in this solution. Too easy:)
Azure Speech Services supports both “speech to text” and “text to speech”. For this solution, I’m using the text to speech (F0) free tier which is limited to 5 million characters per month. You will need to add the Speech service using the Azure Portal and “Grab your key” from the service.
Open the deployment.template.json file and update the BingKey with the key you copied from the Azure Speech service.
- Clone this GitHub
git clone https://github.com/gloveboxes/Azure-IoT-Edge-Custom-Vision-Image-Classification-with-Speech.git
- Install the Azure IoT Edge runtime on your Linux desktop or device (eg Raspberry Pi).Follow the instructions to Deploy your first IoT Edge module to a Linux x64 device.
- Install the following software development tools.
- Visual Studio Code
- Plus, the following Visual Studio Code Extensions
- Azure IoT Edge
- JSON Tools useful for changing the “Create Options” for a module.
- Docker Community Edition on your development machine
- With Visual Studio Code, open the IoT Edge solution you cloned from GitHub to your developer desktop.
The following describes the highlighted sections of the project.
- There are two modules: CameraCaptureOpenCV and ImageClassifierService.
- The module.json file defines the Docker build process, the module version, and your docker registry. Updating the version number, pushing the updated module to an image registry, and updating the deployment manifest for an edge device triggers the Azure IoT Edge runtime to pull down the new module to the edge device.
- The deployment.template.json file is used by the build process. It defines what modules to build, what message routes to set up, and what version of the IoT Edge runtime to run.
- The deployment.json file is generated from the deployment.template.json and is the Deployment Manifest
- The version.py in the project root folder is a helper app you can run on your development machine that updates the version number of each module. Useful as a change in the version number is what triggers Azure IoT Edge runtime to pull the updated module and it is easy to forget to change the module version numbers:)
You need to ensure the image you plan to build matches the target processor architecture specified in the deployment.template.json file.
- Specify your Docker repository in the module.json file for each module. For development and testing, it is faster to push images to a local docker registry
- Setup a local Docker registry for prototyping and testing purposes. It will significantly speed up the development, deployment and test cycles.
docker run -d-p 5000:5000 --restart always --name registry registry:2
- If pushing the image to a local Docker repository the specify localhost:5000.
- Confirm processor architecture you plan to build for.
- Open the deployment.template.json file
- Under settings for each module there is an image property that ends with amd64. This maps to the Platforms collecting in the module.json file for each module, which in turn maps to the Dockerfile to use for the build process. So leave as amd64 or change to arm32v7 depending on the platform you are targeting.
- If you plan to target Raspberry Pi 2, 3, or 3+ and you are developing on Linux you will need to enable cross compiling from Intel to arm32v7. After installing Docker run the following command. See How to Build ARM Docker Images on Intel host for more details.
docker run --rm--privileged multiarch/qemu-user-static:register --reset
- Next, Build and Push the solution to Docker by right mouse clicking the deployment.template.json file and select “Build and Push IoT Edge Solution”. The first build will be slow as Docker needs to pull the base layers to your local machine. If you are cross compiling to arm32v7 then the first build will be very slow as OpenCV and Python requirements need to be compiled. On a fast Intel i7-8750H processor cross compiling this solution will take approximately 40 minutes.
When the Docker Build and Push process has completed select the Azure IoT Hub device you want to deploy the solution to. Right mouse click the deployment.json file found in the config folder and select the target device from the drop-down list.
Once the solution has been deployed you can monitor it on the IoT Edge device itself using the
iotedge list command.
You can monitor the state of the Azure IoT Edge module from the Azure IoT Hub blade on the Azure Portal.
Click on the device from the Azure IoT Edge blade to view more details about the modules running on the device.
When the solution is finally deployed to the IoT Edge device the system will start telling you what items it thinks have been scanned.
Congratulations you have deployed your first Azure IoT Edge Solution!