我正在尝试使用googles vision api和我的c#项目。我正在使用他们提供的测试代码但由于某些原因我的代码无法在此处找到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 这就是我的工作。但我仍然得到上面的错误。请帮忙!
答案 0 :(得分:0)
从Google Cloud vision中的服务帐户下载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);
}
}
}
}