Google Vision API Document_Text_Detection

Refresh

March 2019

Views

105 time

1

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

2 answers

2

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

0

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