Search code examples
c#azureazure-cognitive-servicesmicrosoft-custom-vision

CustomVision API returns "Operation returned an invalid status code: 'NotFound'"


I am using the Nuget package Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction

I have created a Custom Vision application in the Custom Vision portal and obtained API keys and a project ID.

Whenever I try to make a request to the API, I always get the following exception thrown:

HttpOperationException: Operation returned an invalid status code 'NotFound'

Here is my code:

        HttpClient httpClient = new HttpClient();
        CustomVisionPredictionClient customVisionPredictionClient = new CustomVisionPredictionClient(httpClient, false)
        {
            ApiKey = PredictionKey,
            Endpoint = PredictionEndpoint,
        };
        var result = customVisionPredictionClient.PredictImageAsync(CUSTOM_VISION_PROJECT_GUID, imageData);        

I have tried several different endpoints:

https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction https://southcentralus.api.cognitive.microsoft.com/customvision/Prediction/v1.0 https://southcentralus.api.cognitive.microsoft.com/customvision/v1.1/Prediction

though on the portal the listed one is the first of the list. I have also succesfuly exported my app on Azure, which gives me the second endpoint in the list but with no more success.

I have also set a default iteration as suggested in a similar issue that I found ( CustomVision: Operation returned an invalid status code: 'NotFound' ).

I have tried this sample https://github.com/Microsoft/Cognitive-CustomVision-Windows/tree/master/Samples/CustomVision.Sample which uses a deprecated windows client, to at least ensure my project information are correct and I was able to access the API.

Any insight would be appreciated


Solution

  • For the .NET client SDK, you need to specify the base endpoint URL without the version or the rest of the path. The version is automatically added by the client SDK. In other words, you'll want (assuming SouthCentralUS is your region):

    PreditionEndpoint = "https://southcentralus.api.cognitive.microsoft.com";
    CustomVisionPredictionClient customVisionPredictionClient = new CustomVisionPredictionClient()
    {
        ApiKey = PredictionKey,
        Endpoint = PredictionEndpoint,
    };
    var result = customVisionPredictionClient.PredictImageAsync(CUSTOM_VISION_PROJECT_GUID, imageData);
    

    As an aside, note that unless you want to fine-tune the behavior, you don't need to pass in an HttpClient object to CustomVisionPredictionClient constructor.

    If you need more sample code, please take a look at the QuickStart.