Skip to content

Commit

Permalink
[FR] Add locale tests (#16026)
Browse files Browse the repository at this point in the history
Fixes: #14817

It also removes `US` from receipts as now it accepts multiple locales
  • Loading branch information
maririos authored Oct 16, 2020
1 parent 81790a6 commit 437d9fb
Show file tree
Hide file tree
Showing 20 changed files with 10,049 additions and 8 deletions.
1 change: 1 addition & 0 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### New Features
- Added support for pre-built business card recognition.
- Added support for providing locale info when recognizing receipts and business cards. Supported locales include support EN-US, EN-AU, EN-CA, EN-GB, EN-IN.
- Added support to train and recognize custom forms with selection marks such as check boxes and radio buttons. This functionality is only available in train with labels scenarios.
- Added ability to create a composed model from the `FormTrainingClient` by calling method `StartCreateComposedModel`.
- Added the properties `ModelName` and `Properties` to types `CustomFormModel` and `CustomFormModelInfo`.
Expand Down
14 changes: 10 additions & 4 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ Azure Cognitive Services Form Recognizer is a cloud service that uses machine le

- Recognize Custom Forms - Recognize and extract form fields and other content from your custom forms, using models you trained with your own form types.
- Recognize Form Content - Recognize and extract tables, lines, words, and selection marks like radio buttons and check boxes in forms documents, without the need to train a model.
<<<<<<< HEAD
- Recognize Receipts - Recognize and extract common fields from US receipts, using a pre-trained receipt model.
- Recognize Business Card - Recognize and extract common fields from business cards, using a pre-trained business cards model.
=======
- Recognize Receipts - Recognize and extract common fields from receipts, using a pre-trained receipt model.
>>>>>>> locale tests
[Source code][formreco_client_src] | [Package (NuGet)][formreco_nuget_package] | [API reference documentation][formreco_refdocs] | [Product documentation][formreco_docs] | [Samples][formreco_samples]

Expand Down Expand Up @@ -103,7 +107,7 @@ var client = new FormRecognizerClient(new Uri(endpoint), new DefaultAzureCredent

- Recognizing form fields and content, using custom models trained to recognize your custom forms. These values are returned in a collection of `RecognizedForm` objects. See example [Recognize Custom Forms](#recognize-custom-forms).
- Recognizing form content, including tables, lines, words, and selection marks like radio buttons and check boxes without the need to train a model. Form content is returned in a collection of `FormPage` objects. See example [Recognize Content](#recognize-content).
- Recognizing common fields from US receipts, using a pre-trained receipt model on the Form Recognizer service. These fields and meta-data are returned in a collection of `RecognizedForm` objects. See example [Recognize Receipts](#recognize-receipts).
- Recognizing common fields from receipts, using a pre-trained receipt model on the Form Recognizer service. These fields and meta-data are returned in a collection of `RecognizedForm` objects. See example [Recognize Receipts](#recognize-receipts).
- Recognizing common fields from business cards, using a pre-trained business cards model on the Form Recognizer service. These fields and meta-data are returned in a collection of `RecognizedForm` objects. See example [Recognize Business Cards](#recognize-business-cards).

### FormTrainingClient
Expand Down Expand Up @@ -207,12 +211,13 @@ foreach (RecognizedForm form in forms)
```

### Recognize Receipts
Recognize data from US sales receipts using a prebuilt model. Receipt fields recognized by the service can be found [here][service_recognize_receipt_fields].
Recognize data from sales receipts using a prebuilt model. Receipt fields recognized by the service can be found [here][service_recognize_receipt_fields].

```C# Snippet:FormRecognizerSampleRecognizeReceiptFileStream
using (FileStream stream = new FileStream(receiptPath, FileMode.Open))
{
RecognizedFormCollection receipts = await client.StartRecognizeReceiptsAsync(stream).WaitForCompletionAsync();
var options = new RecognizeReceiptsOptions() { Locale = "en-US" };
RecognizedFormCollection receipts = await client.StartRecognizeReceiptsAsync(stream, options).WaitForCompletionAsync();

// To see the list of the supported fields returned by service and its corresponding types, consult:
// https://aka.ms/formrecognizer/receiptfields
Expand Down Expand Up @@ -300,7 +305,8 @@ Recognize data from business cards using a prebuilt model. Business card fields
```C# Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream
using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open))
{
RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream).WaitForCompletionAsync();
var options = new RecognizeBusinessCardsOptions() { Locale = "en-US" };
RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream, options).WaitForCompletionAsync();

// To see the list of the supported fields returned by service and its corresponding types, consult:
// https://aka.ms/formrecognizer/businesscardfields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Azure Cognitive Services Form Recognizer is a cloud service that uses machine le

- Recognize form content - Recognize and extract tables, lines, words, and selection marks like radio buttons and check boxes in forms documents, without the need to train a model.
- Recognize custom forms - Recognize and extract form fields and other content from your custom forms, using models you trained with your own form types.
- Recognize receipts - Recognize and extract common fields from US receipts, using a pre-trained receipt model.
- Recognize receipts - Recognize and extract common fields from receipts, using a pre-trained receipt model.
- Recognize business cards - Recognize and extract common fields from business cards, using a pre-trained business cards model.

## Common scenarios samples
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Recognize receipts

This sample demonstrates how to recognize and extract common fields from US receipts, using a pre-trained receipt model. For a suggested approach to extracting information from receipts, see [strongly-typing a recognized form][strongly_typing_a_recognized_form].
This sample demonstrates how to recognize and extract common fields from receipts, using a pre-trained receipt model. For a suggested approach to extracting information from receipts, see [strongly-typing a recognized form][strongly_typing_a_recognized_form].

To get started you'll need a Cognitive Services resource or a Form Recognizer resource. See [README][README] for prerequisites and instructions.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,54 @@ public void StartRecognizeReceiptsFromUriThrowsForNonExistingContent()
Assert.AreEqual("FailedToDownloadImage", ex.ErrorCode);
}

[Test]
[TestCase("en-US")]
[TestCase("")]
public async Task StartRecognizeReceiptsWithSupportedLocale(string locale)
{
var client = CreateFormRecognizerClient();
var options = new RecognizeReceiptsOptions()
{
IncludeFieldElements = true,
Locale = locale
};
RecognizeReceiptsOperation operation;

using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.ReceiptJpg);
using (Recording.DisableRequestBodyRecording())
{
operation = await client.StartRecognizeReceiptsAsync(stream, options);
}

RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(PollingInterval);

var receipt = recognizedForms.Single();

ValidatePrebuiltForm(
receipt,
includeFieldElements: true,
expectedFirstPageNumber: 1,
expectedLastPageNumber: 1);

Assert.Greater(receipt.Fields.Count, 0);

var receiptPage = receipt.Pages.Single();

Assert.Greater(receiptPage.Lines.Count, 0);
Assert.AreEqual(0, receiptPage.SelectionMarks.Count);
Assert.AreEqual(0, receiptPage.Tables.Count);
}

[Test]
public void StartRecognizeReceiptsWithWrongLocale()
{
var client = CreateFormRecognizerClient();

var receiptUri = FormRecognizerTestEnvironment.CreateUri(TestFile.ReceiptJpg);
RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(async () => await client.StartRecognizeReceiptsFromUriAsync(receiptUri, new RecognizeReceiptsOptions() { Locale = "not-locale" }));
Assert.AreEqual("UnsupportedLocale", ex.ErrorCode);
}

#endregion

#region StartRecognizeBusinessCards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ public async Task StartRecognizeReceiptsFromUriEncodesBlankSpaces()
}
}

[Test]
[TestCase("")]
[TestCase("en-US")]
public async Task StartRecognizeReceiptsSendsUserSpecifiedLocale(string locale)
{
var mockResponse = new MockResponse(202);
mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, "host/prebuilt/receipt/analyzeResults/00000000000000000000000000000000"));

var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
var options = new FormRecognizerClientOptions() { Transport = mockTransport };
var client = CreateInstrumentedClient(options);

using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.InvoiceLeTiff);
var recognizeOptions = new RecognizeReceiptsOptions { Locale = locale };
await client.StartRecognizeReceiptsAsync(stream, recognizeOptions);

var requestUriQuery = mockTransport.Requests.Single().Uri.Query;

var localeQuery = "locale=";
var index = requestUriQuery.IndexOf(localeQuery);
var length = requestUriQuery.Length - (index + localeQuery.Length);
Assert.AreEqual(locale, requestUriQuery.Substring(index + localeQuery.Length, length));
}

#endregion

#region Recognize Business Cards
Expand Down Expand Up @@ -250,6 +274,30 @@ public async Task StartRecognizeBusinessCardsFromUriEncodesBlankSpaces()
}
}

[Test]
[TestCase("")]
[TestCase("en-US")]
public async Task StartRecognizeBusinessCardsSendsUserSpecifiedLocale(string locale)
{
var mockResponse = new MockResponse(202);
mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, "host/prebuilt/businesscards/analyzeResults/00000000000000000000000000000000"));

var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
var options = new FormRecognizerClientOptions() { Transport = mockTransport };
var client = CreateInstrumentedClient(options);

using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.InvoiceLeTiff);
var recognizeOptions = new RecognizeReceiptsOptions { Locale = locale };
await client.StartRecognizeReceiptsAsync(stream, recognizeOptions);

var requestUriQuery = mockTransport.Requests.Single().Uri.Query;

var localeQuery = "locale=";
var index = requestUriQuery.IndexOf(localeQuery);
var length = requestUriQuery.Length - (index + localeQuery.Length);
Assert.AreEqual(locale, requestUriQuery.Substring(index + localeQuery.Length, length));
}

#endregion

#region Recognize Custom Forms
Expand Down
Loading

0 comments on commit 437d9fb

Please sign in to comment.