Select Page

This Microsoft Azure AI Vision post will show how to use Azure AI Vision with C# to analyze images.

The Azure AI Vision service is part of the Azure AI Services family and offers the following capabilities.

  • Optical Character Recognition (OCR)  – OCR allows us to extract text from images (printed or handwritten)
  • Image Analysis – Extract visual features from images, provide captions, and recognize faces and objects.
  • Face – Recognise human faces that can be used for Facial recognition software (image blurring, identification and access control)

The above list can help develop AI solutions to detect humans or objects using Azure’s deep-learning-based models.

.NET SDK

To access Azure AI Vision programmatically, we can use one of the available SDKs for the service (Python, Java, NET). In our case, we will use the Azure SDK for .NET using a C# application that takes an image and passes it to the service.

The first step in using the SDK is providing AI Vision service in Azure.

Create an Azure AI Vision resource using Bicep

To deploy an Azure AI Vision to Azure, we will use Azure Bicep. Bicep is a Domain-specific language (DSL) used for infrastructure provisioning.

The following main.bicep file will create an Azure AI Vision resource.

To deploy the resource, install Azure PowerShell and log into Azure.

Create a resource group.

Run the following command to deploy the resource.

After deploying the resource, open it from the portal and note the Key and Endpoint details, as we will use them later.

Use Azure AI Vision With C# to Analyze Images

After creating the AI Vision resource, we are ready to use the SDK and create a program that uses the AI Vision service.

Start with creating a C# Console application using the following command.

Copy the following content to your .csproj file. The file contains all the program’s packages and adds the option to use appsettings.json to store the key and endpoint details.

Create a file called appsettings.json in the application’s root folder and add the resource Key and Endpoint.

In the Program.cs file, add the following code.

To test the application, create a directory called images in the root folder and add images.

In the program.cs, you can set the name of the image to specify which image AI Vision will analyse.

Note: You can easily change this to either prompt for an image name read all the images inside the folder.

Run Application

To run the application, open the terminal window and run

dotnet run 

The output will look similar to the text below.