Google Vision API Document_Text_Detection

只谈情不闲聊 提交于 2021-02-10 14:53:22

问题


I am trying to develop C# Google Vision API function.

the code is supposed to compile into dll and it should run to do the following steps.

  1. get the image from the image Path.
  2. send the image to Google vision api
  3. Call the document text detection function
  4. get the return value (text string values)
  5. Done

When I run the dll, However, it keeps giving me an throw exception error. I am assuming that the problem is on the google credential but not sure...

Could somebody help me out with this? I don't even know that the var credential = GoogleCredential.FromFile(Credential_Path); would be the right way to call the json file...

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...";
        }
    }
}

回答1:


You just need to setup the environment variable GOOGLE_APPLICATION_CREDENTIALS as mentioned here




回答2:


You mus have to mention you json file name in the environment variable as this.

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Your_Json_File_Name.json");

Your code would look like this.

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...";
        }
    }
}

or you can send it through your Credential_Path variable.

for more details please visit Google Vision API Docs




回答3:


You need to setup your environment in your console with code like this :

Windows Server: $env:GOOGLE_APPLICATION_CREDENTIALS="File Path"

Linux Server : export GOOGLE_APPLICATION_CREDENTIALS="File Path"

Hope it helps!



来源:https://stackoverflow.com/questions/53701338/google-vision-api-document-text-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!