当我尝试在xamarin项目中使用认知视觉库时,我收到一条错误消息:远程服务器返回错误(401) 我正在使用VisionServiceClient对象 此代码是用于分析所拍摄图片的代码。 我无法解决错误。 有什么建议吗?
PS:使用免费试用版Api密钥是否有问题?这可能是错误的原因吗? 我应该用信用卡在https://portal.azure.com/#home中创建认知视觉资源实例吗?
这是主页代码:
using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using Plugin.Connectivity;
using Plugin.Media;
using Plugin.Media.Abstractions;
using System;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ComputerVisionSample
{
public partial class MainPage : ContentPage
{
private readonly VisionServiceClient visionClient;
public MainPage()
{
InitializeComponent();
this.visionClient =
new VisionServiceClient("my_api_key");
}
private async Task<AnalysisResult> AnalyzePictureAsync(Stream inputFile)
{
if (!CrossConnectivity.Current.IsConnected)
{
await DisplayAlert("Network error",
"Please check your network connection and retry.", "OK");
return null;
}
VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult,
VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description,
VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
AnalysisResult analysisResult =
await visionClient.AnalyzeImageAsync(inputFile,
visualFeatures);
return analysisResult;
}
private async void UploadPictureButton_Clicked(object sender, EventArgs e)
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("No upload", "Picking a photo is not supported.", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
this.Indicator1.IsVisible = true;
this.Indicator1.IsRunning = true;
Image1.Source = ImageSource.FromStream(() => file.GetStream());
try
{
this.BindingContext = await AnalyzePictureAsync(file.GetStream());
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
return;
}
finally
{
this.Indicator1.IsRunning = false;
this.Indicator1.IsVisible = false;
}
}
}
}
答案 0 :(得分:0)
如果您有401
,则表示:
您正在通过以下方式使用ComputerVision
的默认端点/区域:
this.visionClient = new VisionServiceClient("my_api_key");
您正在使用旧程序包(Microsoft.ProjectOxford.Vision
是项目代号)。
因此,您应该执行以下操作:
1-切换到名为Microsoft.Azure.CognitiveServices.Vision.ComputerVision
的最新软件包,该软件包可在Nuget here上获得
2-通过执行以下操作创建您的客户端:
var visionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials("yourAPIkeyHere")))
{
Endpoint = "yourEndpointHere"
})
端点格式为:“ https://region.api.cognitive.microsoft.com”,例如在西欧:“ https://westeurope.api.cognitive.microsoft.com”
由于软件包的更改,您正在调用的方法或参数可能有所更改,但是现在您是最新的