module VisionUtils
Public Instance Methods
get_word_layout(_url, _api_key, timeout: 30)
click to toggle source
# File lib/utils/vision_utils.rb, line 7 def get_word_layout(_url, _api_key, timeout: 30) extract_word_layout handle_response conn(timeout).post( "/v1/images:annotate?key=#{_api_key}", 'requests' => [ { 'image' => build_image_source(_url), 'features' => [ { 'type' => 'DOCUMENT_TEXT_DETECTION' } ] } ] ) end
Private Instance Methods
build_image_source(_url)
click to toggle source
# File lib/utils/vision_utils.rb, line 23 def build_image_source(_url) uri = URI(_url) if uri_is_local(uri) { 'content' => Base64.encode64(uri.open(&:read)) } else { 'source' => { 'imageUri': _url } } end end
conn(_timeout)
click to toggle source
# File lib/utils/vision_utils.rb, line 91 def conn(_timeout) @conn ||= Faraday.new(url: "https://vision.googleapis.com") do |faraday| faraday.request :json faraday.response :json faraday.adapter :patron faraday.options.timeout = _timeout faraday.options.open_timeout = _timeout end end
extract_bounding_box(_vertices)
click to toggle source
# File lib/utils/vision_utils.rb, line 58 def extract_bounding_box(_vertices) [].tap do |bounding_box| _vertices.each do |vertex| if vertex['x'] == nil || vertex['y'] == nil return nil end bounding_box << [vertex['x'], vertex['y']] end end end
extract_word(_raw_word)
click to toggle source
# File lib/utils/vision_utils.rb, line 70 def extract_word(_raw_word) wtext = _raw_word['symbols'].map { |sym| sym['text'] }.join confidence = _raw_word['symbols'].map { |sym| sym['confidence'].to_f }.min vertices = _raw_word['boundingBox']['vertices'] bounding_box = extract_bounding_box(vertices) if bounding_box == nil return nil end [wtext, bounding_box, confidence] end
extract_word_layout(_data)
click to toggle source
# File lib/utils/vision_utils.rb, line 38 def extract_word_layout(_data) image_text = _data['responses'].first['fullTextAnnotation'] return [] if image_text.nil? [].tap do |result| image_text['pages'].each do |page| page['blocks'].each do |block| next unless block['blockType'] == 'TEXT' block['paragraphs'].each do |para| para['words'].each do |w| extracted_word = extract_word(w) result << extracted_word if extracted_word end end end end end end
handle_response(_response)
click to toggle source
# File lib/utils/vision_utils.rb, line 83 def handle_response(_response) if _response.status != 200 && _response.status != 201 raise 'Service error' end _response.body end
uri_is_local(_uri)
click to toggle source
# File lib/utils/vision_utils.rb, line 32 def uri_is_local(_uri) return true if _uri.scheme != 'http' && _uri.scheme != 'https' _uri.host == 'localhost' || _uri.host == '127.0.0.1' end