-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
StronglyTypedRecognizedForm.java
79 lines (72 loc) · 4.22 KB
/
StronglyTypedRecognizedForm.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.formrecognizer;
import com.azure.ai.formrecognizer.models.OperationResult;
import com.azure.ai.formrecognizer.models.RecognizedForm;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;
import java.util.List;
/**
* Sample demonstrating converting recognized form fields to strongly typed US receipt field values.
* See
* <a href="https://aka.ms/azsdk/python/formrecognizer/receiptfields"></a>
* for information on the strongly typed fields returned by service when recognizing receipts.
*/
public class StronglyTypedRecognizedForm {
/**
* Main method to invoke this demo.
*
* @param args Unused. Arguments to the program.
*/
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerClient client = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{api_Key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();
String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer"
+ "/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg";
SyncPoller<OperationResult, List<RecognizedForm>> recognizeReceiptPoller =
client.beginRecognizeReceiptsFromUrl(receiptUrl);
List<RecognizedForm> receiptPageResults = recognizeReceiptPoller.getFinalResult();
for (int i = 0; i < receiptPageResults.size(); i++) {
final RecognizedForm recognizedForm = receiptPageResults.get(i);
System.out.printf("----------- Recognized Receipt page %d -----------%n", i);
// Use Receipt model transform the recognized form to strongly typed US receipt fields
Receipt usReceipt = new Receipt(recognizedForm);
System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getValue(),
usReceipt.getMerchantName().getConfidence());
System.out.printf("Merchant Address: %s, confidence: %.2f%n",
usReceipt.getMerchantAddress().getValue(),
usReceipt.getMerchantAddress().getConfidence());
System.out.printf("Merchant Phone Number %s, confidence: %.2f%n",
usReceipt.getMerchantPhoneNumber().getValue(), usReceipt.getMerchantPhoneNumber().getConfidence());
System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getValue(),
usReceipt.getTotal().getConfidence());
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
usReceipt.getTransactionDate().getValue(), usReceipt.getTransactionDate().getConfidence());
System.out.printf("Transaction Time: %s, confidence: %.2f%n",
usReceipt.getTransactionTime().getValue(), usReceipt.getTransactionTime().getConfidence());
System.out.printf("Receipt Items: %n");
usReceipt.getReceiptItems().forEach(receiptItem -> {
if (receiptItem.getName() != null) {
System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getValue(),
receiptItem.getName().getConfidence());
}
if (receiptItem.getQuantity() != null) {
System.out.printf("Quantity: %f, confidence: %.2f%n", receiptItem.getQuantity().getValue(),
receiptItem.getQuantity().getConfidence());
}
if (receiptItem.getPrice() != null) {
System.out.printf("Price: %f, confidence: %.2f%n", receiptItem.getPrice().getValue(),
receiptItem.getPrice().getConfidence());
}
if (receiptItem.getTotalPrice() != null) {
System.out.printf("Total Price: %f, confidence: %.2f%n",
receiptItem.getTotalPrice().getValue(), receiptItem.getTotalPrice().getConfidence());
}
});
System.out.print("-----------------------------------");
}
}
}