Unable to use Custom Vision Model with latest library "Azure.AI.Vision.ImageAnalysis"

Deepankar 40 Reputation points
2024-12-19T16:04:23.3733333+00:00

Hi,

In the latest update of the library, I am unable to use the Analyze method or pass my custom model name through the endpoint to get the confidence score or other data for the image URI I am analyzing. Can someone provide the latest syntax for using a custom AI Vision Model deployed in C#?

I am unable to find the syntax on the Microsoft portal. It mentions passing the model name in the endpoint as a Model-name parameter for a custom vision model, but I need guidance on how to use the analyze function to get the confidence score of the image I am passing. The function requires both the feature parameter and the options parameter.

Azure AI Custom Vision
Azure AI Custom Vision
An Azure artificial intelligence service and end-to-end platform for applying computer vision to specific domains.
254 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Ketsha 165 Reputation points Microsoft Employee
    2024-12-19T16:27:40.5433333+00:00

    Hi Deepankar -

    To use the Analyze method with a custom AI Vision Model in C#, you’ll need to ensure you’re passing the correct parameters, including the model name, feature, and options. Here’s a basic example of how you can achieve this:

    Set up your client:

    var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials("<Your API Key>"))

    { Endpoint = "<Your Endpoint>"};

    Define the parameters:

    string imageUri = "<Your Image URI>";

    string modelName = "<Your Custom Model Name>";

    var features = new List<VisualFeatureTypes?> { VisualFeatureTypes.Objects };

    var options = new ImageAnalysisOptions

    {

    ModelVersion = modelName
    

    };

    Call the Analyze method:

    var analysisResult = await client.AnalyzeImageAsync(imageUri, features, options);

    Extract the confidence score:

    foreach (var obj in analysisResult.Objects)

    {

    Console.WriteLine($"Object: {obj.ObjectProperty}, Confidence: {obj.Confidence}");
    

    }

    In this example, replace "<Your API Key>", "<Your Endpoint>", "<Your Image URI>", and "<Your Custom Model Name>" with your actual values. The AnalyzeImageAsync method takes the image URI, a list of features to analyze, and options that include your custom model name.


  2. Ketsha 165 Reputation points Microsoft Employee
    2024-12-19T17:22:09.2233333+00:00

    I see what you are saying. The classes and methods you mentioned, such as VisualFeatureType and ImageAnalysisOptions, have been removed or updated in the recent package. The other option is as follows:

    To use the latest syntax for analyzing images with a custom AI Vision Model in C#, you can follow these updated steps. I am positive that this should work, please let me know the outcome.

    Set up your client:

    var client = new ImageAnalysisClient(new Uri("<Your Endpoint>"), new AzureKeyCredential("<Your API Key>"));

    Define the parameters:

    string imageUri = "<Your Image URI>";

    string modelName = "<Your Custom Model Name>";

    Call the Analyze method:

    var analysisOptions = new ImageAnalysisOptions

    {

    ModelVersion = modelName,
    
    Features = new List<ImageAnalysisFeature> { ImageAnalysisFeature.Objects }
    

    };

    var analysisResult = await client.AnalyzeImageAsync(new Uri(imageUri), analysisOptions);

    Extract the confidence score:

    foreach (var obj in analysisResult.Objects)

    {

    Console.WriteLine($"Object: {obj.Name}, Confidence: {obj.Confidence}");
    

    }

    In this example, replace <Your Endpoint>, <Your API Key>, <Your Image URI>, and <Your Custom Model Name> with your actual values. The AnalyzeImageAsync method now takes a URI and an ImageAnalysisOptions object that includes your custom model name and the features to analyze.

    I hope this helps! Let me know if you have any further questions or need additional assistance.


  3. Deepankar 40 Reputation points
    2024-12-22T08:23:47.25+00:00

    I am able to achieve my requirement through Rest API using the below endpoint syntax:

    string requestUrl = $"{endpoint}/computervision/imageanalysis:analyze?model-name={modelName}&api-version=2023-04-01-preview";

    This is the syntax which is working for my requirement. As of now, I think .Net SDK is not allowing to use custom model. The only way to achieve is to through REST API.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.