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

↘锁芯ラ 提交于 2019-12-11 08:44:32

问题


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


回答1:


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.




回答2:


How to use the Prediction API

If you have an image URL:

your endpoint would be something like this

https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{Project-GUID}/url?iterationId={Iteration-ID}

Set Prediction-Key Header to : predictionId
Set Content-Type Header to : application/json
Set Body to : {"Url": "https://example.com/image.png"}

Or If you have an image file:

Endpoint would be like

https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{ProjectGuid}/image?iterationId={Iteration-Id}

Set Prediction-Key Header to : Predcition-key
Set Content-Type Header to : application/octet-stream
Set Body to : <image file>

Remember, you can mark an iteration as Default so you can send data to it without specifying an iteration id. You can then change which iteration your app is pointing to without having to update your app.

Check my other answer on the similar issue using python

Python custom vision predictor fails

Hope it helps.



来源:https://stackoverflow.com/questions/55198000/customvision-api-returns-operation-returned-an-invalid-status-code-notfound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!