API Google Vision Document_Text_Detection - PullRequest
       44

API Google Vision Document_Text_Detection

0 голосов
/ 10 декабря 2018

Я пытаюсь разработать функцию API Google Vision на C #.

код должен скомпилироваться в dll, и он должен быть выполнен для выполнения следующих шагов.

  1. получить изображениеиз изображения Путь.
  2. отправить изображение в Google vision api
  3. Вызвать функцию определения текста документа
  4. получить возвращаемое значение (значения текстовой строки)
  5. Done

Когда я запускаю dll, Тем не менее, он продолжает выдавать ошибку исключения исключения.Я предполагаю, что проблема в учетных данных Google, но не уверен ...

Может кто-нибудь помочь мне с этим?Я даже не знаю, что 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...";
        }
    }
}

Ответы [ 3 ]

0 голосов
/ 04 февраля 2019

Вы должны указать имя файла 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

0 голосов
/ 23 апреля 2019

Вам необходимо настроить свою среду в консоли с помощью следующего кода:

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

Linux Server: export GOOGLE_APPLICATION_CREDENTIALS="File Path"

Надеюсь, это поможет!

0 голосов
/ 22 января 2019

Вам просто нужно установить переменную окружения GOOGLE_APPLICATION_CREDENTIALS, как указано здесь

...