-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.php
More file actions
51 lines (41 loc) · 1.73 KB
/
Copy pathanalyze.php
File metadata and controls
51 lines (41 loc) · 1.73 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* The smallest useful call, and how to read what comes back.
*
* export VISION_API_KEY=sk_live_…
* php examples/analyze.php invoice.pdf [preset]
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use VisionApi\Client;
use VisionApi\Result;
$path = $argv[1] ?? 'invoice.pdf';
$preset = $argv[2] ?? 'auto';
$vision = new Client();
$res = $vision->analyze(['file' => $path, 'preset' => $preset]);
printf(
"request %s — %d page(s), %d credit(s), %d left\n",
$res['id'],
$res['pages'],
$res['credits_used'],
$res['credits_remaining']
);
// Under preset "auto" the API tells you what it decided the file was, for free.
if (isset($res['detection'])) {
$detection = $res['detection'];
$note = $detection['fallback'] ? ' — fallback, so treat the shape as unknown' : '';
printf("detected %s (%s)%s\n", $detection['preset'], $detection['confidence'], $note);
foreach ($detection['alternatives'] as $alt) {
printf(" runner-up: %s (%s) — %s\n", $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.
echo json_encode(
Result::unwrap($res['result'] ?? null, dropNull: true),
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
), "\n";
// The two readings worth having in production: what was absent, and what was found but
// weakly. The second is your review queue.
echo 'absent: ', implode(', ', Result::missing($res['result'] ?? null)) ?: '(nothing)', "\n";
echo 'below high confidence: ', implode(', ', Result::belowConfidence($res['result'] ?? null, 'high')) ?: '(nothing)', "\n";