gpt4 book ai didi

c# - Google Vision API 指定 JSON 文件

转载 作者:行者123 更新时间:2023-11-30 23:10:23 35 4
gpt4 key购买 nike

我正在尝试使用 JSON 文件向 Google Vision API 进行身份验证。通常,我使用 GOOGLE_APPLICATION_CREDENTIALS 环境变量来执行此操作,该变量指定 JSON 文件本身的路径。

但是,我需要在我的应用程序本身中指定它并使用 JSON 文件内容进行身份验证。

现在,我尝试指定 CallSettings,然后将其作为参数传递给 ImageAnnotatorClient.Create 方法。果然,CallSettings 对象可以通过从 JSON 文件中读取身份验证信息来完美创建,但是将它作为参数传递给 ImageAnnotatorClient 似乎没有任何区别,因为ImageAnnotatorClient.Create方法还在寻找环境变量,抛出InvalidOperation异常,说明找不到环境变量。

知道如何获得所需的行为吗?

Google Vision Docs

最佳答案

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);
}
}
}
}

关于c# - Google Vision API 指定 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372938/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com