Exploration on Azure Computer Vision Model To Build AI-Ready Web Applications
This article explains how to integrate computer vision SDK with JavaScript to develop custom models or use pre-built models to build intelligent web applications.
Join the DZone community and get the full member experience.
Join For FreeThe Azure Computer Vision model is a powerful tool for building AI-ready web applications. This model enables developers to extract insights and meaning from visual data and can be used to build a wide range of applications, from image classification and object detection to facial recognition and natural language processing. In this article, we will explore the capabilities of the Azure Computer Vision model and how it can be used to build AI-ready web applications. We will also discuss the benefits of using this model and provide a step-by-step guide on how to get started.
Capabilities of Azure Computer Vision Model
The Azure Computer Vision model is a cloud-based API that enables developers to analyze and understand visual data. This model can be used to:
1. Classify Images Into Different Categories
Azure Computer Vision can classify images into different categories such as objects, scenes, and actions.
Ex: for the below picture, it will classify into the category Animal_dog
Image from Google
2. Detect Objects Within Images
Azure Computer Vision can detect objects within images, including people, animals, and objects.
Ex: The below picture will detect the object apple and provide the response in the JSON as shown below.
Image from Google
{
"objects":[
{
"rectangle":{
"x":730,
"y":66,
"w":135,
"h":85
},
"object":"Apple",
"confidence":0.98
}
}
3. Recognize Faces and Extract Facial Features
Azure Computer Vision can recognize faces and extract facial features such as age, gender, and emotions.
Image from Microsoft
{
"faces": [
{
"age": 11,
"gender": "Male",
"faceRectangle": {
"top": 62,
"left": 22,
"width": 45,
"height": 45
}
},
{
"age": 11,
"gender": "Female",
"faceRectangle": {
"top": 127,
"left": 240,
"width": 42,
"height": 42
}
},
{
"age": 37,
"gender": "Female",
"faceRectangle": {
"top": 55,
"left": 200,
"width": 41,
"height": 41
}
},
{
"age": 41,
"gender": "Male",
"faceRectangle": {
"top": 45,
"left": 103,
"width": 39,
"height": 39
}
}
],
"requestId": "3a383cbe-1a05-4104-9ce7-1b5cf352b239",
"metadata": {
"height": 230,
"width": 300,
"format": "Png"
}
}
4. Extract Text From Images
Azure Computer Vision can extract text from images, including handwritten and printed text.
5. Generate Image Descriptions
Azure Computer Vision can generate image descriptions, including captions and tags.
Benefits of Using Azure Computer Vision Model
Using the Azure Computer Vision model can bring numerous benefits to your web application, including:
- Improved user experience: By analyzing visual data, you can provide users with a more personalized and engaging experience.
- Increased efficiency: Automating image analysis can save time and reduce manual effort.
- Enhanced security: Facial recognition and object detection can be used to enhance security and prevent fraud.
- Better decision-making: Extracting insights from visual data can help businesses make informed decisions.
You can explore and play with Azure custom vision models by navigating to the Azure AI | Vision Studio.
Step-By-Step Guide To Building AI-Ready Web Applications With Azure Computer Vision Model
Here's a step-by-step guide to building AI-ready web applications with the Azure Computer Vision model:
- Sign up for Azure: Create an Azure account and subscribe to the Computer Vision service by navigating to the AI Vision studio link. Copy Key and Endpoint which is used later in code to call computer vision client SDK.
- Choose a programming language: Choose a programming language such as Python, Java, or C# to build your application. I have used NodeJS for the code example below.
- Install the Azure Computer Vision SDK: Install the Azure Computer Vision SDK for your chosen programming language.
- Upload images: Upload images to your Azure storage account which you can use in your code to analyze it
- Analyze images: Use the Azure Computer Vision API to analyze images and extract insights.
- Integrate with your web application: Integrate the extracted insights with your web application to provide a more personalized and engaging experience
The code below is written using NodeJS where I have used the computer vision client SDK to analyze an image that contains fruits and vegetables and provide output about detected objects and the response.
'use strict';
var http = require('http');
var port = process.env.PORT || 1337;
const { ComputerVisionClient } = require("@azure/cognitiveservices-computervision");
const { ApiKeyCredentials } = require("@azure/ms-rest-js");
const key = '<subscription Key>';
const endpoint = '<computervision endpoint>';
const credentials = new ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } });
const client = new ComputerVisionClient(credentials, endpoint);
http.createServer(async function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
//res.end('Hello World\n');
// Define the URL of the image uploaded to storage account which you want to analyze
const imageUrl = '<imageUrl>';
// Call the function to analyze the image
const result = await analyzeImage(imageUrl, { visualFeatures: ['Objects'] });
// Assuming 'result' is the response from the analyzeImage call
if (result.objects && result.objects.length > 0) {
const fruitsAndVeggies = result.objects.filter(obj => obj.object.toLowerCase().includes('fruit') || obj.object.toLowerCase().includes('vegetable'));
console.log("Detected fruits and vegetables:", fruitsAndVeggies);
} else {
console.log("No fruits or vegetables detected.");
}
// Send the analysis result as the response
res.end(`Image Analysis Result:\n${JSON.stringify(result, null, 2)}`);
}).listen(port);
async function analyzeImage(url) {
try {
const result = await client.analyzeImage(url, { visualFeatures: ['Categories', 'Description', 'Objects'] });
console.log("Image analysis result:", result);
return result;
} catch (error) {
console.error("An error occurred while analyzing the image:", error);
return error;
}
}
Conclusion
The Azure Computer Vision model is a powerful tool for building AI-ready web applications. By leveraging this model, developers can extract insights and meaning from visual data and provide users with a more personalized and engaging experience. With vast areas, numerous benefits, and ease of use, the Azure Computer Vision model is an ideal choice for businesses looking to build intelligent AI-ready web applications.
Visit here for more details about learning Azure Computer Vision.
Happy coding !!!
Opinions expressed by DZone contributors are their own.
Comments