-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyze.java
More file actions
51 lines (41 loc) · 2.11 KB
/
Copy pathAnalyze.java
File metadata and controls
51 lines (41 loc) · 2.11 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
/*
* The smallest useful call, and how to read what comes back.
*
* mvn package
* export VISION_API_KEY=sk_live_…
* java -cp target/visionapi-java-1.0.0.jar examples/Analyze.java invoice.pdf [preset]
*/
import io.visionapi.AnalyzeRequest;
import io.visionapi.AnalyzeResponse;
import io.visionapi.Confidence;
import io.visionapi.ExtractionResult;
import io.visionapi.VisionApi;
import java.nio.file.Path;
public class Analyze {
public static void main(String[] args) {
String path = args.length > 0 ? args[0] : "invoice.pdf";
String preset = args.length > 1 ? args[1] : "auto";
VisionApi vision = VisionApi.create();
AnalyzeResponse res = vision.analyze(AnalyzeRequest.builder()
.file(Path.of(path))
.preset(preset)
.build());
System.out.printf("request %s — %d page(s), %d credit(s), %d left%n",
res.id(), res.pages(), res.creditsUsed(), res.creditsRemaining());
// Under preset "auto" the API tells you what it decided the file was, for free.
res.detection().ifPresent(detection -> {
String note = detection.isFallback() ? " — fallback, so treat the shape as unknown" : "";
System.out.printf("detected %s (%s)%s%n", detection.preset(), detection.confidence(), note);
detection.alternatives().forEach(alt ->
System.out.printf(" runner-up: %s (%s) — %s%n", alt.preset(), alt.confidence(), alt.reason()));
});
ExtractionResult result = res.result();
// A preset response carries every field of the preset, so most of a 37-field invoice
// is usually null. unwrap(true) keeps only what was actually found.
result.unwrap(true).forEach((name, value) -> System.out.println(" " + name + ": " + value));
// The two readings worth having in production: what was absent, and what was found
// but weakly. The second is your review queue.
System.out.println("absent: " + result.missing());
System.out.println("below high confidence: " + result.belowConfidence(Confidence.HIGH));
}
}