-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[FR] Add documentation for invoices #16659
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample10_RecognizeInvoices.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented for now so CI doesn't fail when validating the link. I will update in a future PR