Skip to content

Commit

Permalink
rename sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Jul 14, 2020
1 parent 352b559 commit 31c0f7e
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 281 deletions.
47 changes: 24 additions & 23 deletions sdk/formrecognizer/azure-ai-formrecognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ resource, or by running the following Azure CLI command to get the key from the
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
```
Use the API key as the credential parameter to authenticate the client:
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L48-L51 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L47-L50 -->
```java
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
```
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L58-L61 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L57-L60 -->
```java
FormTrainingClient formTrainingClient = new FormTrainingClientBuilder()
.credential(new AzureKeyCredential("{key}"))
Expand All @@ -98,7 +98,7 @@ FormTrainingClient formTrainingClient = new FormTrainingClientBuilder()

The Azure Form Recognizer client library provides a way to **rotate the existing key**.

<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L68-L74 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L67-L73 -->
```java
AzureKeyCredential credential = new AzureKeyCredential("{key}");
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
Expand Down Expand Up @@ -137,7 +137,7 @@ Authorization is easiest using [DefaultAzureCredential][wiki_identity]. It finds
running environment. For more information about using Azure Active Directory authorization with Form Recognizer, please
refer to [the associated documentation][aad_authorization].

<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L81-L85 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L80-L84 -->
```java
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
Expand Down Expand Up @@ -195,7 +195,7 @@ The following section provides several code snippets covering some of the most c
### Recognize Forms Using a Custom Model
Recognize name/value pairs and table data from forms. These models are trained with your own data,
so they're tailored to your forms. You should only recognize forms of the same form type that the custom model was trained on.
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L89-L105 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L88-L104 -->
```java
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
Expand All @@ -206,7 +206,7 @@ List<RecognizedForm> recognizedForms = recognizeFormPoller.getFinalResult();

for (int i = 0; i < recognizedForms.size(); i++) {
RecognizedForm form = recognizedForms.get(i);
System.out.printf("----------- Recognized Form %d -----------%n", i);
System.out.printf("----------- Recognized custom form info for page %d -----------%n", i);
System.out.printf("Form type: %s%n", form.getFormType());
form.getFields().forEach((label, formField) ->
System.out.printf("Field %s has value %s with confidence score of %f.%n", label,
Expand All @@ -218,7 +218,7 @@ for (int i = 0; i < recognizedForms.size(); i++) {

### Recognize Content
Recognize text and table structures, along with their bounding box coordinates, from documents.
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L114-L137 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L113-L136 -->
```java
// recognize form content using file input stream
File form = new File("local/file_path/filename.png");
Expand All @@ -232,7 +232,7 @@ List<FormPage> contentPageResults = recognizeContentPoller.getFinalResult();

