我试图通过使用 pickphoto 跨媒体提交图像并获得预测结果来使用 Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction 功能。我试图将图像作为 url 或流传递并不断收到错误请求。我知道我有正确的预测键和端点,因为我从事训练工作,告诉我是我将图像传递给方法的方式。将图像从 Cross Media 挑选照片包转换为
private async void UplodatePictureButton_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
MediaFile file;
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("No upload", "Picking a photo is not supported", "OK");
return;
}
file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
{
return;
}
MainImage.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
// Create the Api, passing in the training key
CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient()
{
ApiKey = trainingKey,
Endpoint = SouthCentralUsEndpointTraining
};
var projects = trainingApi.GetProjects();
var project = projects.FirstOrDefault(p => p.Name == "Car");
CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
{
ApiKey = predictionKey,
Endpoint = SouthCentralUsEndpointPrediction
};
var result = endpoint.ClassifyImageUrl(project.Id, project.Name, new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(file.Path));
foreach (var c in result.Predictions)
{
Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
}
}
未处理的异常:Microsoft.Azure.CognitiveService.Vision.CustomerVision.Prediction.Models.CustomVisionErrorException:操作返回了无效的状态代码“BadRequest”期望预测。