-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.mjs
More file actions
34 lines (27 loc) · 1.48 KB
/
Copy pathanalyze.mjs
File metadata and controls
34 lines (27 loc) · 1.48 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
/**
* The smallest useful call, and how to read what comes back.
*
* export VISION_API_KEY=sk_live_…
* node examples/analyze.mjs invoice.pdf [preset]
*/
import { VisionAPI, belowConfidence, missing, unwrap } from '@devrobotlabs/visionapi';
const [path = 'invoice.pdf', preset = 'auto'] = process.argv.slice(2);
const vision = new VisionAPI();
const res = await vision.analyze({ file: path, preset });
console.log(`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 (res.detection) {
console.log(`detected ${res.detection.preset} (${res.detection.confidence})` +
(res.detection.fallback ? ' — fallback, so treat the shape as unknown' : ''));
for (const alt of res.detection.alternatives) {
console.log(` runner-up: ${alt.preset} (${alt.confidence}) — ${alt.reason}`);
}
}
// `unwrap` strips the { value, confidence } wrappers; dropNull leaves out the fields the
// document did not carry, which for a 37-field preset is usually most of them.
console.log(JSON.stringify(unwrap(res.result, { dropNull: true }), null, 2));
// The two readings worth having in production: what was absent, and what was found but
// weakly. The second is your review queue.
console.log('absent:', missing(res.result).join(', ') || '(nothing)');
console.log('below "high" confidence:', belowConfidence(res.result, 'high').join(', ') || '(nothing)');