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.
The second tag I used was other which included images of glazed donut holes, cinnamon donut holes, cream puffs, and mandarin oranges.
I then selected the Quick Training option. After it completed training, I felt confident enough in my model to begin writing my Azure Function.
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
Ladoo Output
Other input
Other output
4. More resources
- Join this week's Seasons of Serverless challenge
- Learn more about Azure Functions on Microsoft Learn
- Check out my solution to Challenge 1
- Check out my article to learn how to set up your first Custom Vision project