Module: SemaApiRuby::Client::Ocr

Included in:
SemaApiRuby::Client
Defined in:
lib/sema_api_ruby/client/ocr.rb

Instance Method Summary collapse

Instance Method Details

#post_ocr_image(_file_path) ⇒ Object



33
34
35
36
37
38
# File 'lib/sema_api_ruby/client/ocr.rb', line 33

def post_ocr_image(_file_path)
  post_body = ocr_settings.merge(
    file: Faraday::UploadIO.new(_file_path, mime_type(_file_path))
  )
  conn_with_auth.post("/api/#{api_version}/ocr/", post_body)
end

#text_to_image_ratio(_file_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sema_api_ruby/client/ocr.rb', line 9

def text_to_image_ratio(_file_path)
  response = post_ocr_image(_file_path)

  body = JSON.parse(response.body)

  raise SemaApiRuby::Client::EmptyResponse unless body['frames']

  return 0.0 if body['frames'].empty?

  rectangles = body['frames'].first['results']

  SemaApiRuby::GetTextToImageRatio.call(image_size(_file_path), rectangles)

# workaround for bug in sema media api where they are returning invalid json
# when an image does not have any text results. Remove when they start returning
# valid json
rescue JSON::ParserError => e
  if e.message =~ /unexpected token at '(\]\})+'/
    0.0
  else
    raise e
  end
end