cognitive vision library error : the remote server returned an error (401) in xamarin project
cognitive vision library error : the remote server returned an error (401) in xamarin project
当我尝试在 xamarin 项目中使用认知视觉库时,我收到一条错误消息:远程服务器返回错误 (401)
我正在使用 VisionServiceClient 对象
此代码是分析所选图片的代码。
我无法修复错误。
有什么建议吗?
PS : 使用免费试用Api密钥有什么问题吗?这可能是错误的原因?
我应该有一张信用卡来在 https://portal.azure.com/#home 中创建一个认知视觉资源实例吗?
这是 main_page 代码:
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;
}
}
}
}
如果您有 401
,则表示:
- 您没有提供订阅密钥
- 或者您提供了一个,但它与您的资源所在的区域不匹配
您正在使用 ComputerVision
的默认值 endpoint/region,方法如下:
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", for example for West Europe: "https://westeurope.api.cognitive.microsoft.com”
您调用的方法或参数可能会因为包的变化而发生一些变化,但您现在将是up-to-date
当我尝试在 xamarin 项目中使用认知视觉库时,我收到一条错误消息:远程服务器返回错误 (401) 我正在使用 VisionServiceClient 对象 此代码是分析所选图片的代码。 我无法修复错误。 有什么建议吗?
PS : 使用免费试用Api密钥有什么问题吗?这可能是错误的原因? 我应该有一张信用卡来在 https://portal.azure.com/#home 中创建一个认知视觉资源实例吗?
这是 main_page 代码:
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;
}
}
}
}
如果您有 401
,则表示:
- 您没有提供订阅密钥
- 或者您提供了一个,但它与您的资源所在的区域不匹配
您正在使用 ComputerVision
的默认值 endpoint/region,方法如下:
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", for example for West Europe: "https://westeurope.api.cognitive.microsoft.com”
您调用的方法或参数可能会因为包的变化而发生一些变化,但您现在将是up-to-date