private Facturae32.InvoiceIssueDataType CreateInvoiceIssueData(RecognizedForm form) => new Facturae32.InvoiceIssueDataType() { IssueDate = DateTime.ParseExact($"{form.GetFieldValue("Invoices.Invoice.InvoiceIssueData.IssueDate")}", "dd/MM/yyyy", cultureInfo), InvoiceCurrencyCode = Facturae32.CurrencyCodeType.EUR, TaxCurrencyCode = Facturae32.CurrencyCodeType.EUR, LanguageName = Facturae32.LanguageCodeType.es };
public async Task StartRecognizeReceiptsCanAuthenticateWithTokenCredential() { var client = CreateFormRecognizerClient(useTokenCredential: true); RecognizeReceiptsOperation operation; using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.ReceiptJpg); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeReceiptsAsync(stream); } // Sanity check to make sure we got an actual response back from the service. RecognizedFormCollection formPage = await operation.WaitForCompletionAsync(); RecognizedForm form = formPage.Single(); Assert.NotNull(form); ValidatePrebuiltForm( form, includeFieldElements: true, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); }
public async Task StartRecognizeCustomFormsWithLabelsAndSelectionMarks(bool includeFieldElements) { var client = CreateFormRecognizerClient(); var options = new RecognizeCustomFormsOptions { IncludeFieldElements = includeFieldElements }; RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true, ContainerType.SelectionMarks); using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.FormSelectionMarks); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.Single(); ValidateRecognizedForm(form, includeFieldElements: includeFieldElements, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); // Testing that we shuffle things around correctly so checking only once per property. Assert.IsNotEmpty(form.FormType); Assert.IsNotNull(form.Fields); var name = "AMEX_SELECTION_MARK"; Assert.IsNotNull(form.Fields[name]); Assert.AreEqual(FieldValueType.SelectionMark, form.Fields[name].Value.ValueType); Assert.AreEqual(SelectionMarkState.Selected, form.Fields[name].Value.AsSelectionMarkState()); }
private Facturae32.InvoiceTotalsType CreateInvoiceTotals(RecognizedForm form) { var totals = new Facturae32.InvoiceTotalsType() { TotalGrossAmount = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.TaxesOutputs.Tax.TaxableBase.TotalAmount"), cultureInfo)), TotalGrossAmountBeforeTaxes = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.TaxesOutputs.Tax.TaxableBase.TotalAmount"), cultureInfo)), TotalTaxOutputs = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.TaxesOutputs.Tax.TaxAmount.TotalAmount"), cultureInfo)), TotalOutstandingAmount = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.InvoiceTotal"), cultureInfo)), InvoiceTotal = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.InvoiceTotal"), cultureInfo)), TotalExecutableAmount = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.TotalExecutableAmount"), cultureInfo)), }; if (!string.IsNullOrWhiteSpace(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.GeneralSurcharges.Charge.ChargeReason"))) { totals.GeneralSurcharges = new[] { new Facturae32.ChargeType() { ChargeReason = form.GetFieldValue("Invoices.Invoice.InvoiceTotals.GeneralSurcharges.Charge.ChargeReason"), ChargeAmount = new Facturae32.DoubleSixDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.GeneralSurcharges.Charge.ChargeAmount"), cultureInfo)), } }; totals.TotalGeneralSurcharges = totals.GeneralSurcharges.Sum(x => x.ChargeAmount); totals.TotalGeneralSurchargesSpecified = true; totals.TotalGrossAmountBeforeTaxes += totals.TotalGeneralSurcharges; totals.InvoiceTotal += totals.TotalGeneralSurcharges; totals.TotalOutstandingAmount += totals.TotalGeneralSurcharges; } return(totals); }
private Facturae32.InvoiceHeaderType CreateInvoiceHeader(RecognizedForm form) => new Facturae32.InvoiceHeaderType() { InvoiceSeriesCode = $"{form.GetFieldValue("Invoices.Invoice.InvoiceHeader.InvoiceSeriesCode")}", InvoiceNumber = $"{form.GetFieldValue("Invoices.Invoice.InvoiceHeader.InvoiceNumber")}", InvoiceDocumentType = Facturae32.InvoiceDocumentTypeType.FC, InvoiceClass = Facturae32.InvoiceClassType.OO };
public async Task CheckFormTypeinSubmodelAndRecognizedForm(bool labeled) { var client = CreateFormTrainingClient(); var formClient = client.GetFormRecognizerClient(); var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl); TrainingOperation trainingOperation = await client.StartTrainingAsync(trainingFilesUri, labeled); await trainingOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(trainingOperation.HasValue); CustomFormModel model = trainingOperation.Value; Assert.IsNotNull(model.Submodels.FirstOrDefault().FormType); var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.Form1); RecognizeCustomFormsOperation recognizeOperation = await formClient.StartRecognizeCustomFormsFromUriAsync(model.ModelId, uri); await recognizeOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(recognizeOperation.HasValue); RecognizedForm form = recognizeOperation.Value.Single(); Assert.IsNotNull(form.FormType); Assert.AreEqual(form.FormType, model.Submodels.FirstOrDefault().FormType); }
private Facturae32.BusinessType CreateSellerParty(RecognizedForm form) => new Facturae32.BusinessType() { TaxIdentification = new Facturae32.TaxIdentificationType() { PersonTypeCode = Facturae32.PersonTypeCodeType.J, ResidenceTypeCode = Facturae32.ResidenceTypeCodeType.R, TaxIdentificationNumber = $"{form.GetFieldValue("SellerParty.TaxIdentification.TaxIdentificationNumber")}" }, Item = new Facturae32.LegalEntityType() { CorporateName = $"{form.GetFieldValue("SellerParty.LegalEntity.CorporateName")}", RegistrationData = new Facturae32.RegistrationDataType() { Book = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.Book")}", RegisterOfCompaniesLocation = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.RegisterOfCompaniesLocation")}", Sheet = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.Sheet")}", Folio = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.Folio")}", Section = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.Section")}", Volume = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.Volume")}", AdditionalRegistrationData = $"{form.GetFieldValue("SellerParty.LegalEntity.RegistrationData.AdditionalRegistrationData")}" }, Item = new Facturae32.AddressType() { Address = $"{form.GetFieldValue("SellerParty.LegalEntity.AddressInSpain.Address")}", CountryCode = Facturae32.CountryType.ESP, PostCode = $"{form.GetFieldValue("SellerParty.LegalEntity.AddressInSpain.PostCode")}", Province = $"{form.GetFieldValue("SellerParty.LegalEntity.AddressInSpain.Province")}", Town = $"{form.GetFieldValue("SellerParty.LegalEntity.AddressInSpain.Town")}" } } };
public async Task StartRecognizeCustomFormsWithTableFixedRows() { var client = CreateFormRecognizerClient(); RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true, ContainerType.TableFixedRows); using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.FormTableFixedRows); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } RecognizedFormCollection forms = await operation.WaitForCompletionAsync(); RecognizedForm form = forms.Single(); ValidateModelWithLabelsForm( form, trainedModel.ModelId, includeFieldElements: false, expectedFirstPageNumber: 1, expectedLastPageNumber: 1, isComposedModel: false); }
public async Task StartRecognizeCustomFormsWithoutLabels(bool useStream, bool includeFieldElements) { var client = CreateFormRecognizerClient(); var options = new RecognizeCustomFormsOptions { IncludeFieldElements = includeFieldElements }; RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false); if (useStream) { using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Form1); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options); } } else { var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.Form1); operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri, options); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.Single(); ValidateModelWithNoLabelsForm( form, trainedModel.ModelId, includeFieldElements: includeFieldElements, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); //testing that we shuffle things around correctly so checking only once per property Assert.AreEqual("form-0", form.FormType); Assert.IsFalse(form.FormTypeConfidence.HasValue); Assert.AreEqual(1, form.Pages.Count); Assert.AreEqual(2200, form.Pages[0].Height); Assert.AreEqual(1, form.Pages[0].PageNumber); Assert.AreEqual(LengthUnit.Pixel, form.Pages[0].Unit); Assert.AreEqual(1700, form.Pages[0].Width); Assert.IsNotNull(form.Fields); var name = "field-0"; Assert.IsNotNull(form.Fields[name]); Assert.IsNotNull(form.Fields[name].LabelData.Text); Assert.AreEqual(FieldValueType.String, form.Fields[name].Value.ValueType); // Disable this verification for now. // Issue https://github.com/Azure/azure-sdk-for-net/issues/15075 // Assert.AreEqual("Hero Limited", form.Fields[name].LabelData.Text); }
private Facturae32.InstallmentType[] CreatePaymentDetails(RecognizedForm form) => new Facturae32.InstallmentType[] { new Facturae32.InstallmentType() { InstallmentDueDate = DateTime.ParseExact($"{form.GetFieldValue("Invoices.Invoice.PaymentDetails.Installment.InstallmentDueDate")}", "dd/MM/yyyy", cultureInfo), InstallmentAmount = new Facturae32.DoubleTwoDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.PaymentDetails.Installment.InstallmentAmount"), cultureInfo)), } };
public async Task RecognizeInvoicesFromFile() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); string invoicePath = FormRecognizerTestEnvironment.CreatePath("Invoice_1.pdf"); #region Snippet:FormRecognizerSampleRecognizeInvoicesFileStream //@@ string invoicePath = "<invoicePath>"; using var stream = new FileStream(invoicePath, FileMode.Open); var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; RecognizeInvoicesOperation operation = await client.StartRecognizeInvoicesAsync(stream, options); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection invoices = operationResponse.Value; // 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(); if (invoice.Fields.TryGetValue("VendorName", out FormField vendorNameField)) { if (vendorNameField.Value.ValueType == FieldValueType.String) { string vendorName = vendorNameField.Value.AsString(); Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); } } if (invoice.Fields.TryGetValue("CustomerName", out FormField customerNameField)) { if (customerNameField.Value.ValueType == FieldValueType.String) { string customerName = customerNameField.Value.AsString(); Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); } } if (invoice.Fields.TryGetValue("InvoiceTotal", out FormField invoiceTotalField)) { if (invoiceTotalField.Value.ValueType == FieldValueType.Float) { float invoiceTotal = invoiceTotalField.Value.AsFloat(); Console.WriteLine($"Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); } } #endregion }
protected void ValidatePrebuiltForm(RecognizedForm recognizedForm, bool includeFieldElements, int expectedFirstPageNumber, int expectedLastPageNumber) { Assert.NotNull(recognizedForm.FormType); Assert.IsTrue(recognizedForm.FormTypeConfidence.HasValue); Assert.That(recognizedForm.FormTypeConfidence.Value, Is.LessThanOrEqualTo(1.0).Within(0.005)); Assert.IsNull(recognizedForm.ModelId); ValidateRecognizedForm(recognizedForm, includeFieldElements, expectedFirstPageNumber, expectedLastPageNumber); }
private void ValidateModelWithNoLabelsForm(RecognizedForm recognizedForm, string modelId, bool includeFieldElements, int expectedFirstPageNumber, int expectedLastPageNumber) { Assert.NotNull(recognizedForm.FormType); Assert.IsFalse(recognizedForm.FormTypeConfidence.HasValue); Assert.IsNotNull(recognizedForm.ModelId); Assert.AreEqual(modelId, recognizedForm.ModelId); ValidateRecognizedForm(recognizedForm, includeFieldElements, expectedFirstPageNumber, expectedLastPageNumber); }
public async Task StartRecognizeCustomFormsWithLabels(bool useStream, bool includeFieldElements) { var client = CreateFormRecognizerClient(); var options = new RecognizeCustomFormsOptions { IncludeFieldElements = includeFieldElements }; RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true); if (useStream) { using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Form1); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options); } } else { var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.Form1); operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri, options); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.Single(); ValidateModelWithLabelsForm( form, trainedModel.ModelId, includeFieldElements: includeFieldElements, expectedFirstPageNumber: 1, expectedLastPageNumber: 1, isComposedModel: false); // Testing that we shuffle things around correctly so checking only once per property. Assert.IsNotEmpty(form.FormType); Assert.IsTrue(form.FormTypeConfidence.HasValue); Assert.AreEqual(1, form.Pages.Count); Assert.AreEqual(2200, form.Pages[0].Height); Assert.AreEqual(1, form.Pages[0].PageNumber); Assert.AreEqual(LengthUnit.Pixel, form.Pages[0].Unit); Assert.AreEqual(1700, form.Pages[0].Width); Assert.IsNotNull(form.Fields); var name = "PurchaseOrderNumber"; Assert.IsNotNull(form.Fields[name]); Assert.AreEqual(FieldValueType.String, form.Fields[name].Value.ValueType); Assert.AreEqual("948284", form.Fields[name].ValueData.Text); }
public async Task StartRecognizeCustomForms(bool useStream) { var client = CreateInstrumentedFormRecognizerClient(); RecognizeCustomFormsOperation operation; string modelId = await GetModelIdAsync(); try { if (useStream) { using var stream = new FileStream(FormRecognizerTestEnvironment.FormPath, FileMode.Open); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(modelId, stream); } } else { var uri = new Uri(FormRecognizerTestEnvironment.FormUri); operation = await client.StartRecognizeCustomFormsFromUriAsync(modelId, uri); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.FirstOrDefault(); //testing that we shuffle things around correctly so checking only once per property Assert.AreEqual("form-0", form.FormType); Assert.AreEqual(1, form.PageRange.FirstPageNumber); Assert.AreEqual(1, form.PageRange.LastPageNumber); Assert.AreEqual(1, form.Pages.Count); Assert.AreEqual(2200, form.Pages[0].Height); Assert.AreEqual(1, form.Pages[0].PageNumber); Assert.AreEqual(LengthUnit.Pixel, form.Pages[0].Unit); Assert.AreEqual(1700, form.Pages[0].Width); Assert.IsNotNull(form.Fields); var name = "field-0"; Assert.IsNotNull(form.Fields[name]); Assert.IsNotNull(form.Fields[name].Confidence); Assert.IsNotNull(form.Fields[name].LabelText.Text); Assert.AreEqual(FieldValueType.StringType, form.Fields[name].Value.Type); Assert.AreEqual("Hero Limited", form.Fields[name].LabelText.Text); } finally { DeleteModel(modelId); } }
private Facturae32.Facturae MappingFrom(RecognizedForm form) { var facturae = new Facturae32.Facturae { FileHeader = CreateFileHeader(form), Parties = CreateParties(form), Invoices = CreateInvoices(form) }; return(facturae); }
private Facturae32.InvoiceType[] CreateInvoices(RecognizedForm form) => new Facturae32.InvoiceType[] { new Facturae32.InvoiceType() { InvoiceHeader = CreateInvoiceHeader(form), InvoiceIssueData = CreateInvoiceIssueData(form), TaxesOutputs = CreateTaxesOutputs(form), InvoiceTotals = CreateInvoiceTotals(form), Items = CreateInvoiceLines(form), PaymentDetails = CreatePaymentDetails(form) } };
//assigning the right key with the right dictionary with for loop so we dont use to many if-else private void addValues(RecognizedForm businessCard) { /* keys: * "CompanyNames", "ContactNames", "JobTitles", "WorkPhones", "MobilePhones","Faxes", "OtherPhones", "Emails", "Addresses", "Departments" * dictionaries: * companyDic, nameDic, jobTitleDic, phoneDic, mobileDic, faxDic, phoneDic, emailDic, addressDic, natureDic */ for (int i = 0; i < dictionaries.Length; i++) { checkField(businessCard, keys[i], dictionaries[i]); } }
public async Task ScanLocalCardId(string filePath) { using var stream = new FileStream(filePath, FileMode.Open); var options = new RecognizeIdentityDocumentsOptions() { ContentType = FormContentType.Jpeg }; RecognizeIdentityDocumentsOperation operation = await _client.StartRecognizeIdentityDocumentsAsync(stream, options); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection identityDocuments = operationResponse.Value; RecognizedForm identityDocument = identityDocuments.Single(); }
public async Task StartRecognizeCustomFormsWithLabels(bool useStream) { var client = CreateInstrumentedFormRecognizerClient(); RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true); if (useStream) { using var stream = new FileStream(FormRecognizerTestEnvironment.FormPath, FileMode.Open); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } } else { var uri = new Uri(FormRecognizerTestEnvironment.FormUri); operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.FirstOrDefault(); // Testing that we shuffle things around correctly so checking only once per property. Assert.AreEqual("custom:form", form.FormType); Assert.AreEqual(1, form.PageRange.FirstPageNumber); Assert.AreEqual(1, form.PageRange.LastPageNumber); Assert.AreEqual(1, form.Pages.Count); Assert.AreEqual(2200, form.Pages[0].Height); Assert.AreEqual(1, form.Pages[0].PageNumber); Assert.AreEqual(LengthUnit.Pixel, form.Pages[0].Unit); Assert.AreEqual(1700, form.Pages[0].Width); Assert.IsNotNull(form.Fields); var name = "PurchaseOrderNumber"; Assert.IsNotNull(form.Fields[name]); Assert.IsNotNull(form.Fields[name].Confidence); Assert.AreEqual(FieldValueType.StringType, form.Fields[name].Value.Type); Assert.AreEqual("948284", form.Fields[name].ValueText.Text); }
public async Task StartRecognizeCustomFormsWithoutLabels(bool useStream) { var client = CreateInstrumentedFormRecognizerClient(); RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false); if (useStream) { using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Form1); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } } else { var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.Form1); operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.FirstOrDefault(); //testing that we shuffle things around correctly so checking only once per property Assert.AreEqual("form-0", form.FormType); Assert.AreEqual(1, form.PageRange.FirstPageNumber); Assert.AreEqual(1, form.PageRange.LastPageNumber); Assert.AreEqual(1, form.Pages.Count); Assert.AreEqual(2200, form.Pages[0].Height); Assert.AreEqual(1, form.Pages[0].PageNumber); Assert.AreEqual(LengthUnit.Pixel, form.Pages[0].Unit); Assert.AreEqual(1700, form.Pages[0].Width); Assert.IsNotNull(form.Fields); var name = "field-0"; Assert.IsNotNull(form.Fields[name]); Assert.IsNotNull(form.Fields[name].LabelText.Text); Assert.AreEqual(FieldValueType.String, form.Fields[name].Value.Type); Assert.AreEqual("Hero Limited", form.Fields[name].LabelText.Text); }
private static async Task <bool> IsExpensiveAsync(string modelId, Uri documentUri, FormRecognizerClient client) { RecognizeCustomFormsOperation operation = await client.StartRecognizeCustomFormsFromUriAsync(modelId, documentUri); Response <RecognizedFormCollection> response = await operation.WaitForCompletionAsync(); RecognizedForm form = response.Value[0]; if (form.Fields.TryGetValue("totalPrice", out FormField totalPriceField) && totalPriceField.Value.ValueType == FieldValueType.Float) { return(totalPriceField.Confidence > 0.7f && totalPriceField.Value.AsFloat() > 100f); } else { return(false); } }
/// <summary> /// Initializes a new instance of the <see cref="Receipt"/> class. /// </summary> /// <param name="recognizedForm">The form from which information will be extracted to build this receipt.</param> #region Snippet:FormRecognizerSampleReceiptWrapper public Receipt(RecognizedForm recognizedForm) { // To see the list of the supported fields returned by service and its corresponding types, consult: // https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult ReceiptType = ConvertStringField("ReceiptType", recognizedForm.Fields); MerchantAddress = ConvertStringField("MerchantAddress", recognizedForm.Fields); MerchantName = ConvertStringField("MerchantName", recognizedForm.Fields); MerchantPhoneNumber = ConvertPhoneNumberField("MerchantPhoneNumber", recognizedForm.Fields); Subtotal = ConvertFloatField("Subtotal", recognizedForm.Fields); Tax = ConvertFloatField("Tax", recognizedForm.Fields); Tip = ConvertFloatField("Tip", recognizedForm.Fields); Total = ConvertFloatField("Total", recognizedForm.Fields); TransactionDate = ConvertDateField("TransactionDate", recognizedForm.Fields); TransactionTime = ConvertTimeField("TransactionTime", recognizedForm.Fields); Items = ConvertReceiptItems(recognizedForm.Fields); }
/// <summary> /// Initializes a new instance of the <see cref="Receipt"/> class. /// </summary> /// <param name="recognizedForm">The form from which information will be extracted to build this receipt.</param> #region Snippet:FormRecognizerSampleReceiptWrapper public Receipt(RecognizedForm recognizedForm) { // To see the list of the supported fields returned by service and its corresponding types, consult: // https://aka.ms/azsdk/python/formrecognizer/receiptfields ReceiptType = ConvertStringField("ReceiptType", recognizedForm.Fields); MerchantAddress = ConvertStringField("MerchantAddress", recognizedForm.Fields); MerchantName = ConvertStringField("MerchantName", recognizedForm.Fields); MerchantPhoneNumber = ConvertPhoneNumberField("MerchantPhoneNumber", recognizedForm.Fields); Subtotal = ConvertFloatField("Subtotal", recognizedForm.Fields); Tax = ConvertFloatField("Tax", recognizedForm.Fields); Tip = ConvertFloatField("Tip", recognizedForm.Fields); Total = ConvertFloatField("Total", recognizedForm.Fields); TransactionDate = ConvertDateField("TransactionDate", recognizedForm.Fields); TransactionTime = ConvertTimeField("TransactionTime", recognizedForm.Fields); Items = ConvertReceiptItems(recognizedForm.Fields); }
public async Task ScanRemoteCardId(string url) { Uri formFileUri = new Uri(url); RecognizeIdentityDocumentsOperation operation = await _client.StartRecognizeIdentityDocumentsFromUriAsync(formFileUri); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection identityDocuments = operationResponse.Value; RecognizedForm identityDocument = identityDocuments.Single(); if (identityDocument.FormType == "prebuilt:idDocument:passport") { if (identityDocument.FormTypeConfidence > 0.90) { var s = identityDocument.Fields["MachineReadableZone"].ValueData.Text; } } }
protected void ValidateRecognizedForm(RecognizedForm recognizedForm, bool includeFieldElements, int expectedFirstPageNumber, int expectedLastPageNumber) { Assert.AreEqual(expectedFirstPageNumber, recognizedForm.PageRange.FirstPageNumber); Assert.AreEqual(expectedLastPageNumber, recognizedForm.PageRange.LastPageNumber); Assert.NotNull(recognizedForm.Pages); Assert.AreEqual(expectedLastPageNumber - expectedFirstPageNumber + 1, recognizedForm.Pages.Count); int expectedPageNumber = expectedFirstPageNumber; for (int pageIndex = 0; pageIndex < recognizedForm.Pages.Count; pageIndex++) { var formPage = recognizedForm.Pages[pageIndex]; ValidateFormPage(formPage, includeFieldElements, expectedPageNumber); expectedPageNumber++; } Assert.NotNull(recognizedForm.Fields); foreach (var field in recognizedForm.Fields.Values) { if (field == null) { continue; } Assert.NotNull(field.Name); Assert.That(field.Confidence, Is.GreaterThanOrEqualTo(0.0).Within(0.01)); Assert.That(field.Confidence, Is.LessThanOrEqualTo(1.0).Within(0.01)); ValidateFieldData(field.LabelData, includeFieldElements); if (field.Value.ValueType == FieldValueType.SelectionMark) { ValidateFieldData(field.ValueData, includeFieldElements, true); } else { ValidateFieldData(field.ValueData, includeFieldElements); } } }
private static InvoceModel MappingForm(RecognizedForm form) { var result = new InvoceModel() { CompanyPhone = form.Fields.Where(f => f.Key == "CompanyPhone").FirstOrDefault().Value?.ValueData?.Text, Date = form.Fields.Where(f => f.Key == "DatePurchase").FirstOrDefault().Value?.ValueData?.Text, Email = form.Fields.Where(f => f.Key == "Mail").FirstOrDefault().Value?.ValueData?.Text, PurchaseOrder = form.Fields.Where(f => f.Key == "PurchaseOrder").FirstOrDefault().Value?.ValueData?.Text, ShippedFromCompany = form.Fields.Where(f => f.Key == "ShippedFromCompany").FirstOrDefault().Value?.ValueData?.Text, ShippedtoCompany = form.Fields.Where(f => f.Key == "ShippedToCompany").FirstOrDefault().Value?.ValueData?.Text, ShippedToVendor = form.Fields.Where(f => f.Key == "ShippedToVendor").FirstOrDefault().Value?.ValueData?.Text, Subtotal = form.Fields.Where(f => f.Key == "Subtotal").FirstOrDefault().Value?.ValueData?.Text, Tax = form.Fields.Where(f => f.Key == "Tax").FirstOrDefault().Value?.ValueData?.Text, Total = form.Fields.Where(f => f.Key == "Total").FirstOrDefault().Value?.ValueData?.Text, WebSite = form.Fields.Where(f => f.Key == "WebSite").FirstOrDefault().Value?.ValueData?.Text }; return(result); }
public async Task StartRecognizeCustomFormsWithLabelsAndSelectionMarks(bool includeFieldElements) { var client = CreateFormRecognizerClient(); var options = new RecognizeCustomFormsOptions { IncludeFieldElements = includeFieldElements }; RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true, ContainerType.SelectionMarks); using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.FormSelectionMarks); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options); } await operation.WaitForCompletionAsync(); Assert.IsTrue(operation.HasValue); Assert.GreaterOrEqual(operation.Value.Count, 1); RecognizedForm form = operation.Value.Single(); ValidateRecognizedForm(form, includeFieldElements: includeFieldElements, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); // Testing that we shuffle things around correctly so checking only once per property. Assert.IsNotEmpty(form.FormType); Assert.IsNotNull(form.Fields); var name = "AMEX_SELECTION_MARK"; Assert.IsNotNull(form.Fields[name]); Assert.AreEqual(FieldValueType.SelectionMark, form.Fields[name].Value.ValueType); // If this assertion is failing after a recent update in the generated models, please remember // to update the manually added FieldValue_internal constructor in src/FieldValue_internal.cs if // necessary. The service originally returns "selected" and "unselected" as lowercase strings, // but we overwrite these values there. Consider removing this comment when: // https://github.com/Azure/azure-sdk-for-net/issues/17814 is fixed and the manually added constructor // is not needed anymore. Assert.AreEqual("Selected", form.Fields[name].ValueData.Text); }
public async Task StartRecognizeCustomFormsCanAuthenticateWithTokenCredential(bool useTrainingLabels) { var client = CreateFormRecognizerClient(useTokenCredential: true); await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels); RecognizeCustomFormsOperation operation; using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Form1); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } // Sanity check to make sure we got an actual response back from the service. RecognizedFormCollection formPage = await operation.WaitForCompletionAsync(); RecognizedForm form = formPage.Single(); Assert.NotNull(form); if (useTrainingLabels) { ValidateModelWithLabelsForm( form, trainedModel.ModelId, includeFieldElements: false, expectedFirstPageNumber: 1, expectedLastPageNumber: 1, isComposedModel: false); } else { ValidateModelWithNoLabelsForm( form, trainedModel.ModelId, includeFieldElements: false, expectedFirstPageNumber: 1, expectedLastPageNumber: 1); } }
private Facturae32.FileHeaderType CreateFileHeader(RecognizedForm form) { var fileHeader = new Facturae32.FileHeaderType() { SchemaVersion = Facturae32.SchemaVersionType.Item32, InvoiceIssuerType = Facturae32.InvoiceIssuerTypeType.TE, ThirdParty = AddThirdParty(), Modality = Facturae32.ModalityType.I, Batch = new Facturae32.BatchType() { BatchIdentifier = $"{form.GetFieldValue("SellerParty.TaxIdentification.TaxIdentificationNumber")}" + $"{form.GetFieldValue("Invoices.Invoice.InvoiceHeader.InvoiceNumber")}" + $"{form.GetFieldValue("Invoices.Invoice.InvoiceHeader.InvoiceSeriesCode")}", InvoiceCurrencyCode = Facturae32.CurrencyCodeType.EUR, InvoicesCount = 1, TotalInvoicesAmount = new Facturae32.AmountType() { TotalAmount = Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.InvoiceTotal"), cultureInfo) }, TotalOutstandingAmount = new Facturae32.AmountType() { TotalAmount = Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.InvoiceTotal"), cultureInfo) }, TotalExecutableAmount = new Facturae32.AmountType() { TotalAmount = Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.TotalExecutableAmount"), cultureInfo) } } }; if (!string.IsNullOrWhiteSpace(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.GeneralSurcharges.Charge.ChargeReason"))) { var chargeAmount = new Facturae32.DoubleSixDecimalType(Convert.ToDouble(form.GetFieldValue("Invoices.Invoice.InvoiceTotals.GeneralSurcharges.Charge.ChargeAmount"), cultureInfo)); fileHeader.Batch.TotalInvoicesAmount.TotalAmount += chargeAmount; fileHeader.Batch.TotalOutstandingAmount.TotalAmount += chargeAmount; } return(fileHeader); }