CustomVision API返回“操作返回了无效的状态码:'NotFound'”

时间:2019-03-16 14:45:13

标签: c# azure azure-cognitive-services microsoft-custom-vision

我正在使用Nuget包Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction

我已经在Custom Vision门户中创建了Custom Vision应用程序,并获得了API密钥和项目ID。

每当我尝试向API发出请求时,总是会抛出以下异常:

  

HttpOperationException:操作返回了无效的状态码   “未找到”

这是我的代码:

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

我尝试了几种不同的端点:

  

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

尽管在门户网站上列出的是列表的第一名。我还成功地在Azure上导出了我的应用程序,这使我成为列表中的第二个端点,但没有成功。

我还根据发现的类似问题(CustomVision: Operation returned an invalid status code: 'NotFound')的建议设置了默认迭代。

我已尝试使用不推荐使用的Windows客户端的示例https://github.com/Microsoft/Cognitive-CustomVision-Windows/tree/master/Samples/CustomVision.Sample,以至少确保我的项目信息正确并且能够访问API。

任何见识都会受到赞赏

2 个答案:

答案 0 :(得分:1)

如何使用Prediction API

如果您有图片网址:

您的端点将是这样

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"}

或者,如果您有图像文件:

端点就像

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>

请记住,您可以将一个迭代标记为默认值,这样就可以在不指定迭代ID的情况下向其发送数据。然后,您可以更改应用程序指向的迭代版本,而不必更新应用程序。

使用python检查有关类似问题的其他答案

Python custom vision predictor fails

希望有帮助。

答案 1 :(得分:1)

对于.NET客户端SDK,您需要指定基本端点URL ,而不包含版本或路径的其余部分。该版本由客户端SDK自动添加。换句话说,您会想要的(假设您的区域是SouthCentralUS):

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

顺便说一句,请注意,除非要微调行为,否则无需将HttpClient对象传递给CustomVisionPredictionClient构造函数。

如果您需要更多示例代码,请查看QuickStart