About Me

My photo
I am an MCSE in Data Management and Analytics, specializing in MS SQL Server, and an MCP in Azure. With over 19+ years of experience in the IT industry, I bring expertise in data management, Azure Cloud, Data Center Migration, Infrastructure Architecture planning, as well as Virtualization and automation. I have a deep passion for driving innovation through infrastructure automation, particularly using Terraform for efficient provisioning. If you're looking for guidance on automating your infrastructure or have questions about Azure, SQL Server, or cloud migration, feel free to reach out. I often write to capture my own experiences and insights for future reference, but I hope that sharing these experiences through my blog will help others on their journey as well. Thank you for reading!



Call the Image Analysis 3.2 API - Azure AI services | Microsoft Learn

 csharp


public async Task AnalyzeImage(ComputerVisionClient client, string localImage)
  • public: This method is accessible to other parts of the code.
  • async: Indicates that this method runs asynchronously, meaning it can perform operations without blocking the main thread.
  • Task: Since this method is asynchronous, it returns a Task, which represents the ongoing operation. This allows the calling code to await its completion.
  • AnalyzeImage: The name of the method.
  • ComputerVisionClient client: This parameter is an instance of the Azure Cognitive Services Computer Vision Client, used to make API calls to Azure's Computer Vision service.
  • string localImage: This parameter is a string containing the path to the local image file that will be analyzed.

2. Visual Features List

csharp

List<VisualFeatureTypes> features = new List<VisualFeatureTypes>() { VisualFeatureTypes.Description, VisualFeatureTypes.Tags, };
  • List<VisualFeatureTypes> features: Creates a list that specifies which types of visual features to analyze in the image. In this case:
    • VisualFeatureTypes.Description: Requests the API to provide a description of the image.
    • VisualFeatureTypes.Tags: Requests the API to identify objects in the image and provide relevant tags.

3. Reading the Image File

csharp

using (Stream imageStream = File.OpenRead(localImage))
  • using: Ensures that the resource (imageStream) is properly disposed of after use. This is important for managing memory and file handles.
  • Stream imageStream = File.OpenRead(localImage): Opens the specified image file (localImage) as a stream, which will be passed to the Azure Computer Vision API for analysis.

4. Try-Catch Block

csharp

try {
  • try: Starts a block of code that will attempt to execute the operations inside. If an exception occurs (e.g., file not found, API failure), the code will jump to the catch block to handle the error.

5. Analyzing the Image

csharp

ImageAnalysis results = await client.AnalyzeImageInStreamAsync(imageStream, features);
  • ImageAnalysis results: Holds the results returned by the AnalyzeImageInStreamAsync method.
  • await: Pauses the execution of the method until the analysis operation is complete, without blocking the main thread.
  • client.AnalyzeImageInStreamAsync(imageStream, features): This method sends the image (as a stream) to the Azure Computer Vision API. It also passes the list of visual features (Description and Tags) that we want the API to return. The results will be stored in the results variable.

6. Processing Image Captions

csharp

foreach (var caption in results.Description.Captions) { Console.WriteLine($"{caption.Text} with confidence {caption.Confidence}"); }
  • foreach: Loops through each caption in the results.Description.Captions list.
  • caption.Text: The actual description of the image, generated by the Computer Vision service.
  • caption.Confidence: The confidence score (a value between 0 and 1) indicating how confident the service is in the accuracy of the caption.
  • Console.WriteLine(): Outputs the caption and confidence score to the console.

7. Processing Image Tags

csharp

foreach (var tag in results.Tags) { Console.WriteLine($"{tag.Name} {tag.Confidence}"); }
  • foreach: Loops through each tag in the results.Tags list.
  • tag.Name: The name of the tag (e.g., "car", "tree", etc.) that represents an object detected in the image.
  • tag.Confidence: The confidence score for each tag (a value between 0 and 1).
  • Console.WriteLine(): Outputs the tag name and its confidence score to the console.

8. Catch Block for Exception Handling

csharp

catch (Exception ex) { Console.WriteLine(ex.Message); }
  • catch (Exception ex): Catches any exceptions that might occur during the execution of the code inside the try block (e.g., issues with the file or the API).
  • Console.WriteLine(ex.Message): Outputs the exception message to the console, which helps in diagnosing the problem.

Summary of the Code Flow:

  1. Open a local image file as a stream.
  2. Send the image stream to the Azure Computer Vision API to analyze it for captions (descriptions) and tags (objects).
  3. Display the captions along with their confidence scores.
  4. Display the tags along with their confidence scores.
  5. Handle any exceptions that may occur, such as file I/O or API issues, and log them.

No comments: