Class: Azure_API

Inherits:
Object
  • Object
show all
Defined in:
lib/xtractor/request.rb

Instance Method Summary collapse

Instance Method Details

#request_API(api_key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xtractor/request.rb', line 6

def request_API(api_key)
    uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr')
    uri.query = URI.encode_www_form({

      'language' => 'en',
      'detectOrientation ' => 'true'
    })

    request = Net::HTTP::Post.new(uri.request_uri)

    request['Ocp-Apim-Subscription-Key'] = api_key

    request['Content-Type'] = 'application/octet-stream'

    collect = Hash.new

    all_files = Dir.glob("cell-files/*.{jpg,png,gif}").sort

    all_files.each do |crop_image|
        request.body = File.binread("#{crop_image}")

        response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request(request)
        end

        collect[crop_image[0..-5]]= JSON.parse(response.body)

        initial_res = collect[crop_image[0..-5]].dig("regions",0,"lines",0,"words",0,"text").to_s
        mid_res = collect[crop_image[0..-5]].dig("regions",0,"lines",0,"words",1,"text").to_s
        final_res = collect[crop_image[0..-5]].dig("regions",0,"lines",0,"words",2,"text").to_s

        collect[crop_image[0..-5]] = initial_res + ' ' + mid_res + ' ' + final_res

        puts collect[crop_image[0..-5]]

        File.open("cell-files/#{crop_image[11..-5]}.txt", "w") do |data|
            data.write(collect[crop_image[0..-5]])
        end
    end
end