Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Formrecognizer samples #12987

Merged
merged 4 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions sdk/formrecognizer/azure-ai-formrecognizer/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ public static void main(String[] args) throws IOException {
List<RecognizedForm> formsWithLabeledModel =
client.beginRecognizeCustomForms(
new FileInputStream(analyzeFile), analyzeFile.length(),
"{labeled_model_Id}", new RecognizeOptions()
"{labeled_model_Id}",
new RecognizeOptions()
.setContentType(FormContentType.APPLICATION_PDF)
.setIncludeFieldElements(true)
.setPollInterval(Duration.ofSeconds(5)))
.getFinalResult();

List<RecognizedForm> formsWithUnlabeledModel =
client.beginRecognizeCustomForms(new FileInputStream(analyzeFile), analyzeFile.length(),
"{unlabeled_model_Id}", new RecognizeOptions()
"{unlabeled_model_Id}",
new RecognizeOptions()
.setContentType(FormContentType.APPLICATION_PDF)
.setIncludeFieldElements(true)
.setPollInterval(Duration.ofSeconds(5)))
Expand Down Expand Up @@ -109,15 +111,14 @@ public static void main(String[] args) throws IOException {
String.format("[%.2f, %.2f]", point.getX(), point.getY())).forEach(boundingBoxStr::append);
}

final StringBuilder boundingBoxLabelStr = new StringBuilder();
if (formField.getLabelData() != null && formField.getLabelData().getBoundingBox() != null) {
formField.getLabelData().getBoundingBox().getPoints().stream().map(point ->
String.format("[%.2f, %.2f]", point.getX(), point.getY())).forEach(boundingBoxStr::append);
}
System.out.printf("Field %s has label %s within bounding box %s with a confidence score "
+ "of %.2f.%n",
label, formField.getLabelData().getText(), boundingBoxLabelStr, formField.getConfidence());

