Serverless

Seasons of Serverless, Custom Vision, and Lovely Ladoos

December 7, 2020

Today, I am continuing my Seasons of Serverless series.

In this article, I'll cover my solution to Challenge 2: Lovely Ladoos.

Table of Contents

Prerequisites

1. Challenge outline

2. My solution

3. Check the results

4. More resources


Prerequisites

  • Visual Studio Code
  • Azure Functions Extension Installed
  • Custom Vision

1. Challenge outline

In this challenge, we will enter URLs of ladoo images. The serverless function will return whether the image is a ladoo or not.

Input

For this challenge, our input will be an image URL.

Output

If our image is a ladoo, our output will be:

This is a ladoo :).

If our image is not a ladoo, our output will be:

This is not a ladoo :(.

2. My solution

Step 1. Create your Custom Vision app.

For my project, I created two different tags. The first tag I used was ladoo.

New Project Icon

The second tag I used was other which included images of glazed donut holes, cinnamon donut holes, cream puffs, and mandarin oranges.

New Project Icon

I then selected the Quick Training option. After it completed training, I felt confident enough in my model to begin writing my Azure Function.

New Project Icon

Step 2. Create your Azure Function.

When creating my Azure Function, I selected the HTTP trigger, named the function ladooVision, snd gave it an Anonymous authorization level. I used JavaScript for my solution but there are lots of different languages you can use. I edited one file to include the following code: index.js.

// ladooVision/index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const imageUrl = (req.query.url || (req.body && req.body.url));

    if (!imageUrl) {
        context.res = {
            status: 400,
            body: 'Image url is required'
        };
        return;
    }

    const projectId = [Your project ID here];
    const customVisionPredictionKey = [Your prediction key here];
    const customVisionPredictionEndPoint = [Your prediction endpoint here];
    if (!customVisionPredictionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

    const credentials = new ApiKeyCredentials({ inHeader: {"Prediction-key": customVisionPredictionKey } });
    const client = new PredictionAPIClient(credentials, customVisionPredictionEndPoint);

    await client
        .classifyImageUrl(projectId, "Iteration1", { url: imageUrl })
        .then(result => {
            let message = "This is not a ladoo :(.";
            if (result.predictions[0].tagName === "ladoo") {
                message = "This is a ladoo :).";
            }
            context.res = {
                body: message
            };
        })
        .catch(err => {
            console.log("An error occurred:");
            console.error(err);

            context.res = {
                status: 502,
                body: err
            };
        });
}

3. Check the results

After deploying my code to Azure's Function App, this is what I got:

Ladoo Input

New Project Icon

Ladoo Output

New Project Icon

Other input

New Project Icon

Other output

New Project Icon

4. More resources

  1. Join this week's Seasons of Serverless challenge
  2. Learn more about Azure Functions on Microsoft Learn
  3. Check out my solution to Challenge 1
  4. Check out my article to learn how to set up your first Custom Vision project
Copyright © Locksley Kolakowski 2021 | All opinions are my own