Google 视觉 API 指定 JSON 文件
Google Vision API specify JSON file
我正在尝试使用 JSON 文件向 Google Vision API 进行身份验证。通常,我使用 GOOGLE_APPLICATION_CREDENTIALS
环境变量来指定 JSON 文件本身的路径。
但是,我需要在我的应用程序本身中指定它并使用 JSON 文件内容进行身份验证。
现在,我尝试指定 CallSettings
然后将其作为参数传递给 ImageAnnotatorClient.Create
方法。果然,可以通过从 JSON 文件中读取身份验证信息来完美创建 CallSettings
对象,但是将其作为参数传递给 ImageAnnotatorClient
似乎没有任何区别,因为 ImageAnnotatorClient.Create
方法还在寻找环境变量,抛出InvalidOperation
异常,说明找不到环境变量。
知道如何获得所需的行为吗?
using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
namespace GoogleVision
{
class Program
{
static void Main(string[] args)
{
string jsonPath = @"<path to .json credential file>";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile(@"<path to your image file>");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
}
}
var jsonPath = "<YOUR PATH>";
var imageAnnotatorClientBuilder = new ImageAnnotatorClientBuilder
{
CredentialsPath = jsonPath
};
var client = imageAnnotatorClientBuilder.Build();
IReadOnlyList<EntityAnnotation> textAnnotations = client.DetectText(image);
foreach (EntityAnnotation text in textAnnotations)
{
Console.WriteLine($"Description: {text.Description}");
}
我正在尝试使用 JSON 文件向 Google Vision API 进行身份验证。通常,我使用 GOOGLE_APPLICATION_CREDENTIALS
环境变量来指定 JSON 文件本身的路径。
但是,我需要在我的应用程序本身中指定它并使用 JSON 文件内容进行身份验证。
现在,我尝试指定 CallSettings
然后将其作为参数传递给 ImageAnnotatorClient.Create
方法。果然,可以通过从 JSON 文件中读取身份验证信息来完美创建 CallSettings
对象,但是将其作为参数传递给 ImageAnnotatorClient
似乎没有任何区别,因为 ImageAnnotatorClient.Create
方法还在寻找环境变量,抛出InvalidOperation
异常,说明找不到环境变量。
知道如何获得所需的行为吗?
using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
namespace GoogleVision
{
class Program
{
static void Main(string[] args)
{
string jsonPath = @"<path to .json credential file>";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile(@"<path to your image file>");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
Console.WriteLine(annotation.Description);
}
}
}
}
var jsonPath = "<YOUR PATH>";
var imageAnnotatorClientBuilder = new ImageAnnotatorClientBuilder
{
CredentialsPath = jsonPath
};
var client = imageAnnotatorClientBuilder.Build();
IReadOnlyList<EntityAnnotation> textAnnotations = client.DetectText(image);
foreach (EntityAnnotation text in textAnnotations)
{
Console.WriteLine($"Description: {text.Description}");
}