Skip to content

Commit

Permalink
[FR] Add documentation for invoices (#16659)
Browse files Browse the repository at this point in the history
* documentation

* PR feedback
  • Loading branch information
maririos authored Nov 6, 2020
1 parent 8efd319 commit aaa22b5
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 155 deletions.
66 changes: 59 additions & 7 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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.
- Recognize Receipts - Recognize and extract common fields from 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 Prebuilt models - Recognize data using the following prebuilt models:
- Receipts - Recognize and extract common fields from receipts, using a pre-trained receipt model.
- Business Cards - Recognize and extract common fields from business cards, using a pre-trained business cards model.
- Invoices - Recognize and extract common fields from invoices, using a pre-trained invoice model.

[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 @@ -101,10 +103,12 @@ var client = new FormRecognizerClient(new Uri(endpoint), new DefaultAzureCredent

`FormRecognizerClient` provides operations for:

- 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 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).
- 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 the following form types using prebuilt models. These fields and meta-data are returned in a collection of `RecognizedForm` objects.
- Sales receipts. See example [Recognize Receipts](#recognize-receipts).
- Business cards. See example [Recognize Business Cards](#recognize-business-cards).
- Invoices. See example [Recognize Invoices](#recognize-invoices).

### FormTrainingClient

Expand Down Expand Up @@ -134,6 +138,7 @@ The following section provides several code snippets illustrating common pattern
* [Recognize Custom Forms](#recognize-custom-forms)
* [Recognize Receipts](#recognize-receipts)
* [Recognize Business Cards](#recognize-business-cards)
* [Recognize Invoices](#recognize-invoices)
* [Train a Model](#train-a-model)
* [Manage Custom Models](#manage-custom-models)

Expand Down Expand Up @@ -299,7 +304,7 @@ using (FileStream stream = new FileStream(receiptPath, FileMode.Open))
Recognize data from business cards using a prebuilt model. Business card fields recognized by the service can be found [here][service_recognize_business_cards_fields].

```C# Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream
using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open))
using (FileStream stream = new FileStream(businessCardsPath, FileMode.Open))
{
var options = new RecognizeBusinessCardsOptions() { Locale = "en-US" };
RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream, options).WaitForCompletionAsync();
Expand Down Expand Up @@ -368,6 +373,52 @@ using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open))
}
```

### Recognize Invoices
Recognize data from invoices using a prebuilt model. Invoices fields recognized by the service can be found [here][service_recognize_invoices_fields].

```C# Snippet:FormRecognizerSampleRecognizeInvoicesFileStream
using (FileStream stream = new FileStream(invoicePath, FileMode.Open))
{
var options = new RecognizeInvoicesOptions() { Locale = "en-US" };
RecognizedFormCollection invoices = await client.StartRecognizeInvoicesAsync(stream, options).WaitForCompletionAsync();

// To see the list of the supported fields returned by service and its corresponding types, consult:
// https://aka.ms/formrecognizer/invoicefields
RecognizedForm invoice = invoices.Single();

FormField vendorNameField;
if (invoice.Fields.TryGetValue("VendorName", out vendorNameField))
{
if (vendorNameField.Value.ValueType == FieldValueType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
}
}

FormField customerNameField;
if (invoice.Fields.TryGetValue("CustomerName", out customerNameField))
{
if (customerNameField.Value.ValueType == FieldValueType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
}
}

FormField invoiceTotalField;
if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField))
{
if (invoiceTotalField.Value.ValueType == FieldValueType.Float)
{
float invoiceTotal = invoiceTotalField.Value.AsFloat();
Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}");
}
}
}
```

### Train a Model
Train a machine-learned model on your own form types. The resulting model will be able to recognize values from the types of forms it was trained on.

Expand Down Expand Up @@ -602,6 +653,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
[labeling_tool]: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/quickstarts/label-tool
[service_recognize_receipt_fields]: https://aka.ms/formrecognizer/receiptfields
[service_recognize_business_cards_fields]: https://aka.ms/formrecognizer/businesscardfields
[service_recognize_invoices_fields]: https://aka.ms/formrecognizer/invoicefields
[dotnet_lro_guidelines]: https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-longrunning

[logging]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/core/Azure.Core/samples/Diagnostics.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Azure Cognitive Services Form Recognizer is a cloud service that uses machine le
- [Recognize custom forms](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample2_RecognizeCustomForms.md)
- [Recognize receipts](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeReceipts.md)
- [Recognize business cards](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample9_RecognizeBusinessCards.md)
<!--- [Recognize invoices](https://github.com/Azure/azure-sdk-for-net/blob/feature/formrecognizer2.1/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample10_RecognizeInvoices.md)-->
- [Train a model](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample5_TrainModel.md)
- [Manage custom models](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample6_ManageCustomModels.md)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Recognize invoices

This sample demonstrates how to recognize and extract common fields from invoices, using a pre-trained model. For a suggested approach to extracting information from invoices, 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.

## Creating a `FormRecognizerClient`

To create a new `FormRecognizerClient` you need the endpoint and credentials from your resource. In the sample below you'll use a Form Recognizer API key credential by creating an `AzureKeyCredential` object, that if needed, will allow you to update the API key without creating a new client.

You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application.

```C# Snippet:CreateFormRecognizerClient
string endpoint = "<endpoint>";
string apiKey = "<apiKey>";
var credential = new AzureKeyCredential(apiKey);
var client = new FormRecognizerClient(new Uri(endpoint), credential);
```

## Recognize invoices from a URI

To recognize invoices from a URI, use the `StartRecognizeInvoicesFromUriAsync` method.

```C# Snippet:FormRecognizerSampleRecognizeInvoicesUri
var options = new RecognizeInvoicesOptions() { Locale = "en-US" };
RecognizedFormCollection invoices = await client.StartRecognizeInvoicesFromUriAsync(invoiceUri, options).WaitForCompletionAsync();

// To see the list of the supported fields returned by service and its corresponding types, consult:
// https://aka.ms/formrecognizer/invoicefields
RecognizedForm invoice = invoices.Single();

FormField invoiceIdField;
if (invoice.Fields.TryGetValue("InvoiceId", out invoiceIdField))
{
if (invoiceIdField.Value.ValueType == FieldValueType.String)
{
string invoiceId = invoiceIdField.Value.AsString();
Console.WriteLine($" Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}");
}
}

FormField invoiceDateField;
if (invoice.Fields.TryGetValue("InvoiceDate", out invoiceDateField))
{
if (invoiceDateField.Value.ValueType == FieldValueType.Date)
{
DateTime invoiceDate = invoiceDateField.Value.AsDate();
Console.WriteLine($" Invoice Date: '{invoiceDate}', with confidence {invoiceDateField.Confidence}");
}
}

FormField dueDateField;
if (invoice.Fields.TryGetValue("DueDate", out dueDateField))
{
if (dueDateField.Value.ValueType == FieldValueType.Date)
{
DateTime dueDate = dueDateField.Value.AsDate();
Console.WriteLine($" Due Date: '{dueDate}', with confidence {dueDateField.Confidence}");
}
}

FormField vendorNameField;
if (invoice.Fields.TryGetValue("VendorName", out vendorNameField))
{
if (vendorNameField.Value.ValueType == FieldValueType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
}
}

FormField vendorAddressField;
if (invoice.Fields.TryGetValue("VendorAddress", out vendorAddressField))
{
if (vendorAddressField.Value.ValueType == FieldValueType.String)
{
string vendorAddress = vendorAddressField.Value.AsString();
Console.WriteLine($" Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.Confidence}");
}
}

FormField customerNameField;
if (invoice.Fields.TryGetValue("CustomerName", out customerNameField))
{
if (customerNameField.Value.ValueType == FieldValueType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
}
}

FormField customerAddressField;
if (invoice.Fields.TryGetValue("CustomerAddress", out customerAddressField))
{
if (customerAddressField.Value.ValueType == FieldValueType.String)
{
string customerAddress = customerAddressField.Value.AsString();
Console.WriteLine($" Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}");
}
}

FormField customerAddressRecipientField;
if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out customerAddressRecipientField))
{
if (customerAddressRecipientField.Value.ValueType == FieldValueType.String)
{
string customerAddressRecipient = customerAddressRecipientField.Value.AsString();
Console.WriteLine($" Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.Confidence}");
}
}

FormField invoiceTotalField;
if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField))
{
if (invoiceTotalField.Value.ValueType == FieldValueType.Float)
{
float invoiceTotal = invoiceTotalField.Value.AsFloat();
Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}");
}
}
}
```

## Recognize invoices from a given file

To recognize invoices from a given file, use the `StartRecognizeInvoicesAsync` method.

```C# Snippet:FormRecognizerSampleRecognizeInvoicesFileStream
using (FileStream stream = new FileStream(invoicePath, FileMode.Open))
{
var options = new RecognizeInvoicesOptions() { Locale = "en-US" };
RecognizedFormCollection invoices = await client.StartRecognizeInvoicesAsync(stream, options).WaitForCompletionAsync();

// To see the list of the supported fields returned by service and its corresponding types, consult:
// https://aka.ms/formrecognizer/invoicefields
RecognizedForm invoice = invoices.Single();

FormField vendorNameField;
if (invoice.Fields.TryGetValue("VendorName", out vendorNameField))
{
if (vendorNameField.Value.ValueType == FieldValueType.String)
{
string vendorName = vendorNameField.Value.AsString();
Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
}
}

FormField customerNameField;
if (invoice.Fields.TryGetValue("CustomerName", out customerNameField))
{
if (customerNameField.Value.ValueType == FieldValueType.String)
{
string customerName = customerNameField.Value.AsString();
Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
}
}

FormField invoiceTotalField;
if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField))
{
if (invoiceTotalField.Value.ValueType == FieldValueType.Float)
{
float invoiceTotal = invoiceTotalField.Value.AsFloat();
Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}");
}
}
}
```

To see the full example source files, see:

* [Recognize invoices from URI](https://github.com/Azure/azure-sdk-for-net/blob/feature/formrecognizer2.1/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs)
* [Recognize invoices from file](https://github.com/Azure/azure-sdk-for-net/blob/feature/formrecognizer2.1/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs)

[README]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer#getting-started
[strongly_typing_a_recognized_form]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample4_StronglyTypingARecognizedForm.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public async Task RecognizeBusinessCardsFromFile()

FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

string busienssCardsPath = FormRecognizerTestEnvironment.CreatePath("businessCard.jpg");
string businessCardsPath = FormRecognizerTestEnvironment.CreatePath("businessCard.jpg");

#region Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream
using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open))
using (FileStream stream = new FileStream(businessCardsPath, FileMode.Open))
{
var options = new RecognizeBusinessCardsOptions() { Locale = "en-US" };
RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream, options).WaitForCompletionAsync();
Expand Down
Loading

0 comments on commit aaa22b5

Please sign in to comment.