Image Analysis with Azure AI Vision

Image Analysis with Azure AI Vision

In this tutorial, I'll walk you through how easy it is to get started with Azure AI services, specifically focusing on image analysis using Azure AI Vision. Whether you're new to Azure or looking to add image recognition capabilities to your applications, this guide will show you just how simple it is to get started with a few lines of code.

Why Azure AI Vision?

Azure offers various AI services that are powerful and easy to implement, especially for tasks like image analysis. Azure AI Vision (formerly known as Azure Computer Vision) is one such service that enables developers to analyze images, extract captions, and gather insights from visual content. This is particularly useful for applications needing object recognition, caption generation, and other visual intelligence features.

Setting Up Your Azure Resources

Before diving into the code, let’s talk briefly about setting up Azure resources. You have two main options:

  1. Single-Service Option: You can create an individual "Computer Vision" resource in Azure. This will give you access to image analysis capabilities using a specific API key just for this service.

  2. Multi-Service Option: You can also create an Azure AI Services instance, which provides access to multiple AI services under one roof. With a single API key, you can utilize various capabilities, including Speech, Face, Document Intelligence, and Vision. This option is often more efficient if you plan to use multiple Azure AI services in your application.


For the rest of this tutorial, I'll assume you've created a Computer Vision or Azure AI Services resource and have your Endpoint URL and API Key ready.

AI Image Analysis

Our goal is to use Azure AI Vision to generate a caption for an image. We’ll achieve this in a few lines of code, leveraging Azure's SDK Azure.AI.Vision.ImageAnalysis to connect to the service, analyze an image, and retrieve a caption.

For this example, this is the image we'll analyze:

Below is all the code needed for creating a simple caption generation function using Azure AI Vision. Make sure to replace <your azure computer vision endpoint> and <your azure AI api key> with your specific values from the Azure portal.

using Azure; 
using Azure.AI.Vision.ImageAnalysis;

var client = new ImageAnalysisClient(new Uri("<your azure computer vision endpoint>"), new AzureKeyCredential("<your azure AI api key>"));

var result = client.Analyze(new Uri("<the url to your image>"), VisualFeatures.Caption, new ImageAnalysisOptions { GenderNeutralCaption = true }); Console.WriteLine(result.Caption.Text);

Console.WriteLine(result.Caption.Text);

Here’s what each line does:

  1. Initialize the Client: We create an ImageAnalysisClient with our Azure endpoint and API key. These details can be found in your Azure portal under the created Computer Vision or Azure AI Services resource.

  2. Analyze the Image: Using the Analyze method, we provide the image URL and specify the feature we want (in this case, VisualFeatures.Caption). The GenderNeutralCaption option ensures that any captions generated are gender-neutral.

  3. Print the Result: Finally, we print the caption text to the console. This caption is generated by Azure's image analysis model and provides a concise description of the image content. 

Running this code will output a caption, in this example:

"a painting of a santa claus riding a reindeer"


Summary

Azure AI Vision is a powerful tool for developers looking to add AI image analysis to their applications. It provides an easy-to-implement solution that integrates seamlessly into our projects. And if you want to explore beyond image analysis, Azure’s AI services also offer capabilities such as speech, document processing, face recognition, and more, all through a unified API key when using the Azure AI Services resource. Hope this tutorial was helpful to you.

Cheers friends! ❤️