我正在尝试开发C#功能。
代码应该编译成dll,并且应该运行它来执行以下步骤。
但是,当我运行dll时,它总是给我一个抛出异常错误。我假设问题在谷歌认证上,但不确定.
有人能帮我解决这个问题吗?我甚至不知道var凭据= GoogleCredential.FromFile(Credential_Path);是调用json文件的正确方式.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Vision.V1;
using Google.Apis.Auth.OAuth2;
using Image = Google.Cloud.Vision.V1.Image;
namespace DLL_TEST_NetFramework4._6._1version
{
public class Class1
{
public string doc_text_dection(string GVA_File_Path, string Credential_Path)
{
var credential = GoogleCredential.FromFile(Credential_Path);
//Load the image file into memory
var image = Image.FromFile(GVA_File_Path);
// Instantiates a client
ImageAnnotatorClient client = ImageAnnotatorClient.Create();
TextAnnotation text = client.DetectDocumentText(image);
//Console.WriteLine($"Text: {text.Text}");
return $"Text: {text.Text}";
//return "test image...";
}
}
}发布于 2019-01-22 08:00:08
您只需要设置环境变量GOOGLE_APPLICATION_CREDENTIALS,就像前面提到的这里
发布于 2019-02-04 02:48:24
您必须在环境变量中提到json文件名,如下所示。
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Your_Json_File_Name.json");你的代码会像这样。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Vision.V1;
using Google.Apis.Auth.OAuth2;
using Image = Google.Cloud.Vision.V1.Image;
namespace DLL_TEST_NetFramework4._6._1version
{
public class Class1
{
public string doc_text_dection(string GVA_File_Path, string Credential_Path)
{
//var credential = GoogleCredential.FromFile(Credential_Path);
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Your_Json_File_Name.json");
//Load the image file into memory
var image = Image.FromFile(GVA_File_Path);
// Instantiates a client
ImageAnnotatorClient client = ImageAnnotatorClient.Create();
TextAnnotation text = client.DetectDocumentText(image);
//Console.WriteLine($"Text: {text.Text}");
return $"Text: {text.Text}";
//return "test image...";
}
}
}或者您可以通过Credential_Path变量发送它。
欲知更多详情,请访问文档
发布于 2019-04-23 01:25:43
您需要使用如下代码在控制台中设置您的环境:
Windows:$env:GOOGLE_APPLICATION_CREDENTIALS="File Path"
Linux:export GOOGLE_APPLICATION_CREDENTIALS="File Path"
希望能帮上忙!
https://stackoverflow.com/questions/53701338
复制相似问题