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
andTags
) that we want the API to return. The results will be stored in theresults
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 theresults.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 theresults.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:
- Open a local image file as a stream.
- Send the image stream to the Azure Computer Vision API to analyze it for captions (descriptions) and tags (objects).
- Display the captions along with their confidence scores.
- Display the tags along with their confidence scores.
- Handle any exceptions that may occur, such as file I/O or API issues, and log them.
No comments:
Post a Comment