Skip to main content

18. Logic Apps + Computer Vision

ยท 10 min read
Brian Benz

Welcome to Day 18 of #30DaysOfServerless!

Yesterday my Serverless September post introduced you to making Azure Logic Apps and Azure Cosmos DB work together with a sample application that collects weather data. Today I'm sharing a more robust solution that actually reads my mail. Let's learn about Teaching the cloud to read your mail!

Ready? Let's go!


What We'll Coverโ€‹

  • Introduction to the ReadMail solution
  • Setting up Azure storage, Cosmos DB and Computer Vision
  • Connecting it all together with a Logic App
  • Resources: For self-study!


Introducing the ReadMail solutionโ€‹

The US Postal system offers a subscription service that sends you images of mail it will be delivering to your home. I decided it would be cool to try getting Azure to collect data based on these images, so that I could categorize my mail and track the types of mail that I received.

To do this, I used Azure storage, Cosmos DB, Logic Apps, and computer vision. When a new email comes in from the US Postal service (USPS), it triggers a logic app that:

  • Posts attachments to Azure storage
  • Triggers Azure Computer vision to perform an OCR function on attachments
  • Extracts any results into a JSON document
  • Writes the JSON document to Cosmos DB

workflow for the readmail solution

In this post I'll walk you through setting up the solution for yourself.

Prerequisites

Setup Azure Servicesโ€‹

First, we'll create all of the target environments we need to be used by our Logic App, then we;ll create the Logic App.

1. Azure Storageโ€‹

We'll be using Azure storage to collect attached images from emails as they arrive. Adding images to Azure storage will also trigger a workflow that performs OCR on new attached images and stores the OCR data in Cosmos DB.

To create a new Azure storage account from the portal dashboard, Select Create a resource > Storage account > Create.

The Basics tab covers all of the features and information that we will need for this solution:

SectionFieldRequired or optionalDescription
Project detailsSubscriptionRequiredSelect the subscription for the new storage account.
Project detailsResource groupRequiredCreate a new resource group that you will use for storage, Cosmos DB, Computer Vision and the Logic App.
Instance detailsStorage account nameRequiredChoose a unique name for your storage account. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
Instance detailsRegionRequiredSelect the appropriate region for your storage account.
Instance detailsPerformanceRequiredSelect Standard performance for general-purpose v2 storage accounts (default).
Instance detailsRedundancyRequiredSelect locally-redundant Storage (LRS) for this example.

Select Review + create to accept the remaining default options, then validate and create the account.

2. Azure CosmosDBโ€‹

CosmosDB will be used to store the JSON documents returned by the COmputer Vision OCR process.

See more details and screen shots for setting up CosmosDB in yesterday's Serverless September post - Using Logic Apps with Cosmos DB

To get started with Cosmos DB, you create an account, then a database, then a container to store JSON documents. To create a new Cosmos DB account from the portal dashboard, Select Create a resource > Azure Cosmos DB > Create. Choose core SQL for the API.

Select your subscription, then for simplicity use the same resource group you created when you set up storage. Enter an account name and choose a location, select provisioned throughput capacity mode and apply the free tier discount. From here you can select Review and Create, then Create

Next, create a new database and container. Go to the Data Explorer in your new Cosmos DB account, and choose New Container. Name the database, and keep all the other defaults except:

SettingAction
Container IDid
Container partition/id

Press OK to create a database and container

3. Azure Computer Visionโ€‹

Azure Cognitive Services' Computer Vision will perform an OCR process on each image attachment that is stored in Azure storage.

From the portal dashboard, Select Create a resource > AI + Machine Learning > Computer Vision > Create.

The Basics and Identity tabs cover all of the features and information that we will need for this solution:

Basics Tab

SectionFieldRequired or optionalDescription
Project detailsSubscriptionRequiredSelect the subscription for the new service.
Project detailsResource groupRequiredUse the same resource group that you used for Azure storage and Cosmos DB.
Instance detailsRegionRequiredSelect the appropriate region for your Computer Vision service.
Instance detailsNameRequiredChoose a unique name for your Computer Vision service.
Instance detailsPricingRequiredSelect the free tier for this example.

Identity Tab

SectionFieldRequired or optionalDescription
System assigned managed identityStatusRequiredEnable system assigned identity to grant the resource access to other existing resources.

Select Review + create to accept the remaining default options, then validate and create the account.


Connect it all with a Logic Appโ€‹

Now we're ready to put this all together in a Logic App workflow!

1. Create Logic App

From the portal dashboard, Select Create a resource > Integration > Logic App > Create. Name your Logic App and select a location, the rest of the settings can be left at their defaults.

2. Create Workflow: Add Trigger

Once the Logic App is created, select Create a workflow from designer.

A workflow is a series of steps that defines a task or process. Each workflow starts with a single trigger, after which you must add one or more actions.

When in designer, search for outlook.com on the right under Add a trigger. Choose outlook.com. Choose When a new email arrives as the trigger.

A trigger is always the first step in any workflow and specifies the condition for running any further steps in that workflow.

Set the following values:

ParameterValue
FolderInbox
ImportanceAny
Only With AttachmentsYes
Include AttachmentsYes

Then add a new parameter:

ParameterValue
FromAdd the email address that sends you the email with attachments
3. Create Workflow: Add Action (for Trigger)

Choose add an action and choose control > for-each.

logic app for each

Inside the for-each action, in Select an output from previous steps, choose attachments. Then, again inside the for-each action, add the create blob action:

Set the following values:

ParameterValue
Folder Path/mailreaderinbox
Blob NameAttachments Name
Blob ContentAttachments Content

This extracts attachments from the email and created a new blob for each attachment.

Next, inside the same for-each action, add the get blob content action.

Set the following values:

ParameterValue
Blobid
Infer content typeYes

We create and read from a blob for each attachment because Computer Vision needs a non-virtual source to read from when performing an OCR process. Because we enabled system assigned identity to grant Computer Vision to other existing resources, it can access the blob but not the outlook.com attachment. Also, we pass the ID of the blob to use as a unique ID when writing to Cosmos DB.

create blob from attachments

Next, inside the same for-each action, choose add an action and choose control > condition. Set the value to Media Type > is equal to > image/JPEG

The USPS sends attachments of multiple types, but we only want to scan attachments that have images of our mail, which are always JPEG images. If the condition is true, we will process the image with Computer Vision OCR and write the results to a JSON document in CosmosDB.

In the True section of the condition, add an action and choose Computer Vision API > Optical Character Recognition (OCR) to JSON.

Set the following values:

ParameterValue
Image SourceImage Content
Image contentFile Content

In the same True section of the condition, choose add an action and choose Cosmos DB. Choose Create or Update Document from the actions. Select Access Key, and provide the primary read-write key (found under keys in Cosmos DB), and the Cosmos DB account ID (without 'documents.azure.com').

Next, fill in your Cosmos DB Database ID and Collection ID. Create a JSON document by selecting dynamic content elements and wrapping JSON formatting around them.

Be sure to use the ID passed from blob storage as your unique ID for CosmosDB. That way you can troubleshoot and JSON or OCR issues by tracing back the JSON document in Cosmos Db to the blob in Azure storage. Also, include the Computer Vision JSON response, as it contains the results of the Computer Vision OCR scan. all other elements are optional.

4. TEST WORKFLOW

When complete, you should have an action the Logic App designer that looks something like this:

Logic App workflow create or update document in cosmosdb

Save the workflow and test the connections by clicking Run Trigger > Run. If connections are working, you should see documents flowing into Cosmos DB each time that an email arrives with image attachments.

Check the data in Cosmos Db by opening the Data explorer, then choosing the container you created and selecting items. You should see documents similar to this:

Logic App workflow with trigger and action

1. Congratulations

You just built your personal ReadMail solution with Logic Apps! ๐ŸŽ‰


Resources: For self-study!โ€‹

Once you have an understanding of the basics in ths post, there is so much more to learn!

Thanks for stopping by!