Creating intelligent functions with Azure Functions & Cognitive Services.

Shashank Kawle
7 min readMar 11, 2021

Creating API endpoints becomes piece of cake with the help of Azure Functions. Azure Functions is FAAS (Function As A Service) offering of Microsoft Azure. This provides a Server less environment to run your code. All you have to do is select configuration for your function and write your code.

Today, we are going to explore Azure function by creating one function integrated with one of the Cognitive service (Cloud Vision). If you don't know what is Cognitive Services, then I recommend to go through this page to get basic grasp of it. Our function will have Blob trigger as triggering point. It will pass uploaded blob reference to Cognitive services and Cognitive service will intelligently analyze objects in that blob.

Architecture of overall plan

Step 1: Creating the Resources

Subscription

If you are ne to Azure you can get Free Trial subscription for 12 months. After that you will have to upgrade your subscription as Pay-As-You-Go subscription.

Resource Group

All Azure resources are placed in Resource group. Hence if you do not have resource group we will need to create one.

As you can see I have created one Resource Group called azureChallange. It is in Free Trial subscription. All resources required for this function and function itself will be placed in this resource group.

Resources

As per our architecture we will need to create below resources:

  • App Service Plan (Required for billing purpose)
  • Azure General Purpose Storage account
  • Azure functions app (This acts as a container which holds all the functions)
  • Computer Vision Cognitive Service

Step 2: Creating the function

We will be developing Azure function through Visual Studio. Visual Studio is IDE provided by Microsoft for developing wide range of applications.

To develop azure functions in Visual Studio, we need to add Azure Extension in Visual Studio through Visual Studio Installer. Open Visual Studio Installer. Click on “Modify” button. New window will open with extensions for Visual studio. In Web & Cloud section, select Azure development and click “Install while downloading” button in bottom right section.

This will install Azure SDK and other components which is necessary to develop , debug & test Azure Functions before actually deploying them on Azure.

After modifying Visual Studio, open it and create a new project of type Azure Functions.

In the new window, give appropriate project name and click Next. After clicking on Next, new window will open with different triggers for function. Select Blob Trigger. In storage account field, click on brows an select the General purpose storage account that we created in step 1. To get the connection string, navigate to the azure storage account in Azure Portal. Go to the Access Keys section in Settings and click on the “Show Keys” button. Copy the Primary connection string and paste it in Connection string input field of Visual Studio window.

Path field in Visual Studio window defines the name of the Blob container that will be holding our images or the path where the uploaded images will reside. I am keeping it as default value i.e. samples-workitems. Later, we will have to create blob container with same name.

Click on Create. This will create new project with boiler plate code for Blob trigger functions which will look like this.

[FunctionName(“Function1”)] will define your function name. Currently it is Function1. Change it however you want.

[BlobTrigger(“samples-workitems/{name}”, Connection = “<connection string>”)] will define your trigger. As you can see it is Blob Trigger.

To access the url and other properties of uploaded Blob we need to change the Input type of Blob variable from Stream to CloudBlockBlob.

As we have discussed earlier we need to create Blob container in our General Purpose storage account. Navigate to the Storage account in Azure Portal. Click on the Storage Explorer (preview) option. This ill open new pane with 4 options in it.

  • BLOB CONTAINERS
  • FILE SHARES
  • QUEUES
  • TABLES

Right click on BLOB CONTAINERS and select “Create blob container” option. Name it as the container name that you have provided while creating function or you can also get it from trigger string in function code i.e. BlobTrigger(“samples-workitems/{name}”

Note: {name} defines the imaginary blob name. i.e. any blob get uploaded in samples-workitems container.

We need to fetch url from uploaded blob and send it to our Computer Vision API resource. But before that you need to understand, what are the parameters required for REST call to this API resource. You can understand this via API Console. Navigate to the Azure Portal. In All Resources, click Computer Vision API Resource.

As you can see, in 2nd point there is link to API console. But before that we will need Key to access this end point from console. Go to the Keys and Endpoint in Resource Management section and click on Show Keys button. Note down KEY 1 value and Endpoint value. Now go back to Quick start in Resource Management section and click on API Console link. New tab will open with different APIs. Select Detect Objects API from the list. I Request URL section note the URL format. We need to use this format y replacing {endpoint} with our noted endpoint value.

https://{endpoint}/vision/v3.0/detect

Go to the Request headers and Request body section. It will define the headers and body contents that we need to pass in order to work with Computer Vision API. If you want to test out before coding, Select appropriate region from POST section. This will open new page.

In Ocp-Apim-Subscription-Key field add KEY 1 value that you have noted from Computer Vision Resource. In Request body change the url with valid image URL. Example:

{“url”:”https://arbordayblog.org/wp-content/uploads/2018/06/oak-tree-sunset-iStock-477164218.jpg"}

Click on the Send button. Verify the Response Status field. It sholld display value as 200 OK. In Response Content section you can see the kind of response you are getting.

Switch back to Visual Studio. Noe we know how Cognitive service url is working and kind of response it is providing for particular image. Now its time to code. First, we need to get url of blob from blob variable. Next we need to create appropriate HTTP request with headers containing Ocp-Apim-Subscription-Key and body with URL of blob. After that we need to send the Request and Parse the response.

Note that I have added additional classes like RequestBody , RootObject & Metadata. Those are only for Parsing JSON data and safe to ignore. After getting Response, we can process it however we want. For now I am simply printing it on console.

Now it is time to test the function. Keep Azure Portal open in browser for uploading image file. In Visual Studio, Run the function. After successful build, it should open console window similar to this.

Now, from Azure Portal navigate to the Storage account. Click Storage Explorer(preview) and in BLOB CONTAINERS section, click on the contaier tat you have created.

Click on Upload button, and upload new image. If everything works correctly you should get output. Here I am uploading one image of the Dog and getting output like this.

Note: There can be delay of several minutes between uploading image and getting logs for the same image.

Step 3: Publishing the function

That’s it! . You have done majority of task. now we just have to publish this function to Azure. Right click on the Project Name in Soluion Explore window an click Publish. This will open new window in Visual Studio.

Make Sure that Service dependencies are configured properly. After everything is configured, click Publish.

Just to make sure that your function is working properly after deployment, navigate to your functions app, click on Functions blade in Functions section

Click on your function. Click Code + Test in Developer section. And Expand Logs window from bottom.

In the new tab, Open Azure portal, navigate to BLOB container that you have created, and upload new image. You should get logs similar to console.

Note: There can be delay of several minutes between uploading image and getting logs for the same image.

If so, then congratulations !!. You have successfully created an intelligent function with Azure Functions and Cognitive Services.

Now don just stop here. There are whole lot of other Services that Azure provides you for creating Applications. Explore them and expand your horizon of knowledge. If you like this blog, do let me know in comments. Thanks :)

--

--

Shashank Kawle

Full time learner, and love to try out new emerging technologies