-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.rb
More file actions
36 lines (28 loc) · 1.42 KB
/
Copy pathanalyze.rb
File metadata and controls
36 lines (28 loc) · 1.42 KB
1
2
3
4
5
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
# frozen_string_literal: true
# The smallest useful call, and how to read what comes back.
#
# export VISION_API_KEY=sk_live_…
# ruby examples/analyze.rb invoice.pdf [preset]
require "json"
require "vision_api"
path = ARGV[0] || "invoice.pdf"
preset = ARGV[1] || "auto"
vision = VisionAPI.new
res = vision.analyze(file: path, preset: preset)
puts "request #{res['id']} — #{res['pages']} page(s), " \
"#{res['credits_used']} credit(s), #{res['credits_remaining']} left"
# Under preset: "auto" the API tells you what it decided the file was, for free.
if (detection = res["detection"])
note = detection["fallback"] ? " — fallback, so treat the shape as unknown" : ""
puts "detected #{detection['preset']} (#{detection['confidence']})#{note}"
detection["alternatives"].each do |alt|
puts " runner-up: #{alt['preset']} (#{alt['confidence']}) — #{alt['reason']}"
end
end
# unwrap strips the {value, confidence} wrappers; drop_null leaves out the fields the
# document did not carry, which for a 37-field preset is usually most of them.
puts JSON.pretty_generate(VisionAPI::Result.unwrap(res["result"], drop_null: true))
# The two readings worth having in production: what was absent, and what was found but
# weakly. The second is your review queue.
puts "absent: #{VisionAPI::Result.missing(res['result']).join(', ')}"
puts "below high confidence: #{VisionAPI::Result.below_confidence(res['result'], 'high').join(', ')}"