Sha256: 9cb55bccc75300a32cbec5767cd2f7a2ea143e34ebe770d92daf9c37a3b0b9be

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "character_recognition_by_vision_api/version"
require 'rest-client'
require 'base64'
require 'json'

module CharacterRecognitionByVisionApi
  extend self

  def extract_to_text(image_file, api_key)
    JSON.parse(response_json(image_file, api_key))['responses'][0]['fullTextAnnotation']['text']
  end

  def payload(image_file)
    {
      requests: [
        {
          image:
            {
              content: Base64.encode64(open(image_file).read)
            },
          features: [
            {
              type: "TEXT_DETECTION",
              maxResults: 10,
            }
          ]
        }
      ]
    }
  end

  def headers_for_post
    {
      'Content-Type': 'application/json',
    }
  end

  def request_uri(api_key)
    "https://vision.googleapis.com/v1/images:annotate?key=#{api_key}"
  end

  def response_json(image_file, api_key)
    begin
      RestClient.post(
        request_uri(api_key),
        payload(image_file).to_json,
        headers_for_post,
      )
    rescue => e
      puts e
      exit(1)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
character_recognition_by_vision_api-0.1.0 lib/character_recognition_by_vision_api.rb