|
<< Click to Display Table of Contents >> OCR com Google Vision |
![]() ![]()
|
Tutorial
https://cloud.google.com/vision/docs/libraries
Primeiro você deve criar uma permissão de acesso ao Google Cloud Plataform
No final você vai ter um arquivo json.
Você precisa ativar a API e ativar o recurso de pagamento. (1 ano grátis para testar).
Tools > NuGet
Install-Package Google.Cloud.Vision.V1 -Pre
Programa teste detectando um texto de cupom fiscal (console)

using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
class Program
{
static void Main(string[] args)
{
string jsonPath = "s:\\Inno-ebd42bc33c15.json";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile("s:\\a.jpg");
var response = client.DetectText(image);
if (response.Count > 0)
{
string[] texto = response[0].Description.Split('\n');
foreach (string s in texto)
{
Console.WriteLine(s);
}
}
Console.WriteLine("done!");
Console.ReadLine();
}
}
Saída
MOSTEC SUPERMERCADOS LTDA
RUA: HILARIO DE SANTOS, 681
BAIRRO: CENTRO CEP: 83302-080 BIO BONIC - C
CNPJ: 31.611.210/0001-30
IE :
212.124.130
CO0:000592792
1570172020 18:16:01 CCF:000350773
CUPOM FISCAL
DESCRICAO
VL UNIT (RS)
ITEM CODIGO
QTD. UN.
001 00000000001097 PAO BATATA B11111
0,39 KG X 12,90
TOTAL
Cartao Debito
:496472592 :465 :5
HD-5: 20F2C90CF82C0780719EE60F9BB171F1
Trib aprox Federal Rs0,21(4.178) Estadual R$0,35(6.
95%) Municipal R$0,00(08)
FONTE: IBPT 5oi7eW
Loja 2
Operador 9838 DEBORA PEREIRA
VL ITEMCRS)
ST
5,03c
5,03
5.03
T12,00%
R$
Op:
9838
032FFLCnZKtIkhijiSufX90yM+LSs¥A9iSHEncOK8/C8=
i
MARCA:EPSON
ECF-IF VERSAO:01.02.01 ECF:034
8.f1114814
MOD.:TM-1900F
LJ:0002
15/01/2020 18:16:24
04151215100
FAB:EP122220000000004288
00000004288 00035077315 01202083306
BR
Detectando o que está numa imagem

using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Vision.V1;
using Grpc.Auth;
class Program
{
static void Main(string[] args)
{
string jsonPath = "d:\\arquivo.json [ caminho para seu arquivo json ]";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(ImageAnnotatorClient.DefaultScopes);
var channel = new Grpc.Core.Channel(ImageAnnotatorClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var client = ImageAnnotatorClient.Create(channel);
var image = Image.FromFile("d:\\wakeupcat.jpg");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
{
Console.WriteLine(annotation.Description);
}
}
Console.WriteLine("done!");
Console.ReadLine();
}
}
Saída:
Cat
Mammal
Vertebrate
Small to medium-sized cats
Whiskers
Felidae
Carnivore
Tabby cat
Domestic short-haired cat
European shorthair
done!