public async Task StartRecognizeReceiptsWithSupportedLocale() { var client = CreateFormRecognizerClient(); var options = new RecognizeReceiptsOptions() { IncludeFieldElements = true, Locale = FormRecognizerLocale.EnUS }; RecognizeReceiptsOperation operation; using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.ReceiptJpg); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeReceiptsAsync(stream, options); } RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(); 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); }
public async Task StartRecognizeReceiptsCanParseBlankPage() { var client = CreateFormRecognizerClient(); var options = new RecognizeReceiptsOptions() { IncludeFieldElements = true }; RecognizeReceiptsOperation operation; using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Blank); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeReceiptsAsync(stream, options); } RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(); var blankForm = recognizedForms.Single(); ValidatePrebuiltForm( blankForm, includeFieldElements: true, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); Assert.AreEqual(0, blankForm.Fields.Count); var blankPage = blankForm.Pages.Single(); Assert.AreEqual(0, blankPage.Lines.Count); Assert.AreEqual(0, blankPage.Tables.Count); }
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.ReceiptJpg); 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)); }
public async Task StartRecognizeReceiptsCanParseMultipageForm(bool useStream) { var client = CreateFormRecognizerClient(); var options = new RecognizeReceiptsOptions() { IncludeFieldElements = true }; RecognizeReceiptsOperation operation; if (useStream) { using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.ReceipMultipage); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeReceiptsAsync(stream, options); } } else { var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.ReceipMultipage); operation = await client.StartRecognizeReceiptsFromUriAsync(uri, options); } RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(); Assert.AreEqual(2, recognizedForms.Count); for (int formIndex = 0; formIndex < recognizedForms.Count; formIndex++) { var recognizedForm = recognizedForms[formIndex]; var expectedPageNumber = formIndex + 1; Assert.NotNull(recognizedForm); ValidatePrebuiltForm( recognizedForm, includeFieldElements: true, expectedFirstPageNumber: expectedPageNumber, expectedLastPageNumber: expectedPageNumber); // Basic sanity test to make sure pages are ordered correctly. var sampleField = recognizedForm.Fields["Total"]; Assert.IsNotNull(sampleField.ValueData); if (formIndex == 0) { Assert.AreEqual("$14.50", sampleField.ValueData.Text); } else if (formIndex == 1) { Assert.AreEqual("$ 1203.39", sampleField.ValueData.Text); } } }
public async Task StartRecognizeReceiptsCanParseMultipageFormWithBlankPage() { var client = CreateFormRecognizerClient(); var options = new RecognizeReceiptsOptions() { IncludeFieldElements = true }; RecognizeReceiptsOperation operation; using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.ReceipMultipageWithBlankPage); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeReceiptsAsync(stream, options); } RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(); Assert.AreEqual(3, recognizedForms.Count); for (int formIndex = 0; formIndex < recognizedForms.Count; formIndex++) { var recognizedForm = recognizedForms[formIndex]; var expectedPageNumber = formIndex + 1; Assert.NotNull(recognizedForm); ValidatePrebuiltForm( recognizedForm, includeFieldElements: true, expectedFirstPageNumber: expectedPageNumber, expectedLastPageNumber: expectedPageNumber); // Basic sanity test to make sure pages are ordered correctly. if (formIndex == 0 || formIndex == 2) { var sampleField = recognizedForm.Fields["Total"]; var expectedValueData = formIndex == 0 ? "$14.50" : "$ 1203.39"; Assert.IsNotNull(sampleField.ValueData); Assert.AreEqual(expectedValueData, sampleField.ValueData.Text); } } var blankForm = recognizedForms[1]; Assert.AreEqual(0, blankForm.Fields.Count); var blankPage = blankForm.Pages.Single(); Assert.AreEqual(0, blankPage.Lines.Count); Assert.AreEqual(0, blankPage.Tables.Count); }
public void StartRecognizeReceiptsRespectsTheCancellationToken() { var client = CreateInstrumentedClient(); var options = new RecognizeReceiptsOptions { ContentType = FormContentType.Pdf }; using var stream = new MemoryStream(Array.Empty <byte>()); using var cancellationSource = new CancellationTokenSource(); cancellationSource.Cancel(); Assert.ThrowsAsync(Is.InstanceOf <OperationCanceledException>(), async() => await client.StartRecognizeReceiptsAsync(stream, recognizeReceiptsOptions: options, cancellationToken: cancellationSource.Token)); }
public async Task StartRecognizeReceiptsSendsUserSpecifiedContentType() { 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 { ContentType = FormContentType.Jpeg }; await client.StartRecognizeReceiptsAsync(stream, recognizeOptions); var request = mockTransport.Requests.Single(); Assert.True(request.Headers.TryGetValue("Content-Type", out var contentType)); Assert.AreEqual("image/jpeg", contentType); }
public async Task RecognizeReceiptsFromFile() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #region Snippet:FormRecognizerSampleRecognizeReceiptFileStream #if SNIPPET string receiptPath = "<receiptPath>"; #else string receiptPath = FormRecognizerTestEnvironment.CreatePath("contoso-receipt.jpg"); #endif using var stream = new FileStream(receiptPath, FileMode.Open); var options = new RecognizeReceiptsOptions() { Locale = "en-US" }; RecognizeReceiptsOperation operation = await client.StartRecognizeReceiptsAsync(stream, options); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection receipts = operationResponse.Value; // To see the list of the supported fields returned by service and its corresponding types, consult: // https://aka.ms/formrecognizer/receiptfields foreach (RecognizedForm receipt in receipts) { if (receipt.Fields.TryGetValue("MerchantName", out FormField merchantNameField)) { if (merchantNameField.Value.ValueType == FieldValueType.String) { string merchantName = merchantNameField.Value.AsString(); Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {merchantNameField.Confidence}"); } } if (receipt.Fields.TryGetValue("TransactionDate", out FormField transactionDateField)) { if (transactionDateField.Value.ValueType == FieldValueType.Date) { DateTime transactionDate = transactionDateField.Value.AsDate(); Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {transactionDateField.Confidence}"); } } if (receipt.Fields.TryGetValue("Items", out FormField itemsField)) { if (itemsField.Value.ValueType == FieldValueType.List) { foreach (FormField itemField in itemsField.Value.AsList()) { Console.WriteLine("Item:"); if (itemField.Value.ValueType == FieldValueType.Dictionary) { IReadOnlyDictionary <string, FormField> itemFields = itemField.Value.AsDictionary(); if (itemFields.TryGetValue("Name", out FormField itemNameField)) { if (itemNameField.Value.ValueType == FieldValueType.String) { string itemName = itemNameField.Value.AsString(); Console.WriteLine($" Name: '{itemName}', with confidence {itemNameField.Confidence}"); } } if (itemFields.TryGetValue("TotalPrice", out FormField itemTotalPriceField)) { if (itemTotalPriceField.Value.ValueType == FieldValueType.Float) { float itemTotalPrice = itemTotalPriceField.Value.AsFloat(); Console.WriteLine($" Total Price: '{itemTotalPrice}', with confidence {itemTotalPriceField.Confidence}"); } } } } } } if (receipt.Fields.TryGetValue("Total", out FormField totalField)) { if (totalField.Value.ValueType == FieldValueType.Float) { float total = totalField.Value.AsFloat(); Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'"); } } } #endregion }