Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Created May 11, 2018 18:31
Show Gist options
  • Save adamcrussell/e82d2ad48f9a8b39884e3272e8111b73 to your computer and use it in GitHub Desktop.
Save adamcrussell/e82d2ad48f9a8b39884e3272e8111b73 to your computer and use it in GitHub Desktop.
Google Vision API Annotate (OCR) from Perl
use strict;
use warnings;
use JSON;
use MIME::Base64;
use REST::Client;
use constant API_KEY => "KEY TEXT";
use constant URL => "https://vision.googleapis.com/v1/images:annotate?key=";
my $request_url = URL.API_KEY;
my $client = new REST::Client();
my $json = new JSON();
my $png;
{ local $/ = undef;
open(PNG, $ARGV[0]) or die "Could not open the file: $!";
binmode PNG;
$png = <PNG>;
close(PNG);
$png = encode_base64($png);
}
my %request;
$request{requests} = [
{image => {content => $png},
features => [{type => "DOCUMENT_TEXT_DETECTION"}]}
];
my $json_text = $json->encode(\%request);
$client->POST($request_url, $json_text);
##
# resonse code is in $client->responseCode();
# respose content is in $client->responseContent();
##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment