【问题标题】:C# project unable to find GOOGLE_APPLICATION_CREDENTIALS for google visionC# 项目无法为谷歌视觉找到 GOOGLE_APPLICATION_CREDENTIALS
【发布时间】:2018-07-13 14:08:36
【问题描述】:

我正在尝试在我的 c# 项目中使用 googles vision api。我正在使用他们提供的测试代码,但由于某种原因,我的代码找不到 google_application_credentials,这是错误:

应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,它们就可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。请参阅https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。

代码如下:

using System;
using Google.Cloud.Vision.V1;
namespace Test
{
    public class Vision
    {
        public static void Main(String[] args)
        {
            // Instantiates a client
            var client = ImageAnnotatorClient.Create();
            // Load the image file into memory
            var image = Image.FromFile("wakeupcat.jpg");
            // Performs label detection on the image file
            var response = client.DetectLabels(image);
            foreach (var annotation in response)
            {
                if (annotation.Description != null)
                    Console.WriteLine(annotation.Description);
            }
        }
    }
}

文档(https://cloud.google.com/vision/docs/reference/libraries) 告诉我导出环境变量 GOOGLE_APPLICATION_CREDENTIALS 这就是我所做的。但我仍然得到上面的错误。请帮忙!

【问题讨论】:

    标签: c# monodevelop google-vision


    【解决方案1】:

    从谷歌云视觉的服务账户下载JSON文件,放入本地系统,然后检查。这将起作用。

    using System;
    using Google.Cloud.Vision.V1;
    namespace Test
    {
        public class Vision
        {
            public static void Main(String[] args)
            {
                // set this environment
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\Users\user\Desktop\....\.json");
                // Instantiates a client
                var client = ImageAnnotatorClient.Create();
                // Load the image file into memory
                var image = Image.FromFile("wakeupcat.jpg");
                // Performs label detection on the image file
                var response = client.DetectLabels(image);
                foreach (var annotation in response)
                {
                    if (annotation.Description != null)
                        Console.WriteLine(annotation.Description);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多