我正在尝试在我的C#项目中使用google的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这就是我所做的。但我仍然收到上面的错误。请帮忙!
从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:UsersuserDesktop.....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);
}
}
}
}