CustomVision API returns “Operation returned an in

2019-08-16 23:17发布

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

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-16 23:37

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-16 23:42

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.

查看更多
登录 后发表回答