
Hi !
Today’s post will be mostly focused on code. And on the main tasks
- Add a configuration property to the module to enable / disable the telemetry messages
- Each time the Azure IoT Module process an image, it will trigger a telemetry with the information of the detected image
The sample running show how for each image analyzed (sent using Postman) a new message is triggered to Azure IoT.

Let’s start from here.
Add a property to enable / disable the message trigger
Let’s start adding environment variable to the Azure IoT Module to define if the module will trigger messages.

Then I defined this function to read the environment value
def GetEnvVarBool(name):
varVal = bool(os.environ[name])
return varVal
And I reading the env value is as easy as:
trigger_enabled = GetEnvVarBool('ReportValues')
Send an Azure IoT Hub message for each processed image
As I defined in my previous posts, I have a variable named [module_client] that represents the Azure IoT Module. The following function will
- Validate if the trigger messages is enabled
- Load the json response from the Custom Vision image process
- Sort the predictions results by probability
- Add each tagName as a message property
- Trigger the message
async def send_iot_message(strMessage):
global module_client
if (trigger_enabled == False):
return
message = Message("")
# load json def and add each tag as properties
jsonStr = strMessage.get_data(as_text=True)
jsonObj = json.loads(jsonStr)
preds = jsonObj['predictions']
sorted_preds = sorted(preds, key=lambda x: x['probability'], reverse=True)
if (sorted_preds):
for pred in sorted_preds:
# tag name and prob * 100
tagName = str(pred['tagName'])
probability = pred['probability'] * 100
message.custom_properties[tagName] = str(probability)
await module_client.send_message(message)
And, finally the call to this function is performed on each http endpoint using asyncio.run() to avoid a thread block.
def predict_image_handler(project=None, publishedName=None):
try:
imageData = None
if ('imageData' in request.files):
imageData = request.files['imageData']
elif ('imageData' in request.form):
imageData = request.form['imageData']
else:
imageData = io.BytesIO(request.get_data())
img = Image.open(imageData)
results = predict_image(img)
jsonResults = jsonify(results)
asyncio.run(send_iot_message(jsonResults))
return jsonResults
So far, so good. I covered a trivial scenario and I think is a good memory backup for me !
Resources
- Azure Custom Vision
- Azure Custom Vision Documentation
- Azure IoT Tools
- Deploy your first IoT Edge module to a virtual Linux device
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
Azure โ IoT
- Install โ Azure IoT on Raspberry Pi
- Deploy โ Azure Blob Storage on IoT Edge, lessons learned
- Connect to โ Azure Blob Storage on IoT Edge using Microsoft Azure Storage Explorer
- Lesson learned and tips on how to install Azure IoT Edge on Ubuntu on a Raspberry Pi
- Azure IoT Explorer, in preview and awesome
- Mapping a local โ Azure IoT Edge folder module with an Edge device folder ๐
- Creating a folder ๐ in the docker definition in an โ Azure IoT Edge
- Granting access to Raspberry Pi GPIO from an โ Azure IoT Edge Module
Create an Azure IoT Module using Raspberry Pi and Grove Sensors
- Raspberry Pi + Grove Sensors, read temperature and humidity values
- Raspberry Pi + Grove Sensors, send temperature and humidity values as telemetry to Azure IoT Hub
- Raspberry Pi + Grove Sensors, create a Azure IoT Module to send temperature and humidity values as telemetry to Azure IoT Hub
- Raspberry Pi + Grove Sensors, publish and use the Azure IoT Module
- Raspberry Pi + Grove Sensors, notes on build and configuration
- Raspberry Pi + Grove Sensors, details on how to send a telemetry message and sample messages
Create an Azure IoT Module from Azure Custom Vision project
- Create and export a Custom Vision Project as Docker image
- Analyze the content of the CV Docker image
- Create and analyze an Azure IoT Module
- Merge the CV project as an Azure IoT Module
- Deploy to an Azure IoT device and test the CV module
- Send telemetry for each analyzed image
- Add digital twin configuration to the Azure IoT module (coming soon)
ยฟCon ganas de ponerte al dรญa?
En Lemoncode te ofrecemos formaciรณn online impartida por profesionales que se baten el cobre en consultorรญa:
- Si tienes ganas de ponerte al dรญa con Front End (ES6, Typescript, React, Angular, Vuejs…) te recomendamos nuestros Mรกster Front End: https://lemoncode.net/master-frontend#inicio-banner
- Si te quieres poner al dรญa en Backend (stacks .net y nodejs), te aconsejamos nuestro Bootcamp Backend: https://lemoncode.net/bootcamp-backend#bootcamp-backend/banner
- Y si tienes ganas de meterte con Docker, Kubernetes, CI/CD…, tenemos nuestro Bootcamp Devops: https://lemoncode.net/bootcamp-devops#bootcamp-devops/inicio