我正在尝试使用Visual Studio的AI工具制作一个简单的图像分析项目。我已将Azure Cognitive Service创建为ComputerVision服务。自创建它以来已超过10分钟,因此api密钥应该有效。然后我通过右键单击它创建了一个新项目。但是当我运行它时,我得到了这个例外:
Please input image url or locate a local image file. If input is empty, example image will be used.
No url or file specified, use the example https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg
One or more errors occurred.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at GetImageDetailsService.Program.Main(String[] args) in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\Program.cs:line 27
Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.ComputerVisionErrorException: Operation returned an invalid status code 'Unauthorized'
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPI.<DescribeImageWithHttpMessagesAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPIExtensions.<DescribeImageAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.CognitiveServices.Samples.ComputerVisionApiExtensions.<DescribeImage>d__2.MoveNext() in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\ComputerVisionApiExtensions.cs:line 34
显然我没有被授权。是什么导致这个? 我登录了我的Azure订阅,我有资金(我正在使用F0层)。我还检查了区域是否正确以及api密钥是否匹配。
编辑:当我将api密钥更改为应用程序调用中的不同内容时,我仍然会收到未经授权的异常。
Edit2:添加了调用api的代码。
var client = new ComputerVisionApiExtensions(ComputerVisionSubscriptionKey, ComputerVisionRegion, null);
var result = client.DescribeImage(imagePath).Result;
答案 0 :(得分:1)
对于计算机视觉API类型的Azure Cognitive Services,分析图像的请求如下所示:
POST https://{location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en
Host: {location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: {subscription-key-under-the-same-region-of-your-Computer-Vision-API}
{"url":"https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg"}
注意:您必须像以前一样在REST调用中使用相同的区域 获取订阅密钥。例如,如果你有你的 来自westus的订阅密钥,替换URL中的“westcentralus” 下面有“westus”。
如果您的Azure认知服务位于东亚,当您使用订阅密钥请求westcentralus.api.cognitive.microsoft.com
而不是eastasia.api.cognitive.microsoft.com
时,您可以获得401拒绝访问和以下响应:
{
"statusCode": 401,
"message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."
}
您可以在运行应用程序时使用fiddler捕获网络跟踪以缩小此问题。此外,您可以通过Azure门户遵循认知服务的“资源管理&gt;快速入门”下的详细教程。
答案 1 :(得分:1)
我得到同样的东西,并认为submit an issue值得。我得到了一个修复的响应,测试了它,这一切都有效!
要解决此问题,只需将this.AzureRegion = GetRegion(region);
放在ComputerVisionApiExtensions.cs
文件的构造函数中。