for (int i = 0; i < contentPageResults.size(); i++) {
FormPage formPage = contentPageResults.get(i);
System.out.printf("----Recognizing content for page %d ----%n", i);
System.out.printf("----Recognizing content info for page %d ----%n", i);
// Table information
System.out.printf("Has width: %f and height: %f, measured with unit: %s.%n", formPage.getWidth(),
formPage.getHeight(),
Expand All @@ -247,10 +247,12 @@ for (int i = 0; i < contentPageResults.size(); i++) {
```

### Recognize receipts
Recognize data from a USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the service returns for a recognized receipt. See [StronglyTypedRecognizedForm.java][strongly_typed_sample] for a suggested approach to extract
Recognize data from a USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields
the service returns for a recognized receipt.
See [StronglyTypedRecognizedForm.java][strongly_typed_sample] for a suggested approach to extract
information from receipts.

<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L141-L199-->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L140-L198-->
```java
String receiptUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/media"
+ "/contoso-allinone.jpg";
Expand All @@ -261,7 +263,7 @@ List<RecognizedForm> receiptPageResults = syncPoller.getFinalResult();
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 == merchantNameField.getValueType()) {
Expand Down Expand Up @@ -317,9 +319,9 @@ for (int i = 0; i < receiptPageResults.size(); i++) {
Train a machine-learned model on your own form type. The resulting model will be able to recognize values from the types of forms it was trained on.
Provide a container SAS url to your Azure Storage Blob container where you're storing the training documents. See details on setting this up
in the [service quickstart documentation][quickstart_training].
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L203-L223 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L202-L222 -->
```java
String trainingFilesUrl = "{SAS-URL-of-your-container-in-blob-storage}";
String trainingFilesUrl = "{SAS_URL_of_your_container_in_blob_storage}";
SyncPoller<OperationResult, CustomFormModel> trainingPoller =
formTrainingClient.beginTraining(trainingFilesUrl, false);

Expand All @@ -332,7 +334,7 @@ System.out.printf("Training started on: %s%n", customFormModel.getTrainingStarte
System.out.printf("Training completed on: %s%n%n", customFormModel.getTrainingCompletedOn());

System.out.println("Recognized Fields:");
// looping through the sub-models, which contains the fields they were trained on
// looping through the subModels, which contains the fields they were trained on
// Since the given training documents are unlabeled, we still group them but they do not have a label.
customFormModel.getSubmodels().forEach(customFormSubmodel -> {
// Since the training data is unlabeled, we are unable to return the accuracy of this model
Expand All @@ -344,9 +346,8 @@ customFormModel.getSubmodels().forEach(customFormSubmodel -> {

### Manage your models
Manage the custom models attached to your account.
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L227-L256 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L226-L254 -->
```java
AtomicReference<String> modelId = new AtomicReference<>();
// First, we see how many custom models we have, and what our limit is
AccountProperties accountProperties = formTrainingClient.getAccountProperties();
System.out.printf("The account has %d custom models, and we can have at most %d custom models",
Expand All @@ -357,8 +358,7 @@ PagedIterable<CustomFormModelInfo> customModels = formTrainingClient.listCustomM
System.out.println("We have following models in the account:");
customModels.forEach(customFormModelInfo -> {
System.out.printf("Model Id: %s%n", customFormModelInfo.getModelId());
// get custom model info
modelId.set(customFormModelInfo.getModelId());
// get specific custom model info
CustomFormModel customModel = formTrainingClient.getCustomModel(customFormModelInfo.getModelId());
System.out.printf("Model Status: %s%n", customModel.getModelStatus());
System.out.printf("Training started on: %s%n", customModel.getTrainingStartedOn());
Expand All @@ -374,8 +374,9 @@ customModels.forEach(customFormModelInfo -> {
}
});
});

// Delete Custom Model
formTrainingClient.deleteModel(modelId.get());
formTrainingClient.deleteModel("{modelId}");
```
For more detailed examples, refer to [samples][sample_readme].

Expand All @@ -386,7 +387,7 @@ to provide an invalid file source URL an `HttpResponseException` would be raised
In the following code snippet, the error is handled
gracefully by catching the exception and display the additional information about the error.

<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L263-L267 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L261-L265 -->
```java
try {
formRecognizerClient.beginRecognizeContentFromUrl("invalidSourceUrl");
Expand Down Expand Up @@ -424,7 +425,7 @@ These code samples show common scenario operations with the Azure Form Recognize
#### Async APIs
All the examples shown so far have been using synchronous APIs, but we provide full support for async APIs as well.
You'll need to use `FormRecognizerAsyncClient`
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L274-L277 -->
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L272-L275 -->
```java
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{key}"))
Expand Down Expand Up @@ -491,12 +492,12 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m
[manage_custom_models]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ManageCustomModels.java
[manage_custom_models_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ManageCustomModelsAsync.java
[recognize_content]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContent.java
[recognize_content_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentAsync.java
[recognize_content_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentFromUrlAsync.java
[recognize_receipts]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java
[recognize_receipts_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java
[recognize_receipts_from_url]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java
[recognize_receipts_from_url_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java
[recognize_custom_forms]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomForms.java
[recognize_custom_forms]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsFromUrl.java
[recognize_custom_forms_async]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsAsync.java
[register_AAD_application]: https://docs.microsoft.com/azure/cognitive-services/authentication#assign-a-role-to-a-service-principal
[train_unlabeled_model]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/TrainModelWithoutLabels.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void main(String[] args) throws IOException {
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 "
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static void main(String[] args) throws IOException {
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 "
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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 == merchantNameField.getValueType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@ public void beginTraining() {
String trainingFilesUrl = "{SAS-URL-of-your-container-in-blob-storage}";
boolean useTrainingLabels = true;
formTrainingAsyncClient.beginTraining(trainingFilesUrl, useTrainingLabels)
.subscribe(trainingPollingOperation -> {
// if training polling operation completed, retrieve the final result.
trainingPollingOperation.getFinalResult().subscribe(customFormModel -> {
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
});
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(customFormModel -> {
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
});
// END: com.azure.ai.formrecognizer.training.FormTrainingAsyncClient.beginTraining#string-boolean
}
Expand All @@ -82,17 +81,15 @@ public void beginTrainingWithOptions() {

formTrainingAsyncClient.beginTraining(trainingFilesUrl, true, trainingFileFilter,
Duration.ofSeconds(5))
.subscribe(trainingPollingOperation -> {
// if training polling operation completed, retrieve the final result.
trainingPollingOperation.getFinalResult()
.subscribe(customFormModel -> {
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels().forEach(customFormSubmodel ->
customFormSubmodel.getFields().forEach((key, customFormModelField) ->
System.out.printf("Form Type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
});
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(customFormModel -> {
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels().forEach(customFormSubmodel ->
customFormSubmodel.getFields().forEach((key, customFormModelField) ->
System.out.printf("Form Type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
});
// END: com.azure.ai.formrecognizer.training.FormTrainingAsyncClient.beginTraining#string-boolean-trainingFileFilter-Duration
}
Expand Down
Loading

0 comments on commit 31c0f7e

Please sign in to comment.