Microsoft 认知服务视觉 API ClientException 错误

Posted

技术标签:

【中文标题】Microsoft 认知服务视觉 API ClientException 错误【英文标题】:Microsoft Cognitive Service Vision API ClientException Error 【发布时间】:2017-10-12 09:24:59 【问题描述】:

我正在尝试使用视觉认知服务来接收图像的描述,但我的代码总是抛出这个异常:

Exception Microsoft.ProjectOxford.Vision.ClientException
HResult=0x80131500
Origine=<Non è possibile valutare l'origine dell'eccezione>
Stack:
in Microsoft.ProjectOxford.Vision.VisionServiceClient.HandleException (Exception exception)
in Microsoft.ProjectOxford.Vision.VisionServiceClient.<SendAsync>b__42_1[TRequest,TResponse](Exception e)
in System.AggregateException.Handle(Func`2 predicate)
in Microsoft.ProjectOxford.Vision.VisionServiceClient.<SendAsync>d__42`2.MoveNext()
in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
in Microsoft.ProjectOxford.Vision.VisionServiceClient.<AnalyzeImageAsync>d__21`1.MoveNext()
in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
in Microsoft.ProjectOxford.Vision.VisionServiceClient.<AnalyzeImageAsync>d__20.MoveNext()
in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
in System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
in CognitiveServices.MainPage.<Button_Clicked>d__1.MoveNext() in C:\Users\manu9\documents\visual studio 2017\Projects\CognitiveServices\CognitiveServices\CognitiveServices\MainPage.xaml.cs: riga 48

这是我的代码:

using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using Plugin.Media;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace CognitiveServices

    public partial class MainPage : ContentPage
    
        public MainPage()
        
            InitializeComponent();
        

        private async void Button_Clicked(object sender, EventArgs e)
        
            var media = Plugin.Media.CrossMedia.Current;
            await media.Initialize();
            var file = await media.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            
                SaveToAlbum = false
            );
            image.Source = ImageSource.FromStream(() => file.GetStream());

            var visionClient = new VisionServiceClient("MY_API_KEY");

            var visualFeats = new VisualFeature[]
            
                VisualFeature.Description, 
                VisualFeature.Faces
            ;

            Stream imagestream = file.GetStream();
            imagestream.Seek(0, SeekOrigin.Begin);

            var result = await visionClient.AnalyzeImageAsync(imagestream, visualFeats);
            description.Text = result.Description.Captions.First().Text;

            Debug.WriteLine(result.Description.Captions[0].Text);
            file.Dispose();
        
    

为什么我总是有这个异常?我读到有人通过添加imageStream.Seek(0) 之类的内容解决了这个问题,这是真的吗?

【问题讨论】:

【参考方案1】:

您的 API 密钥很可能与您要访问的端点不对应。如果您查看source code of the client,您会看到默认情况下它会命中美国西部 (https://westus.api.cognitive.microsoft.com/vision/v1.0),并且您的密钥可能与另一个区域对应(就像我的情况一样)。

您可以通过new VisionServiceClient(apiKey, apiRoot) 更改此设置,其中apiRoot 是通过Azure 门户获得的:

在我的例子中输出Satya Nadella wearing glasses and smiling at the camera的工作代码。

using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
using System;
using System.Configuration;
using System.IO;

namespace VisionClient

    public class Program
    
        public static void Main(string[] args)
        
            AnalyzeImage();
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        

        private static void AnalyzeImage()
        
            var apiKey = ConfigurationManager.AppSettings["VisionApiSubscriptionKey"];
            var apiRoot = "https://eastus2.api.cognitive.microsoft.com/vision/v1.0";
            var visionClient = new VisionServiceClient(apiKey, apiRoot);

            var visualFeats = new VisualFeature[]
            
                VisualFeature.Description,
                VisualFeature.Faces
            ;

            Stream imageStream = File.OpenRead("satyaNadella.jpg");

            try
            
                AnalysisResult analysisResult = visionClient.AnalyzeImageAsync(imageStream, visualFeats).Result;
                foreach(var caption in analysisResult.Description.Captions)
                
                    Console.WriteLine("Description: " + caption.Text);
                
            
            catch (ClientException e)
            
                Console.WriteLine("Vision client error: " + e.Error.Message);
            
            catch (Exception e)
            
                Console.WriteLine("Error: " + e.Message);
            
        
    

【讨论】:

这为我解决了!希望 Azure Docs 提及这些事实!

以上是关于Microsoft 认知服务视觉 API ClientException 错误的主要内容,如果未能解决你的问题,请参考以下文章

Azure/Microsoft 认知服务自定义视觉 - 啥是对象检测模型输出张量规范?

Azure 认知服务--计算机视觉 API - 分析图像

Microsoft 认知服务 - 身份验证问题,无法获取访问令牌

微软认知服务应用秘籍 – 搭建基于云端的中间层以支持跨平台的智能视觉服务

Microsoft Azure 认知服务手写检测边界框参数

Microsoft Azure 认知服务 - 说话人识别 - 速率限制