Microsoft Cognitive Service Vision API ClientException错误 [英] Microsoft Cognitive Service Vision API ClientException Error

查看:102
本文介绍了Microsoft Cognitive Service Vision API ClientException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use Vision Cognitive Services to receive the description of an image but my code always throws this exception:

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)的名称来解决这个问题,是真的吗?

Why do I have always this Exception? I read that someone resolved this by adding something like imageStream.Seek(0) is it true?

推荐答案

您的API密钥很可能与您所点击的端点不符.如果您查看客户端的源代码您会看到默认情况下它会打到美国西部(https://westus.api.cognitive.microsoft.com/vision/v1.0),并且您的钥匙可能与另一个地区相对应(如我的情况).

Most likely your API key does not correspond to the endpoint you're hitting. If you look at the source code of the client you'll see that by default it hits West US (https://westus.api.cognitive.microsoft.com/vision/v1.0) and your key may correspond (as it was in my case) with another region.

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

You can change this by doing new VisionServiceClient(apiKey, apiRoot), where apiRoot is obtained through the Azure Portal:

在我的情况下输出Satya Nadella wearing glasses and smiling at the camera的工作代码.

Working code which in my case outputs 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);
            }
        }
    }
}

这篇关于Microsoft Cognitive Service Vision API ClientException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