System.out.printf("Field %s has label %s within bounding box %s with a confidence score "
+ "of %.2f.%n",
label, formField.getLabelData().getText(), "", formField.getConfidence());
}
System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence score "
+ "of %.2f.%n",
label, formField.getValue(), formField.getValueData().getText(), boundingBoxStr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static void main(String[] args) throws IOException {

PollerFlux<OperationResult, List<RecognizedForm>> labeledCustomFormPoller =
client.beginRecognizeCustomForms(toFluxByteBuffer(new ByteArrayInputStream(fileContent)),
analyzeFile.length(), "{labeled_model_Id}", new RecognizeOptions()
analyzeFile.length(), "{labeled_model_Id}",
new RecognizeOptions()
.setContentType(FormContentType.APPLICATION_PDF)
.setIncludeFieldElements(true)
.setPollInterval(Duration.ofSeconds(5)));
Expand All @@ -71,25 +72,24 @@ public static void main(String[] args) throws IOException {

Mono<List<RecognizedForm>> labeledDataResult = labeledCustomFormPoller
.last()
.flatMap(trainingOperationResponse -> {
if (trainingOperationResponse.getStatus().isComplete()) {
.flatMap(pollResponse -> {
if (pollResponse.getStatus().isComplete()) {
// training completed successfully, retrieving final result.
return trainingOperationResponse.getFinalResult();
return pollResponse.getFinalResult();
} else {
return Mono.error(new RuntimeException("Polling completed unsuccessfully with status:"
+ trainingOperationResponse.getStatus()));
+ pollResponse.getStatus()));
}
});

Mono<List<RecognizedForm>> unlabeledDataResult = unlabeledCustomFormPoller
.last()
.flatMap(trainingOperationResponse -> {
if (trainingOperationResponse.getStatus().isComplete()) {
// training completed successfully, retrieving final result.
return trainingOperationResponse.getFinalResult();
.flatMap(pollResponse -> {
if (pollResponse.getStatus().isComplete()) {
return pollResponse.getFinalResult();
} else {
return Mono.error(new RuntimeException("Polling completed unsuccessfully with status:"
+ trainingOperationResponse.getStatus()));
+ pollResponse.getStatus()));
}
});

Expand Down Expand Up @@ -146,14 +146,14 @@ public static void main(String[] args) throws IOException {
String.format("[%.2f, %.2f]", point.getX(), point.getY())).forEach(boundingBoxStr::append);
}

final StringBuilder boundingBoxLabelStr = new StringBuilder();
if (formField.getLabelData() != null && formField.getLabelData().getBoundingBox() != null) {
formField.getLabelData().getBoundingBox().getPoints().stream().map(point ->
String.format("[%.2f, %.2f]", point.getX(), point.getY())).forEach(boundingBoxStr::append);

System.out.printf("Field %s has label %s within bounding box %s with a confidence score "
+ "of %.2f.%n",
label, formField.getLabelData().getText(), "", formField.getConfidence());
}
System.out.printf("Field %s has label %s within bounding box %s with a confidence score "
+ "of %.2f.%n",
label, formField.getLabelData().getText(), boundingBoxLabelStr, formField.getConfidence());

System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence "
+ "score of %.2f.%n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public class Authentication {
*
*/
public static void main(String[] args) {
/**
* Set the environment variables with your own values before running the sample:
* AZURE_CLIENT_ID - the client ID of your active directory application.
* AZURE_TENANT_ID - the tenant ID of your active directory application.
* AZURE_CLIENT_SECRET - the secret of your active directory application.
/*
Set the environment variables with your own values before running the sample:
AZURE_CLIENT_ID - the client ID of your active directory application.
AZURE_TENANT_ID - the tenant ID of your active directory application.
AZURE_CLIENT_SECRET - the secret of your active directory application.
*/

// Form recognizer client: Key credential
Expand Down Expand Up @@ -92,10 +92,10 @@ private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRe
for (int i = 0; i < receiptPageResults.size(); i++) {
RecognizedForm recognizedForm = receiptPageResults.get(i);
Map<String, FormField<?>> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized Receipt page %d -----------%n", i);
System.out.printf("----------- Recognizing receipt info for page %d -----------%n", i);
FormField<?> merchantNameField = recognizedFields.get("MerchantName");
if (merchantNameField != null) {
if (FieldValueType.STRING.equals(merchantNameField.getValueType())) {
if (FieldValueType.STRING == merchantNameField.getValueType()) {
String merchantName = FieldValueType.STRING.cast(merchantNameField);
System.out.printf("Merchant Name: %s, confidence: %.2f%n",
merchantName, merchantNameField.getConfidence());
Expand All @@ -104,7 +104,7 @@ private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRe

FormField<?> merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
if (merchantPhoneNumberField != null) {
if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) {
if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValueType()) {
String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField);
System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
merchantAddress, merchantPhoneNumberField.getConfidence());
Expand All @@ -113,7 +113,7 @@ private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRe

FormField<?> transactionDateField = recognizedFields.get("TransactionDate");
if (transactionDateField != null) {
if (FieldValueType.DATE.equals(transactionDateField.getValueType())) {
if (FieldValueType.DATE == transactionDateField.getValueType()) {
LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField);
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
transactionDate, transactionDateField.getConfidence());
Expand All @@ -123,29 +123,27 @@ private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRe
FormField<?> receiptItemsField = recognizedFields.get("Items");
if (receiptItemsField != null) {
System.out.printf("Receipt Items: %n");
if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) {
if (FieldValueType.LIST == receiptItemsField.getValueType()) {
List<FormField<?>> receiptItems = FieldValueType.LIST.cast(receiptItemsField);
receiptItems.forEach(receiptItem -> {
if (FieldValueType.MAP.equals(receiptItem.getValueType())) {
Map<String, FormField<?>> formFieldMap = FieldValueType.MAP.cast(receiptItem);
formFieldMap.forEach((key, formField) -> {
if ("Name".equals(key)) {
if (FieldValueType.STRING.equals(formField.getValueType())) {
String name = FieldValueType.STRING.cast(formField);
System.out.printf("Name: %s, confidence: %.2fs%n",
name, formField.getConfidence());
}
receiptItems.stream()
.filter(receiptItem -> FieldValueType.MAP == receiptItem.getValueType())
.<Map<String, FormField<?>>>map(FieldValueType.MAP::cast)
.forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
if ("Name".equals(key)) {
if (FieldValueType.STRING == formField.getValueType()) {
String name = FieldValueType.STRING.cast(formField);
System.out.printf("Name: %s, confidence: %.2fs%n",
name, formField.getConfidence());
}
if ("Quantity".equals(key)) {
if (FieldValueType.DOUBLE.equals(formField.getValueType())) {
Float quantity = FieldValueType.DOUBLE.cast(formField);
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
}
if ("Quantity".equals(key)) {
if (FieldValueType.DOUBLE == formField.getValueType()) {
Float quantity = FieldValueType.DOUBLE.cast(formField);
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
});
}
});
}
}));
}
}
System.out.print("-----------------------------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.ai.formrecognizer.training.FormTrainingAsyncClient;
import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.AsyncPollResponse;

import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -42,7 +43,8 @@ public static void main(final String[] args) {
// Start copy operation from the source client
// The Id of the model that needs to be copied to the target resource
.subscribe(copyAuthorization -> sourceClient.beginCopyModel(copyModelId, copyAuthorization)
.subscribe(copyPoller -> copyPoller.getFinalResult()
.filter(pollResponse -> pollResponse.getStatus().isComplete())
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(customFormModelInfo -> {
System.out.printf("Original model has model Id: %s, model status: %s, training started on: %s,"
+ " training completed on: %s.%n",
Expand All @@ -59,7 +61,7 @@ public static void main(final String[] args) {
customFormModel.getModelStatus(),
customFormModel.getTrainingStartedOn(),
customFormModel.getTrainingCompletedOn()));
})));
}));

// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
Expand Down
Loading