【问题标题】:Google Vision API Document_Text_DetectionGoogle Vision API Document_Text_Detection
【发布时间】:2019-05-11 02:20:32
【问题描述】:

我正在尝试开发 C# Google Vision API 功能。

代码应该编译成 dll,它应该运行以执行以下步骤。

  1. 从图片路径中获取图片。
  2. 将图片发送到 Google vision api
  3. 调用文档文本检测函数
  4. 获取返回值(文本字符串值)
  5. 完成

当我运行 dll 时,它一直给我抛出异常错误。我假设问题出在谷歌凭证上,但不确定......

有人可以帮我解决这个问题吗?我什至不知道 var credential = 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...";
        }
    }
}

【问题讨论】:

    标签: c# .net google-authentication .net-framework-version google-vision


    【解决方案1】:

    您只需要设置环境变量GOOGLE_APPLICATION_CREDENTIALS,如here所述

    【讨论】:

      【解决方案2】:

      你必须在环境变量中提到你的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 变量发送它。

      更多详情请访问Google Vision API Docs

      【讨论】:

        【解决方案3】:

        您需要使用如下代码在控制台中设置环境:

        Windows 服务器: $env:GOOGLE_APPLICATION_CREDENTIALS="File Path"

        Linux 服务器: export GOOGLE_APPLICATION_CREDENTIALS="File Path"

        希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-25
          • 2020-03-21
          • 1970-01-01
          • 2021-03-01
          • 2017-07-07
          • 1970-01-01
          • 2016-06-10
          相关资源
          最近更新 更多