Creating an OpenCV module for IoT Edge

Recently, I had a need to do image classification on the Edge using Azure IoT Edge.

To start, there are 2 challenges:

  1. No image detection module
  2. No image classification module

No problem, Azure IoT Edge support many languages, including Python, and with OpenCV we can connect to RTSP cameras and classify images.

This blog walks you through the process of setting up the image / video ingestion module.  Here is the image classification Machine Learning module .  It assumes you have Azure IoT Edge running.  New to IoT Edge?  Start here:

Our Architecture will look something like this:


Note how openCV is a module used for image (or video stream) ingestion.  We later score or interact with the images.  Also note how the openCV module allows the Machine Learning module to fetch the image for scoring and processing.  This interaction allows for images or streams of any size to be scored and overcomes any message limitations.

To get started, open a new folder in Visual Studio Code and select View –> Command Palette


Select Edge: New IoT Edge Solution


Name your solution


Select Python Module Language


Name your Module


Select your Container Registry


Open the file and replace the contents from:  It is worth looking through the code to really understand what is happening.  Close and save this file.

Open the Dockerfile and replace the contents from: note this file actually downloads opencv and builds the binaries.  Close and save this file.

Right click on module.json and click Build and Push IoT Edge Module Image


Select amd64 as your platform.  As of this writing, the Python module is only supported on 64 bit Linux.


Assuming you have login credentials to your container registry, it should upload and you can view the image via the Azure Portal


Next we will configure an IoT deployment, with settings needed.  Note, you can add other modules and routes later, which are not the focus of this blog.

In the Azure Portal, goto an existing IoT Edge device.  Select Set Modules


Select Add IoT Edge Module


At the module dialog, name the module, type your URI and (optionally) include the container create options that allow remote viewing of the images and include the desired properties of a valid RTSP URL as shown below.  You can copy and past the following JSON to make it simpler.


You can copy and past the following JSON to make it simpler.

	"ExposedPorts": {
		"8080/tcp": {}
	"HostConfig": {
		"PortBindings": {
			"8080/tcp": [
					"HostPort": "8080"

	"properties.desired": {
 		"imageProcessingInterval": 15,
 		"camaraArray" : {
 			"MyCamera": "rtsp://myUser:myPassword@"

Note, the JSON above assumes you are using a remote camera.  If you are using a local camera on the Linux host, you need to use the following JSON as the create options to give access to the /dev/video0 device:

  "ExposedPorts": {
    "8080/tcp": {}
  "HostConfig": {
    "Privileged": true, 
    "Binds": [
    "PortBindings": {
      "8080/tcp": [
          "HostPort": "8080"

Click Save and Next for the default route.

Finally click submit for to submit the task.

Assuming no typos, you should be able to type “docker ps” to see the image running and that it is visible from port 8080.


Viewing the docker logs (docker logs opencv) you will see the following logs in the opencv module:


You can also web browse to the docker container to see the image:


Lastly, you can see the message from Device Explorer:


Note we see the image size, name, URL, and changes from the prior image.  To understand the change methods, look to:

Putting it all together and using Azure Stream Analytics + Power BI, we can see real-time the changes happening:


Also, looking at it from a hardware perspective, without any GPU’s it can consume CPU and, as shown below:


This is running on a 4 core “Intel(R) Core(TM) i5-2467M CPU @ 1.60GHz” machine.


One thought on “Creating an OpenCV module for IoT Edge

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s