Пример #1
0
        public async Task StartRecognizeCustomFormsWithLabelsCanParseMultipageFormWithBlankPage()
        {
            var client  = CreateInstrumentedFormRecognizerClient();
            var options = new RecognizeOptions()
            {
                IncludeTextContent = true
            };
            RecognizeCustomFormsOperation operation;

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.InvoiceMultipageBlank);
            using (Recording.DisableRequestBodyRecording())
            {
                operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options);
            }

            RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync(PollingInterval);

            var recognizedForm = recognizedForms.Single();

            ValidateRecognizedForm(recognizedForm, includeTextContent: true,
                                   expectedFirstPageNumber: 1, expectedLastPageNumber: 3);

            for (int pageIndex = 0; pageIndex < recognizedForm.Pages.Count; pageIndex++)
            {
                if (pageIndex == 0 || pageIndex == 2)
                {
                    var formPage     = recognizedForm.Pages[pageIndex];
                    var sampleLine   = formPage.Lines[3];
                    var expectedText = pageIndex == 0 ? "Bilbo Baggins" : "Frodo Baggins";

                    Assert.AreEqual(expectedText, sampleLine.Text);
                }
            }

            var blankPage = recognizedForm.Pages[1];

            Assert.AreEqual(0, blankPage.Lines.Count);
            Assert.AreEqual(0, blankPage.Tables.Count);
        }
        private static async Task AnalyzeCustomForm(FormRecognizerClient recognizerClient, string modelID, string formUrl)
        {
            RecognizeCustomFormsOptions options = new RecognizeCustomFormsOptions();

            //recognize form
            RecognizeCustomFormsOperation operation = await recognizerClient.StartRecognizeCustomFormsFromUriAsync(modelID, new Uri(formUrl));

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection forms = operationResponse.Value;

            foreach (RecognizedForm form in forms)
            {
                returnString += $"Form of type: {form.FormType}{Environment.NewLine}";
                foreach (FormField field in form.Fields.Values)
                {
                    returnString += $"Field '{field.Name}: ";

                    if (field.LabelData != null)
                    {
                        returnString += $"    Label: '{field.LabelData.Text}";
                    }

                    returnString += $"    Value: '{field.ValueData.Text}";
                    returnString += $"    Confidence: '{field.Confidence}{Environment.NewLine}";
                }
                returnString += $"Table data:{Environment.NewLine}";
                foreach (FormPage page in form.Pages)
                {
                    for (int i = 0; i < page.Tables.Count; i++)
                    {
                        FormTable table = page.Tables[i];
                        //Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");
                        foreach (FormTableCell cell in table.Cells)
                        {
                            returnString += $"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) contains {(cell.IsHeader ? "header" : "text")}: '{cell.Text}'{Environment.NewLine}";
                        }
                    }
                }
            }
        }
Пример #3
0
        public async Task RecognizeReceiptFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string receiptPath = FormRecognizerTestEnvironment.CreatePath("contoso-receipt.jpg");

            #region Snippet:FormRecognizerRecognizeReceiptFromFile
            using (FileStream stream = new FileStream(receiptPath, FileMode.Open))
            {
                RecognizedFormCollection receipts = await client.StartRecognizeReceipts(stream).WaitForCompletionAsync();

                /*
                 *
                 */
            }
            #endregion
        }
    // </snippet_trainlabels_response>

    // <snippet_analyze>
    // Analyze PDF form data
    private static async Task AnalyzePdfForm(
        FormRecognizerClient recognizerClient, String modelId, string formUrl)
    {
        RecognizedFormCollection forms = await recognizerClient
                                         .StartRecognizeCustomFormsFromUri(modelId, new Uri(formUrl))
                                         .WaitForCompletionAsync();

        // </snippet_analyze>
        // <snippet_analyze_response>
        foreach (RecognizedForm form in forms)
        {
            Console.WriteLine($"Form of type: {form.FormType}");
            foreach (FormField field in form.Fields.Values)
            {
                Console.WriteLine($"Field '{field.Name}: ");

                if (field.LabelData != null)
                {
                    Console.WriteLine($"    Label: '{field.LabelData.Text}");
                }

                Console.WriteLine($"    Value: '{field.ValueData.Text}");
                Console.WriteLine($"    Confidence: '{field.Confidence}");
            }
            Console.WriteLine("Table data:");
            foreach (FormPage page in form.Pages)
            {
                for (int i = 0; i < page.Tables.Count; i++)
                {
                    FormTable table = page.Tables[i];
                    Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");
                    foreach (FormTableCell cell in table.Cells)
                    {
                        Console.WriteLine($"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) contains {(cell.IsHeader ? "header" : "text")}: '{cell.Text}'");
                    }
                }
            }
        }
    }
Пример #5
0
        public async Task StartRecognizeCustomFormsWithLabelsCanParseMultipageForms(bool useStream, bool includeTextContent)
        {
            var client  = CreateInstrumentedFormRecognizerClient();
            var options = new RecognizeOptions()
            {
                IncludeTextContent = includeTextContent
            };
            RecognizeCustomFormsOperation operation;

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : true);

            if (useStream)
            {
                using var stream = new FileStream(FormRecognizerTestEnvironment.MultipageFormPath, FileMode.Open);
                using (Recording.DisableRequestBodyRecording())
                {
                    operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options);
                }
            }
            else
            {
                var uri = new Uri(FormRecognizerTestEnvironment.MultipageFormUri);
                operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri, options);
            }

            RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync();

            var recognizedForm = recognizedForms.Single();

            ValidateRecognizedForm(recognizedForm, includeTextContent,
                                   expectedFirstPageNumber: 1, expectedLastPageNumber: 2);

            for (int formIndex = 0; formIndex < recognizedForms.Count; formIndex++)
            {
                // Basic sanity test to make sure pages are ordered correctly.
                // TODO: implement sanity check once #11821 is solved.
            }
        }
        private async Task <List <dynamic> > Evaluate(RecognizeCustomFormsOperation operation)
        {
            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection forms = operationResponse.Value;

            List <dynamic> res = new();

            foreach (RecognizedForm form in forms)
            {
                _logger.LogInformation($"Form of type: {form.FormType}");
                _logger.LogInformation($"Form analyzed with model ID: {form.ModelId}");

                dynamic exo = new System.Dynamic.ExpandoObject();

                foreach (FormField field in form.Fields.Values)
                {
                    ((IDictionary <string, object>)exo).Add(field.Name, new { Value = field?.ValueData?.Text, field?.Confidence });
                }
                res.Add(exo);
            }
            return(res);
        }
        // Analyze PNG form data
        private static async Task AnalyzePngForm(
            FormTrainingClient formTrainingClient, string modelId, string pngFormFile)
        {
            if (string.IsNullOrEmpty(pngFormFile))
            {
                Console.WriteLine("\nInvalid pngFormFile.");
                return;
            }

            try
            {
                using (FileStream stream = new FileStream(pngFormFile, FileMode.Open))
                {
                    RecognizedFormCollection result = await formTrainingClient.GetFormRecognizerClient().StartRecognizeCustomFormsAsync(modelId, stream).WaitForCompletionAsync();

                    Console.WriteLine("\nExtracted data from:" + pngFormFile);
                    DisplayAnalyzeResult(result);
                }
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Analyze PNG  form  : " + ex.Message);
            }
        }
Пример #8
0
        public async Task RecognizeReceiptsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleCreateClient
            FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            #endregion

            string receiptPath = FormRecognizerTestEnvironment.CreatePath("contoso-receipt.jpg");

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileStream
            using (FileStream stream = new FileStream(receiptPath, FileMode.Open))
            {
                RecognizedFormCollection receipts = await client.StartRecognizeReceiptsAsync(stream).WaitForCompletionAsync();

                // 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

                foreach (RecognizedForm receipt in receipts)
                {
                    FormField merchantNameField;
                    if (receipt.Fields.TryGetValue("MerchantName", out merchantNameField))
                    {
                        if (merchantNameField.Value.Type == FieldValueType.String)
                        {
                            string merchantName = merchantNameField.Value.AsString();

                            Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {merchantNameField.Confidence}");
                        }
                    }

                    FormField transactionDateField;
                    if (receipt.Fields.TryGetValue("TransactionDate", out transactionDateField))
                    {
                        if (transactionDateField.Value.Type == FieldValueType.Date)
                        {
                            DateTime transactionDate = transactionDateField.Value.AsDate();

                            Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {transactionDateField.Confidence}");
                        }
                    }

                    FormField itemsField;
                    if (receipt.Fields.TryGetValue("Items", out itemsField))
                    {
                        if (itemsField.Value.Type == FieldValueType.List)
                        {
                            foreach (FormField itemField in itemsField.Value.AsList())
                            {
                                Console.WriteLine("Item:");

                                if (itemField.Value.Type == FieldValueType.Dictionary)
                                {
                                    IReadOnlyDictionary <string, FormField> itemFields = itemField.Value.AsDictionary();

                                    FormField itemNameField;
                                    if (itemFields.TryGetValue("Name", out itemNameField))
                                    {
                                        if (itemNameField.Value.Type == FieldValueType.String)
                                        {
                                            string itemName = itemNameField.Value.AsString();

                                            Console.WriteLine($"    Name: '{itemName}', with confidence {itemNameField.Confidence}");
                                        }
                                    }

                                    FormField itemTotalPriceField;
                                    if (itemFields.TryGetValue("TotalPrice", out itemTotalPriceField))
                                    {
                                        if (itemTotalPriceField.Value.Type == FieldValueType.Float)
                                        {
                                            float itemTotalPrice = itemTotalPriceField.Value.AsFloat();

                                            Console.WriteLine($"    Total Price: '{itemTotalPrice}', with confidence {itemTotalPriceField.Confidence}");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    FormField totalField;
                    if (receipt.Fields.TryGetValue("Total", out totalField))
                    {
                        if (totalField.Value.Type == FieldValueType.Float)
                        {
                            float total = totalField.Value.AsFloat();

                            Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'");
                        }
                    }
                }
            }
            #endregion
        }
Пример #9
0
        public static async Task RunAsync([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log)
        {
            //Extracting content type and url of the blob triggering the function
            var jsondata = JsonConvert.SerializeObject(eventGridEvent.Data);
            var tmp      = new { contentType = "", url = "" };
            var data     = JsonConvert.DeserializeAnonymousType(jsondata, tmp);

            //Checking if the trigger was iniatiated for a PDF File.
            if (data.contentType == "application/pdf")
            {
                var pdfUrl = data.url;

                string endpoint = System.Environment.GetEnvironmentVariable("FormsRecognizerEndpoint", EnvironmentVariableTarget.Process);
                string apiKey   = System.Environment.GetEnvironmentVariable("FormsRecognizerKey", EnvironmentVariableTarget.Process);
                string contosoStorageConnectionString = System.Environment.GetEnvironmentVariable("ContosoStorageConnectionString", EnvironmentVariableTarget.Process);
                string cosmosEndpointUrl = System.Environment.GetEnvironmentVariable("CosmosDBEndpointUrl", EnvironmentVariableTarget.Process);
                string cosmosPrimaryKey  = System.Environment.GetEnvironmentVariable("CosmosDBPrimaryKey", EnvironmentVariableTarget.Process);

                //Create a SAS Link to give Forms Recognizer read access to the document
                BlobServiceClient   blobServiceClient = new BlobServiceClient(contosoStorageConnectionString);
                BlobContainerClient container         = new BlobContainerClient(contosoStorageConnectionString, "claims");
                string         blobName   = pdfUrl.Split('/').Last();
                BlobClient     blob       = container.GetBlobClient(blobName);
                BlobSasBuilder sasBuilder = new BlobSasBuilder()
                {
                    BlobContainerName = blob.GetParentBlobContainerClient().Name,
                    BlobName          = blob.Name,
                    Resource          = "b"
                };
                sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
                sasBuilder.SetPermissions(BlobSasPermissions.Read);
                Uri sasUri = blob.GenerateSasUri(sasBuilder);

                var credential = new AzureKeyCredential(apiKey);

                //Get the latest trained model
                var formTrainingClient = new FormTrainingClient(new Uri(endpoint), credential);
                Pageable <CustomFormModelInfo> formsModels = formTrainingClient.GetCustomModels();
                var latestModel = (from inc in formsModels orderby inc.TrainingCompletedOn descending select inc).FirstOrDefault();

                //Run the document through the model
                var formRecognizerClient = new FormRecognizerClient(new Uri(endpoint), credential);
                var options = new RecognizeCustomFormsOptions()
                {
                    IncludeFieldElements = true
                };
                RecognizeCustomFormsOperation             operation         = formRecognizerClient.StartRecognizeCustomFormsFromUri(latestModel.ModelId, sasUri, options);
                Azure.Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

                RecognizedFormCollection forms = operationResponse.Value;

                //Insert documents into CosmosDB
                var cosmosClient    = new CosmosClient(cosmosEndpointUrl, cosmosPrimaryKey);
                var cosmosDatabase  = (await cosmosClient.CreateDatabaseIfNotExistsAsync("Contoso")).Database;
                var cosmosContainer = (await cosmosDatabase.CreateContainerIfNotExistsAsync("Claims", "/InsuredID")).Container;

                Model.ClaimsDocument processedDocument = new Model.ClaimsDocument();
                processedDocument.DocumentDate = new DateTime(int.Parse(blobName.Substring(0, 4)),
                                                              int.Parse(blobName.Substring(4, 2)), int.Parse(blobName.Substring(6, 2)));
                processedDocument.PatientName = forms[0].Fields["PatientName"].ValueData?.Text;
                processedDocument.InsuredID   = forms[0].Fields["InsuredID"].ValueData?.Text;
                processedDocument.Diagnosis   = forms[0].Fields["Diagnosis"].ValueData?.Text;
                decimal.TryParse(forms[0].Fields["TotalCharges"].ValueData?.Text, out decimal totalCharges);
                processedDocument.TotalCharges = totalCharges;
                DateTime.TryParse(forms[0].Fields["PatientBirthDate"].ValueData?.Text, out DateTime patientBirthDate);
                processedDocument.PatientBirthDate = patientBirthDate;
                decimal.TryParse(forms[0].Fields["AmountPaid"].ValueData?.Text, out decimal amountPaid);
                processedDocument.AmountPaid = amountPaid;
                decimal.TryParse(forms[0].Fields["AmountDue"].ValueData?.Text, out decimal amountDue);
                processedDocument.AmountDue = amountDue;
                processedDocument.FileName  = blobName;
                if (processedDocument.InsuredID == null)
                {
                    processedDocument.Id = Guid.NewGuid().ToString();
                }
                else
                {
                    processedDocument.Id = processedDocument.InsuredID;
                }

                try
                {
                    ItemResponse <Model.ClaimsDocument> cosmosResponse = await
                                                                         cosmosContainer.CreateItemAsync(processedDocument, new PartitionKey(processedDocument.InsuredID));
                }
                catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    //Conflicting EnsurerID is silently ignored for demo purposes.
                }
            }
            log.LogInformation(eventGridEvent.Data.ToString());
        }
Пример #10
0
        public async Task OutputModelsTrainedWithDynamicRowsTables()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.TableDynamicRowsContainerSasUrlV2;
            string formFilePath    = FormRecognizerTestEnvironment.CreatePath("label_table_dynamic_rows1.pdf");

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

            // Model trained with labeled table with dynamic rows.
            CustomFormModel modelTrainedWithLabels = await trainingClient.StartTrainingAsync(new Uri(trainingFileUrl), useTrainingLabels : true).WaitForCompletionAsync();

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                RecognizedFormCollection forms = await client.StartRecognizeCustomFormsAsync(modelTrainedWithLabels.ModelId, stream).WaitForCompletionAsync();

                Console.WriteLine("---------Recognized labeled table with dynamic rows---------");
                foreach (RecognizedForm form in forms)
                {
                    foreach (FormField field in form.Fields.Values)
                    {
                        /// Substitute "table" for the label given to the table tag during training
                        /// (if different than sample training docs).
                        if (field.Name == "table")
                        {
                            if (field.Value.ValueType == FieldValueType.List)
                            {
                                var table = field.Value.AsList();
                                //columns
                                for (int i = 0; i < table.Count; i++)
                                {
                                    Console.WriteLine($"Row {i+1}:");
                                    var row = table[i].Value.AsDictionary();
                                    foreach (var colValues in row)
                                    {
                                        Console.Write($"  Col {colValues.Key}. Value: ");
                                        if (colValues.Value != null)
                                        {
                                            var rowContent = colValues.Value;
                                            if (rowContent.Value.ValueType == FieldValueType.String)
                                            {
                                                Console.WriteLine($"'{rowContent.Value.AsString()}'");
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine("Empty cell.");
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine($"Field {field.Name}: ");
                            Console.WriteLine($"    Value: '{field.ValueData.Text}");
                            Console.WriteLine($"    Confidence: '{field.Confidence}");
                        }
                    }
                }
            }
        }
Пример #11
0
        public async Task <RecognizedFormCollection> AnalyzeCustomFormFromStream(Stream form, string modelId)
        {
            RecognizedFormCollection collection = await _frClient.StartRecognizeCustomFormsAsync(modelId, form).WaitForCompletionAsync();

            return(collection);
        }
Пример #12
0
        private RecognizedForm ProcessResults(RecognizedFormCollection invoices)
        {
            // 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();

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

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

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

            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("VendorAddress", out FormField vendorAddressField))
            {
                if (vendorAddressField.Value.ValueType == FieldValueType.String)
                {
                    string vendorAddress = vendorAddressField.Value.AsString();
                    Console.WriteLine($"Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.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("CustomerAddress", out FormField customerAddressField))
            {
                if (customerAddressField.Value.ValueType == FieldValueType.String)
                {
                    string customerAddress = customerAddressField.Value.AsString();
                    Console.WriteLine($"Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}");
                }
            }

            if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out FormField customerAddressRecipientField))
            {
                if (customerAddressRecipientField.Value.ValueType == FieldValueType.String)
                {
                    string customerAddressRecipient = customerAddressRecipientField.Value.AsString();
                    Console.WriteLine($"Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.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}");
                }
            }

            return(invoice);
        }
Пример #13
0
        public async Task RecognizeBusinessCardsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

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

            #region Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream
            //@@ string businessCardsPath = "<businessCardsPath>";

            using var stream = new FileStream(businessCardsPath, FileMode.Open);
            var options = new RecognizeBusinessCardsOptions()
            {
                Locale = "en-US"
            };

            RecognizeBusinessCardsOperation operation = await client.StartRecognizeBusinessCardsAsync(stream, options);

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection businessCards = operationResponse.Value;

            // To see the list of the supported fields returned by service and its corresponding types, consult:
            // https://aka.ms/formrecognizer/businesscardfields

            foreach (RecognizedForm businessCard in businessCards)
            {
                if (businessCard.Fields.TryGetValue("ContactNames", out FormField contactNamesField))
                {
                    if (contactNamesField.Value.ValueType == FieldValueType.List)
                    {
                        foreach (FormField contactNameField in contactNamesField.Value.AsList())
                        {
                            Console.WriteLine($"Contact Name: {contactNameField.ValueData.Text}");

                            if (contactNameField.Value.ValueType == FieldValueType.Dictionary)
                            {
                                IReadOnlyDictionary <string, FormField> contactNameFields = contactNameField.Value.AsDictionary();

                                if (contactNameFields.TryGetValue("FirstName", out FormField firstNameField))
                                {
                                    if (firstNameField.Value.ValueType == FieldValueType.String)
                                    {
                                        string firstName = firstNameField.Value.AsString();

                                        Console.WriteLine($"  First Name: '{firstName}', with confidence {firstNameField.Confidence}");
                                    }
                                }

                                if (contactNameFields.TryGetValue("LastName", out FormField lastNameField))
                                {
                                    if (lastNameField.Value.ValueType == FieldValueType.String)
                                    {
                                        string lastName = lastNameField.Value.AsString();

                                        Console.WriteLine($"  Last Name: '{lastName}', with confidence {lastNameField.Confidence}");
                                    }
                                }
                            }
                        }
                    }
                }

                if (businessCard.Fields.TryGetValue("Emails", out FormField emailFields))
                {
                    if (emailFields.Value.ValueType == FieldValueType.List)
                    {
                        foreach (FormField emailField in emailFields.Value.AsList())
                        {
                            if (emailField.Value.ValueType == FieldValueType.String)
                            {
                                string email = emailField.Value.AsString();

                                Console.WriteLine($"Email: '{email}', with confidence {emailField.Confidence}");
                            }
                        }
                    }
                }
            }
            #endregion
        }
Пример #14
0
        public async Task RecognizeIdentityDocumentsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:FormRecognizerSampleRecognizeIdentityDocumentsFileStream
#if SNIPPET
            string sourcePath = "<sourcePath>";
#else
            string sourcePath = FormRecognizerTestEnvironment.CreatePath("license.jpg");
#endif

            using var stream = new FileStream(sourcePath, FileMode.Open);

            RecognizeIdentityDocumentsOperation operation = await client.StartRecognizeIdentityDocumentsAsync(stream);

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection identityDocuments = operationResponse.Value;

            // To see the list of all the supported fields returned by service and its corresponding types, consult:
            // https://aka.ms/formrecognizer/iddocumentfields

            RecognizedForm identityDocument = identityDocuments.Single();

            if (identityDocument.Fields.TryGetValue("Address", out FormField addressField))
            {
                if (addressField.Value.ValueType == FieldValueType.String)
                {
                    string address = addressField.Value.AsString();
                    Console.WriteLine($"Address: '{address}', with confidence {addressField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("CountryRegion", out FormField countryRegionField))
            {
                if (countryRegionField.Value.ValueType == FieldValueType.CountryRegion)
                {
                    string countryRegion = countryRegionField.Value.AsCountryRegion();
                    Console.WriteLine($"CountryRegion: '{countryRegion}', with confidence {countryRegionField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("DateOfBirth", out FormField dateOfBirthField))
            {
                if (dateOfBirthField.Value.ValueType == FieldValueType.Date)
                {
                    DateTime dateOfBirth = dateOfBirthField.Value.AsDate();
                    Console.WriteLine($"Date Of Birth: '{dateOfBirth}', with confidence {dateOfBirthField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("DateOfExpiration", out FormField dateOfExpirationField))
            {
                if (dateOfExpirationField.Value.ValueType == FieldValueType.Date)
                {
                    DateTime dateOfExpiration = dateOfExpirationField.Value.AsDate();
                    Console.WriteLine($"Date Of Expiration: '{dateOfExpiration}', with confidence {dateOfExpirationField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("DocumentNumber", out FormField documentNumberField))
            {
                if (documentNumberField.Value.ValueType == FieldValueType.String)
                {
                    string documentNumber = documentNumberField.Value.AsString();
                    Console.WriteLine($"Document Number: '{documentNumber}', with confidence {documentNumberField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("FirstName", out FormField firstNameField))
            {
                if (firstNameField.Value.ValueType == FieldValueType.String)
                {
                    string firstName = firstNameField.Value.AsString();
                    Console.WriteLine($"First Name: '{firstName}', with confidence {firstNameField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("LastName", out FormField lastNameField))
            {
                if (lastNameField.Value.ValueType == FieldValueType.String)
                {
                    string lastName = lastNameField.Value.AsString();
                    Console.WriteLine($"Last Name: '{lastName}', with confidence {lastNameField.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("Region", out FormField regionfield))
            {
                if (regionfield.Value.ValueType == FieldValueType.String)
                {
                    string region = regionfield.Value.AsString();
                    Console.WriteLine($"Region: '{region}', with confidence {regionfield.Confidence}");
                }
            }

            if (identityDocument.Fields.TryGetValue("Sex", out FormField sexfield))
            {
                if (sexfield.Value.ValueType == FieldValueType.String)
                {
                    string sex = sexfield.Value.AsString();
                    Console.WriteLine($"Sex: '{sex}', with confidence {sexfield.Confidence}");
                }
            }

            #endregion
        }
Пример #15
0
    // </snippet_bc_print>

    // <snippet_invoice_call>
    private static async Task AnalyzeInvoice(
        FormRecognizerClient recognizerClient, string invoiceUrl)
    {
        var options = new RecognizeInvoicesOptions()
        {
            Locale = "en-US"
        };
        RecognizedFormCollection invoices = await recognizerClient.StartRecognizeInvoicesFromUriAsync(invoiceUrl, options).WaitForCompletionAsync();

        // </snippet_invoice_call>
        // <snippet_invoice_print>
        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}");
            }
        }
    }
Пример #16
0
        public async Task StronglyTypingARecognizedForm()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:FormRecognizerSampleStronglyTypingARecognizedForm
#if SNIPPET
            Uri receiptUri = new Uri("<receiptUri>");
#else
            Uri receiptUri = FormRecognizerTestEnvironment.CreateUri("contoso-receipt.jpg");
#endif

            RecognizeReceiptsOperation operation = await client.StartRecognizeReceiptsFromUriAsync(receiptUri);

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection recognizedForms = operationResponse.Value;

            foreach (RecognizedForm recognizedForm in recognizedForms)
            {
                Receipt receipt = new Receipt(recognizedForm);

                if (receipt.MerchantName != null)
                {
                    string merchantName = receipt.MerchantName;
                    Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {receipt.MerchantName.Confidence}");
                }

                if (receipt.TransactionDate != null)
                {
                    DateTime transactionDate = receipt.TransactionDate;
                    Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {receipt.TransactionDate.Confidence}");
                }

                foreach (ReceiptItem item in receipt.Items)
                {
                    Console.WriteLine("Item:");

                    if (item.Name != null)
                    {
                        string name = item.Name;
                        Console.WriteLine($"  Name: '{name}', with confidence {item.Name.Confidence}");
                    }

                    if (item.TotalPrice != null)
                    {
                        float totalPrice = item.TotalPrice;
                        Console.WriteLine($"  Total Price: '{totalPrice}', with confidence {item.TotalPrice.Confidence}");
                    }
                }

                if (receipt.Total != null)
                {
                    float total = receipt.Total;
                    Console.WriteLine($"Total: '{total}', with confidence {receipt.Total.Confidence}");
                }
            }
            #endregion
        }
Пример #17
0
        public async Task StartRecognizeCustomFormsWithoutLabelsCanParseMultipageFormWithBlankPage(bool useStream)
        {
            var client  = CreateFormRecognizerClient();
            var options = new RecognizeCustomFormsOptions()
            {
                IncludeFieldElements = true
            };
            RecognizeCustomFormsOperation operation;

            await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false);

            if (useStream)
            {
                using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.InvoiceMultipageBlank);
                using (Recording.DisableRequestBodyRecording())
                {
                    operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream, options);
                }
            }
            else
            {
                var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.InvoiceMultipageBlank);
                operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri, 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;

                ValidateModelWithNoLabelsForm(
                    recognizedForm,
                    trainedModel.ModelId,
                    includeFieldElements: true,
                    expectedFirstPageNumber: expectedPageNumber,
                    expectedLastPageNumber: expectedPageNumber);

                // Basic sanity test to make sure pages are ordered correctly.

                if (formIndex == 0 || formIndex == 2)
                {
                    var expectedValueData = formIndex == 0 ? "300.00" : "3000.00";

                    FormField fieldInPage = recognizedForm.Fields.Values.Where(field => field.LabelData.Text.Contains("Subtotal:")).FirstOrDefault();
                    Assert.IsNotNull(fieldInPage);
                    Assert.IsNotNull(fieldInPage.ValueData);
                    Assert.AreEqual(expectedValueData, fieldInPage.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);
        }
Пример #18
0
        public async Task StartRecognizeBusinessCardsCanParseMultipageForm(bool useStream)
        {
            var client  = CreateFormRecognizerClient();
            var options = new RecognizeBusinessCardsOptions()
            {
                IncludeFieldElements = true
            };
            RecognizeBusinessCardsOperation operation;

            if (useStream)
            {
                using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.BusinessMultipage);
                using (Recording.DisableRequestBodyRecording())
                {
                    operation = await client.StartRecognizeBusinessCardsAsync(stream, options);
                }
            }
            else
            {
                var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.BusinessMultipage);
                operation = await client.StartRecognizeBusinessCardsFromUriAsync(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.
                Assert.IsTrue(recognizedForm.Fields.ContainsKey("Emails"));
                FormField sampleFields = recognizedForm.Fields["Emails"];
                Assert.AreEqual(FieldValueType.List, sampleFields.Value.ValueType);
                var field = sampleFields.Value.AsList().Single();

                if (formIndex == 0)
                {
                    Assert.AreEqual("*****@*****.**", field.ValueData.Text);
                }
                else if (formIndex == 1)
                {
                    Assert.AreEqual("*****@*****.**", field.ValueData.Text);
                }

                // Check for ContactNames.Page value
                Assert.IsTrue(recognizedForm.Fields.ContainsKey("ContactNames"));
                FormField contactNameField = recognizedForm.Fields["ContactNames"].Value.AsList().Single();
                Assert.AreEqual(formIndex + 1, contactNameField.ValueData.PageNumber);
            }
        }
        public async Task CreateComposedModel()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.BlobContainerSasUrlV2;

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

            #region Snippet:FormRecognizerSampleTrainVariousModels
            // For this sample, you can use the training forms found in the `trainingFiles` folder.
            // Upload the forms to your storage container and then generate a container SAS URL.
            // For instructions on setting up forms for training in an Azure Storage Blob Container, see
            // https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-training-data-set#upload-your-training-data

            bool useLabels = true;

#if SNIPPET
            Uri officeSuppliesUri = new Uri("<purchaseOrderOfficeSuppliesUri>");
#else
            Uri officeSuppliesUri = new Uri(trainingFileUrl);
#endif
            string suppliesModelName = "Purchase order - Office supplies";

            TrainingOperation suppliesOperation = await client.StartTrainingAsync(officeSuppliesUri, useLabels, suppliesModelName);

            Response <CustomFormModel> suppliesOperationResponse = await suppliesOperation.WaitForCompletionAsync();

            CustomFormModel officeSuppliesModel = suppliesOperationResponse.Value;

#if SNIPPET
            Uri officeEquipmentUri = new Uri("<purchaseOrderOfficeEquipmentUri>");
#else
            Uri officeEquipmentUri = new Uri(trainingFileUrl);
#endif
            string equipmentModelName = "Purchase order - Office Equipment";

            TrainingOperation equipmentOperation = await client.StartTrainingAsync(officeEquipmentUri, useLabels, equipmentModelName);

            Response <CustomFormModel> equipmentOperationResponse = await equipmentOperation.WaitForCompletionAsync();

            CustomFormModel officeEquipmentModel = equipmentOperationResponse.Value;

#if SNIPPET
            Uri furnitureUri = new Uri("<purchaseOrderFurnitureUri>");
#else
            Uri furnitureUri = new Uri(trainingFileUrl);
#endif
            string furnitureModelName = "Purchase order - Furniture";

            TrainingOperation furnitureOperation = await client.StartTrainingAsync(furnitureUri, useLabels, furnitureModelName);

            Response <CustomFormModel> furnitureOperationResponse = await furnitureOperation.WaitForCompletionAsync();

            CustomFormModel furnitureModel = furnitureOperationResponse.Value;

#if SNIPPET
            Uri cleaningSuppliesUri = new Uri("<purchaseOrderCleaningSuppliesUri>");
#else
            Uri cleaningSuppliesUri = new Uri(trainingFileUrl);
#endif
            string cleaningModelName = "Purchase order - Cleaning Supplies";

            TrainingOperation cleaningOperation = await client.StartTrainingAsync(cleaningSuppliesUri, useLabels, cleaningModelName);

            Response <CustomFormModel> cleaningOperationResponse = await cleaningOperation.WaitForCompletionAsync();

            CustomFormModel cleaningSuppliesModel = cleaningOperationResponse.Value;

            #endregion

            #region Snippet:FormRecognizerSampleCreateComposedModelV3

            List <string> modelIds = new List <string>()
            {
                officeSuppliesModel.ModelId,
                officeEquipmentModel.ModelId,
                furnitureModel.ModelId,
                cleaningSuppliesModel.ModelId
            };

            string purchaseModelName = "Composed Purchase order";
            CreateComposedModelOperation operation = await client.StartCreateComposedModelAsync(modelIds, purchaseModelName);

            Response <CustomFormModel> operationResponse = await operation.WaitForCompletionAsync();

            CustomFormModel purchaseOrderModel = operationResponse.Value;

            Console.WriteLine($"Purchase Order Model Info:");
            Console.WriteLine($"  Is composed model: {purchaseOrderModel.Properties.IsComposedModel}");
            Console.WriteLine($"  Model Id: {purchaseOrderModel.ModelId}");
            Console.WriteLine($"  Model name: {purchaseOrderModel.ModelName}");
            Console.WriteLine($"  Model Status: {purchaseOrderModel.Status}");
            Console.WriteLine($"  Create model started on: {purchaseOrderModel.TrainingStartedOn}");
            Console.WriteLine($"  Create model completed on: {purchaseOrderModel.TrainingCompletedOn}");

            #endregion

            #region Snippet:FormRecognizerSampleSubmodelsInComposedModel

            Dictionary <string, List <TrainingDocumentInfo> > trainingDocsPerModel;
            trainingDocsPerModel = purchaseOrderModel.TrainingDocuments.GroupBy(doc => doc.ModelId).ToDictionary(g => g.Key, g => g.ToList());

            Console.WriteLine($"The purchase order model is based on {purchaseOrderModel.Submodels.Count} models");

            foreach (CustomFormSubmodel model in purchaseOrderModel.Submodels)
            {
                Console.WriteLine($"  Model Id: {model.ModelId}");
                Console.WriteLine("  The documents used to trained the model are: ");
                foreach (var doc in trainingDocsPerModel[model.ModelId])
                {
                    Console.WriteLine($"    {doc.Name}");
                }
            }

            #endregion

            #region Snippet:FormRecognizerSampleRecognizeCustomFormWithComposedModel

#if SNIPPET
            string purchaseOrderFilePath = "<purchaseOrderFilePath>";
#else
            string purchaseOrderFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
#endif
            FormRecognizerClient recognizeClient = client.GetFormRecognizerClient();
            using var stream = new FileStream(purchaseOrderFilePath, FileMode.Open);

            RecognizeCustomFormsOperation recognizeOperation = await recognizeClient.StartRecognizeCustomFormsAsync(purchaseOrderModel.ModelId, stream);

            Response <RecognizedFormCollection> recognizeOperationResponse = await recognizeOperation.WaitForCompletionAsync();

            RecognizedFormCollection forms = recognizeOperationResponse.Value;

            // Find labeled field.
            foreach (RecognizedForm form in forms)
            {
                // Setting an arbitrary confidence level
                if (form.FormTypeConfidence.Value > 0.9)
                {
                    if (form.Fields.TryGetValue("Total", out FormField field))
                    {
                        Console.WriteLine($"Total value in the form `{form.FormType}` is `{field.ValueData.Text}`");
                    }
                }
                else
                {
                    Console.WriteLine("Unable to recognize form.");
                }
            }

            #endregion

            // Delete the models on completion to clean environment.
            await client.DeleteModelAsync(officeSuppliesModel.ModelId).ConfigureAwait(false);

            await client.DeleteModelAsync(officeEquipmentModel.ModelId).ConfigureAwait(false);

            await client.DeleteModelAsync(furnitureModel.ModelId).ConfigureAwait(false);

            await client.DeleteModelAsync(cleaningSuppliesModel.ModelId).ConfigureAwait(false);

            await client.DeleteModelAsync(purchaseOrderModel.ModelId).ConfigureAwait(false);
        }
Пример #20
0
    // </snippet_invoice_print>

    // <snippet_id_call>
    private static async Task AnalyzeId(
        FormRecognizerClient recognizerClient, string idUrl)
    {
        RecognizedFormCollection businessCards = await recognizerClient.StartRecognizeIdDocumentsFromUriAsync(idUrl).WaitForCompletionAsync();

        //</snippet_id_call>
        //<snippet_id_print>

        RecognizedForm idDocument = idDocuments.Single();

        if (idDocument.Fields.TryGetValue("Address", out FormField addressField))
        {
            if (addressField.Value.ValueType == FieldValueType.String)
            {
                string address = addressField.Value.AsString();
                Console.WriteLine($"Address: '{address}', with confidence {addressField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("Country", out FormField countryField))
        {
            if (countryField.Value.ValueType == FieldValueType.Country)
            {
                string country = countryField.Value.AsCountryCode();
                Console.WriteLine($"Country: '{country}', with confidence {countryField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("DateOfBirth", out FormField dateOfBirthField))
        {
            if (dateOfBirthField.Value.ValueType == FieldValueType.Date)
            {
                DateTime dateOfBirth = dateOfBirthField.Value.AsDate();
                Console.WriteLine($"Date Of Birth: '{dateOfBirth}', with confidence {dateOfBirthField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("DateOfExpiration", out FormField dateOfExpirationField))
        {
            if (dateOfExpirationField.Value.ValueType == FieldValueType.Date)
            {
                DateTime dateOfExpiration = dateOfExpirationField.Value.AsDate();
                Console.WriteLine($"Date Of Expiration: '{dateOfExpiration}', with confidence {dateOfExpirationField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("DocumentNumber", out FormField documentNumberField))
        {
            if (documentNumberField.Value.ValueType == FieldValueType.String)
            {
                string documentNumber = documentNumberField.Value.AsString();
                Console.WriteLine($"Document Number: '{documentNumber}', with confidence {documentNumberField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("FirstName", out FormField firstNameField))
        {
            if (firstNameField.Value.ValueType == FieldValueType.String)
            {
                string firstName = firstNameField.Value.AsString();
                Console.WriteLine($"First Name: '{firstName}', with confidence {firstNameField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("LastName", out FormField lastNameField))
        {
            if (lastNameField.Value.ValueType == FieldValueType.String)
            {
                string lastName = lastNameField.Value.AsString();
                Console.WriteLine($"Last Name: '{lastName}', with confidence {lastNameField.Confidence}");
            }
        }

        if (idDocument.Fields.TryGetValue("Region", out FormField regionfield))
        {
            if (regionfield.Value.ValueType == FieldValueType.String)
            {
                string region = regionfield.Value.AsString();
                Console.WriteLine($"Region: '{region}', with confidence {regionfield.Confidence}");
            }
        }
    }
    // </snippet_getcontent_print>

    // <snippet_receipt_call>
    private static async Task AnalyzeReceipt(
        FormRecognizerClient recognizerClient, string receiptUri)
    {
        RecognizedFormCollection receipts = await recognizerClient.StartRecognizeReceiptsFromUri(new Uri(receiptUrl)).WaitForCompletionAsync();

        // </snippet_receipt_call>

        // <snippet_receipt_print>
        foreach (RecognizedForm receipt in receipts)
        {
            FormField merchantNameField;
            if (receipt.Fields.TryGetValue("MerchantName", out merchantNameField))
            {
                if (merchantNameField.Value.ValueType == FieldValueType.String)
                {
                    string merchantName = merchantNameField.Value.AsString();

                    Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {merchantNameField.Confidence}");
                }
            }

            FormField transactionDateField;
            if (receipt.Fields.TryGetValue("TransactionDate", out transactionDateField))
            {
                if (transactionDateField.Value.ValueType == FieldValueType.Date)
                {
                    DateTime transactionDate = transactionDateField.Value.AsDate();

                    Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {transactionDateField.Confidence}");
                }
            }

            FormField itemsField;
            if (receipt.Fields.TryGetValue("Items", out 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();

                            FormField itemNameField;
                            if (itemFields.TryGetValue("Name", out itemNameField))
                            {
                                if (itemNameField.Value.ValueType == FieldValueType.String)
                                {
                                    string itemName = itemNameField.Value.AsString();

                                    Console.WriteLine($"    Name: '{itemName}', with confidence {itemNameField.Confidence}");
                                }
                            }

                            FormField itemTotalPriceField;
                            if (itemFields.TryGetValue("TotalPrice", out itemTotalPriceField))
                            {
                                if (itemTotalPriceField.Value.ValueType == FieldValueType.Float)
                                {
                                    float itemTotalPrice = itemTotalPriceField.Value.AsFloat();

                                    Console.WriteLine($"    Total Price: '{itemTotalPrice}', with confidence {itemTotalPriceField.Confidence}");
                                }
                            }
                        }
                    }
                }
            }
            FormField totalField;
            if (receipt.Fields.TryGetValue("Total", out totalField))
            {
                if (totalField.Value.ValueType == FieldValueType.Float)
                {
                    float total = totalField.Value.AsFloat();

                    Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'");
                }
            }
        }
    }
Пример #22
0
        public async Task <RecognizedFormCollection> AnalyzeReceiptFromStream(Stream receipt)
        {
            RecognizedFormCollection collection = await _frClient.StartRecognizeReceiptsAsync(receipt).WaitForCompletionAsync();

            return(collection);
        }
        private static async Task AnalyzeCustomFormReturnObj(FormRecognizerClient recognizerClient, string modelID, string formUrl)
        {
            RecognizeCustomFormsOptions options = new RecognizeCustomFormsOptions();

            //recognize form
            RecognizeCustomFormsOperation operation = await recognizerClient.StartRecognizeCustomFormsFromUriAsync(modelID, new Uri(formUrl));

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection forms = operationResponse.Value;

            foreach (RecognizedForm form in forms)
            {
                FormResponseObj rObj = new FormResponseObj();

                foreach (FormField field in form.Fields.Values)
                {
                    switch (field.Name)
                    {
                    case "Birthdate":
                    {
                        rObj.Birthdate = field.ValueData.Text;
                    }
                    break;

                    case "Breed":
                    {
                        rObj.Breed = field.ValueData.Text;
                    }
                    break;

                    case "Color":
                    {
                        rObj.Color = field.ValueData.Text;
                    }
                    break;

                    case "DateOfVaccination":
                    {
                        rObj.DateOfVaccination = field.ValueData.Text;
                    }
                    break;

                    case "DueDate":
                    {
                        rObj.DueDate = field.ValueData.Text;
                    }
                    break;

                    case "Duration":
                    {
                        rObj.Duration = field.ValueData.Text;
                    }
                    break;

                    case "LotExpiration":
                    {
                        rObj.LotExpiration = field.ValueData.Text;
                    }
                    break;

                    case "Manufacturer":
                    {
                        rObj.Manufacturer = field.ValueData.Text;
                    }
                    break;

                    case "MicrochipNumber":
                    {
                        rObj.MicrochipNumber = field.ValueData.Text;
                    }
                    break;

                    case "Owner":
                    {
                        rObj.Owner = field.ValueData.Text;
                    }
                    break;

                    case "OwnerAddress":
                    {
                        rObj.OwnerAddress = field.ValueData.Text;
                    }
                    break;

                    case "OwnerPhone":
                    {
                        rObj.OwnerPhone = field.ValueData.Text;
                    }
                    break;

                    case "PetsName":
                    {
                        rObj.PetsName = field.ValueData.Text;
                    }
                    break;

                    case "SerialNumber":
                    {
                        rObj.SerialNumber = field.ValueData.Text;
                    }
                    break;

                    case "Sex":
                    {
                        rObj.Sex = field.ValueData.Text;
                    }
                    break;

                    case "Species":
                    {
                        rObj.Species = field.ValueData.Text;
                    }
                    break;

                    case "TagNumber":
                    {
                        rObj.TagNumber = field.ValueData.Text;
                    }
                    break;

                    case "VaccinationLocation":
                    {
                        rObj.VaccinationLocation = field.ValueData.Text;
                    }
                    break;

                    case "Weight":
                    {
                        rObj.Weight = field.ValueData.Text;
                    }
                    break;
                    }
                }

                returnObjects.Add(rObj);
            }
        }
Пример #24
0
    // </snippet_receipt_print>

    // <snippet_bc_call>
    private static async Task AnalyzeBusinessCard(
        FormRecognizerClient recognizerClient, string bcUrl)
    {
        RecognizedFormCollection businessCards = await recognizerClient.StartRecognizeBusinessCardsFromUriAsync(bcUrl).WaitForCompletionAsync();

        // </snippet_bc_call>
        // <snippet_bc_print>
        foreach (RecognizedForm businessCard in businessCards)
        {
            FormField ContactNamesField;
            if (businessCard.Fields.TryGetValue("ContactNames", out ContactNamesField))
            {
                if (ContactNamesField.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField contactNameField in ContactNamesField.Value.AsList())
                    {
                        Console.WriteLine($"Contact Name: {contactNameField.ValueData.Text}");

                        if (contactNameField.Value.ValueType == FieldValueType.Dictionary)
                        {
                            IReadOnlyDictionary <string, FormField> contactNameFields = contactNameField.Value.AsDictionary();

                            FormField firstNameField;
                            if (contactNameFields.TryGetValue("FirstName", out firstNameField))
                            {
                                if (firstNameField.Value.ValueType == FieldValueType.String)
                                {
                                    string firstName = firstNameField.Value.AsString();

                                    Console.WriteLine($"    First Name: '{firstName}', with confidence {firstNameField.Confidence}");
                                }
                            }

                            FormField lastNameField;
                            if (contactNameFields.TryGetValue("LastName", out lastNameField))
                            {
                                if (lastNameField.Value.ValueType == FieldValueType.String)
                                {
                                    string lastName = lastNameField.Value.AsString();

                                    Console.WriteLine($"    Last Name: '{lastName}', with confidence {lastNameField.Confidence}");
                                }
                            }
                        }
                    }
                }
            }

            FormField jobTitlesFields;
            if (businessCard.Fields.TryGetValue("JobTitles", out jobTitlesFields))
            {
                if (jobTitlesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField jobTitleField in jobTitlesFields.Value.AsList())
                    {
                        if (jobTitleField.Value.ValueType == FieldValueType.String)
                        {
                            string jobTitle = jobTitleField.Value.AsString();

                            Console.WriteLine($"  Job Title: '{jobTitle}', with confidence {jobTitleField.Confidence}");
                        }
                    }
                }
            }

            FormField departmentFields;
            if (businessCard.Fields.TryGetValue("Departments", out departmentFields))
            {
                if (departmentFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField departmentField in departmentFields.Value.AsList())
                    {
                        if (departmentField.Value.ValueType == FieldValueType.String)
                        {
                            string department = departmentField.Value.AsString();

                            Console.WriteLine($"  Department: '{department}', with confidence {departmentField.Confidence}");
                        }
                    }
                }
            }

            FormField emailFields;
            if (businessCard.Fields.TryGetValue("Emails", out emailFields))
            {
                if (emailFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField emailField in emailFields.Value.AsList())
                    {
                        if (emailField.Value.ValueType == FieldValueType.String)
                        {
                            string email = emailField.Value.AsString();

                            Console.WriteLine($"  Email: '{email}', with confidence {emailField.Confidence}");
                        }
                    }
                }
            }

            FormField websiteFields;
            if (businessCard.Fields.TryGetValue("Websites", out websiteFields))
            {
                if (websiteFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField websiteField in websiteFields.Value.AsList())
                    {
                        if (websiteField.Value.ValueType == FieldValueType.String)
                        {
                            string website = websiteField.Value.AsString();

                            Console.WriteLine($"  Website: '{website}', with confidence {websiteField.Confidence}");
                        }
                    }
                }
            }

            FormField mobilePhonesFields;
            if (businessCard.Fields.TryGetValue("MobilePhones", out mobilePhonesFields))
            {
                if (mobilePhonesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField mobilePhoneField in mobilePhonesFields.Value.AsList())
                    {
                        if (mobilePhoneField.Value.ValueType == FieldValueType.PhoneNumber)
                        {
                            string mobilePhone = mobilePhoneField.Value.AsPhoneNumber();

                            Console.WriteLine($"  Mobile phone number: '{mobilePhone}', with confidence {mobilePhoneField.Confidence}");
                        }
                    }
                }
            }

            FormField otherPhonesFields;
            if (businessCard.Fields.TryGetValue("OtherPhones", out otherPhonesFields))
            {
                if (otherPhonesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField otherPhoneField in otherPhonesFields.Value.AsList())
                    {
                        if (otherPhoneField.Value.ValueType == FieldValueType.PhoneNumber)
                        {
                            string otherPhone = otherPhoneField.Value.AsPhoneNumber();

                            Console.WriteLine($"  Other phone number: '{otherPhone}', with confidence {otherPhoneField.Confidence}");
                        }
                    }
                }
            }

            FormField faxesFields;
            if (businessCard.Fields.TryGetValue("Faxes", out faxesFields))
            {
                if (faxesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField faxField in faxesFields.Value.AsList())
                    {
                        if (faxField.Value.ValueType == FieldValueType.PhoneNumber)
                        {
                            string fax = faxField.Value.AsPhoneNumber();

                            Console.WriteLine($"  Fax phone number: '{fax}', with confidence {faxField.Confidence}");
                        }
                    }
                }
            }

            FormField addressesFields;
            if (businessCard.Fields.TryGetValue("Addresses", out addressesFields))
            {
                if (addressesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField addressField in addressesFields.Value.AsList())
                    {
                        if (addressField.Value.ValueType == FieldValueType.String)
                        {
                            string address = addressField.Value.AsString();

                            Console.WriteLine($"  Address: '{address}', with confidence {addressField.Confidence}");
                        }
                    }
                }
            }

            FormField companyNamesFields;
            if (businessCard.Fields.TryGetValue("CompanyNames", out companyNamesFields))
            {
                if (companyNamesFields.Value.ValueType == FieldValueType.List)
                {
                    foreach (FormField companyNameField in companyNamesFields.Value.AsList())
                    {
                        if (companyNameField.Value.ValueType == FieldValueType.String)
                        {
                            string companyName = companyNameField.Value.AsString();

                            Console.WriteLine($"  Company name: '{companyName}', with confidence {companyNameField.Confidence}");
                        }
                    }
                }
            }
        }
    }
        public async Task OutputModelsTrainedWithoutLabels()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;
            string formFilePath    = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");

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

            // Model trained without labels
            CustomFormModel modelTrainedWithoutLabels = await trainingClient.StartTrainingAsync(new Uri(trainingFileUrl), useTrainingLabels : false, "My Model").WaitForCompletionAsync();

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                RecognizedFormCollection forms = await client.StartRecognizeCustomFormsAsync(modelTrainedWithoutLabels.ModelId, stream).WaitForCompletionAsync();

                // With a form recognized by a model trained without labels, the 'field.Name' property will be denoted
                // by a numeric index. To look for the labels identified during the training step,
                // use the `field.LabelText` property.
                Console.WriteLine("---------Recognizing forms using models trained without labels---------");
                foreach (RecognizedForm form in forms)
                {
                    Console.WriteLine($"Form of type: {form.FormType}");
                    Console.WriteLine($"Form was analyzed with model with ID: {form.ModelId}");
                    foreach (FormField field in form.Fields.Values)
                    {
                        Console.WriteLine($"Field {field.Name}: ");

                        if (field.LabelData != null)
                        {
                            Console.WriteLine($"    Label: '{field.LabelData.Text}");
                        }

                        Console.WriteLine($"    Value: '{field.ValueData.Text}");
                        Console.WriteLine($"    Confidence: '{field.Confidence}");
                    }
                }

                // Find the value of unlabeled fields.
                foreach (RecognizedForm form in forms)
                {
                    // Find the value of a specific unlabeled field.
                    Console.WriteLine("Find the value for a specific unlabeled field:");
                    foreach (FormField field in form.Fields.Values)
                    {
                        if (field.LabelData != null && field.LabelData.Text == "Vendor Name:")
                        {
                            Console.WriteLine($"The Vendor Name is {field.ValueData.Text}");
                        }
                    }

                    // Find the value of unlabeled fields with specific words
                    Console.WriteLine("Find the value for labeled field with specific words:");
                    form.Fields.Values.Where(field => field.LabelData.Text.StartsWith("Ven"))
                    .ToList().ForEach(v => Console.WriteLine($"{v.LabelData.Text} is {v.ValueData.Text}"));
                    form.Fields.Values.Where(field => field.LabelData.Text.Contains("Name"))
                    .ToList().ForEach(v => Console.WriteLine($"{v.LabelData.Text} is {v.ValueData.Text}"));
                }
            }
        }
        public async Task RecognizeInvoicesFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:FormRecognizerSampleRecognizeInvoicesFileStream
#if SNIPPET
            string invoicePath = "<invoicePath>";
#else
            string invoicePath = FormRecognizerTestEnvironment.CreatePath("recommended_invoice.jpg");
#endif

            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 all 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("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("Description", out FormField itemDescriptionField))
                            {
                                if (itemDescriptionField.Value.ValueType == FieldValueType.String)
                                {
                                    string itemDescription = itemDescriptionField.Value.AsString();

                                    Console.WriteLine($"  Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}");
                                }
                            }

                            if (itemFields.TryGetValue("Quantity", out FormField itemQuantityField))
                            {
                                if (itemQuantityField.Value.ValueType == FieldValueType.Float)
                                {
                                    float quantityAmount = itemQuantityField.Value.AsFloat();

                                    Console.WriteLine($"  Quantity: '{quantityAmount}', with confidence {itemQuantityField.Confidence}");
                                }
                            }

                            if (itemFields.TryGetValue("UnitPrice", out FormField itemUnitPriceField))
                            {
                                if (itemUnitPriceField.Value.ValueType == FieldValueType.Float)
                                {
                                    float itemUnitPrice = itemUnitPriceField.Value.AsFloat();

                                    Console.WriteLine($"  UnitPrice: '{itemUnitPrice}', with confidence {itemUnitPriceField.Confidence}");
                                }
                            }

                            if (itemFields.TryGetValue("Tax", out FormField itemTaxPriceField))
                            {
                                if (itemTaxPriceField.Value.ValueType == FieldValueType.Float)
                                {
                                    try
                                    {
                                        float itemTax = itemTaxPriceField.Value.AsFloat();
                                        Console.WriteLine($"  Tax: '{itemTax}', with confidence {itemTaxPriceField.Confidence}");
                                    }
                                    catch
                                    {
                                        string itemTaxText = itemTaxPriceField.ValueData.Text;
                                        Console.WriteLine($"  Tax: '{itemTaxText}', with confidence {itemTaxPriceField.Confidence}");
                                    }
                                }
                            }

                            if (itemFields.TryGetValue("Amount", out FormField itemAmountField))
                            {
                                if (itemAmountField.Value.ValueType == FieldValueType.Float)
                                {
                                    float itemAmount = itemAmountField.Value.AsFloat();

                                    Console.WriteLine($"  Amount: '{itemAmount}', with confidence {itemAmountField.Confidence}");
                                }
                            }
                        }
                    }
                }
            }

            if (invoice.Fields.TryGetValue("SubTotal", out FormField subTotalField))
            {
                if (subTotalField.Value.ValueType == FieldValueType.Float)
                {
                    float subTotal = subTotalField.Value.AsFloat();
                    Console.WriteLine($"Sub Total: '{subTotal}', with confidence {subTotalField.Confidence}");
                }
            }

            if (invoice.Fields.TryGetValue("TotalTax", out FormField totalTaxField))
            {
                if (totalTaxField.Value.ValueType == FieldValueType.Float)
                {
                    float totalTax = totalTaxField.Value.AsFloat();
                    Console.WriteLine($"Total Tax: '{totalTax}', with confidence {totalTaxField.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
        }
Пример #27
0
        public async Task RecognizeReceiptsFromUri()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileFromUri
#if SNIPPET
            Uri receiptUri = new Uri("<receiptUri>");
#else
            Uri receiptUri = FormRecognizerTestEnvironment.CreateUri("contoso-receipt.jpg");
#endif

            RecognizeReceiptsOperation operation = await client.StartRecognizeReceiptsFromUriAsync(receiptUri);

            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
        }
        public async Task RecognizeBusinessCardsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

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

            #region Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream
            using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open))
            {
                RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream).WaitForCompletionAsync();

                // To see the list of the supported fields returned by service and its corresponding types, consult:
                // https://aka.ms/formrecognizer/businesscardfields

                foreach (RecognizedForm businessCard in businessCards)
                {
                    FormField ContactNamesField;
                    if (businessCard.Fields.TryGetValue("ContactNames", out ContactNamesField))
                    {
                        if (ContactNamesField.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField contactNameField in ContactNamesField.Value.AsList())
                            {
                                Console.WriteLine($"Contact Name: {contactNameField.ValueData.Text}");

                                if (contactNameField.Value.ValueType == FieldValueType.Dictionary)
                                {
                                    IReadOnlyDictionary <string, FormField> contactNameFields = contactNameField.Value.AsDictionary();

                                    FormField firstNameField;
                                    if (contactNameFields.TryGetValue("FirstName", out firstNameField))
                                    {
                                        if (firstNameField.Value.ValueType == FieldValueType.String)
                                        {
                                            string firstName = firstNameField.Value.AsString();

                                            Console.WriteLine($"    First Name: '{firstName}', with confidence {firstNameField.Confidence}");
                                        }
                                    }

                                    FormField lastNameField;
                                    if (contactNameFields.TryGetValue("LastName", out lastNameField))
                                    {
                                        if (lastNameField.Value.ValueType == FieldValueType.String)
                                        {
                                            string lastName = lastNameField.Value.AsString();

                                            Console.WriteLine($"    Last Name: '{lastName}', with confidence {lastNameField.Confidence}");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    FormField jobTitlesFields;
                    if (businessCard.Fields.TryGetValue("JobTitles", out jobTitlesFields))
                    {
                        if (jobTitlesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField jobTitleField in jobTitlesFields.Value.AsList())
                            {
                                if (jobTitleField.Value.ValueType == FieldValueType.String)
                                {
                                    string jobTitle = jobTitleField.Value.AsString();

                                    Console.WriteLine($"  Job Title: '{jobTitle}', with confidence {jobTitleField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField departmentFields;
                    if (businessCard.Fields.TryGetValue("Departments", out departmentFields))
                    {
                        if (departmentFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField departmentField in departmentFields.Value.AsList())
                            {
                                if (departmentField.Value.ValueType == FieldValueType.String)
                                {
                                    string department = departmentField.Value.AsString();

                                    Console.WriteLine($"  Department: '{department}', with confidence {departmentField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField emailFields;
                    if (businessCard.Fields.TryGetValue("Emails", out emailFields))
                    {
                        if (emailFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField emailField in emailFields.Value.AsList())
                            {
                                if (emailField.Value.ValueType == FieldValueType.String)
                                {
                                    string email = emailField.Value.AsString();

                                    Console.WriteLine($"  Email: '{email}', with confidence {emailField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField websiteFields;
                    if (businessCard.Fields.TryGetValue("Websites", out websiteFields))
                    {
                        if (websiteFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField websiteField in websiteFields.Value.AsList())
                            {
                                if (websiteField.Value.ValueType == FieldValueType.String)
                                {
                                    string website = websiteField.Value.AsString();

                                    Console.WriteLine($"  Website: '{website}', with confidence {websiteField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField mobilePhonesFields;
                    if (businessCard.Fields.TryGetValue("MobilePhones", out mobilePhonesFields))
                    {
                        if (mobilePhonesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField mobilePhoneField in mobilePhonesFields.Value.AsList())
                            {
                                if (mobilePhoneField.Value.ValueType == FieldValueType.PhoneNumber)
                                {
                                    string mobilePhone = mobilePhoneField.Value.AsPhoneNumber();

                                    Console.WriteLine($"  Mobile phone number: '{mobilePhone}', with confidence {mobilePhoneField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField otherPhonesFields;
                    if (businessCard.Fields.TryGetValue("OtherPhones", out otherPhonesFields))
                    {
                        if (otherPhonesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField otherPhoneField in otherPhonesFields.Value.AsList())
                            {
                                if (otherPhoneField.Value.ValueType == FieldValueType.PhoneNumber)
                                {
                                    string otherPhone = otherPhoneField.Value.AsPhoneNumber();

                                    Console.WriteLine($"  Other phone number: '{otherPhone}', with confidence {otherPhoneField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField faxesFields;
                    if (businessCard.Fields.TryGetValue("Faxes", out faxesFields))
                    {
                        if (faxesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField faxField in faxesFields.Value.AsList())
                            {
                                if (faxField.Value.ValueType == FieldValueType.PhoneNumber)
                                {
                                    string fax = faxField.Value.AsPhoneNumber();

                                    Console.WriteLine($"  Fax phone number: '{fax}', with confidence {faxField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField addressesFields;
                    if (businessCard.Fields.TryGetValue("Addresses", out addressesFields))
                    {
                        if (addressesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField addressField in addressesFields.Value.AsList())
                            {
                                if (addressField.Value.ValueType == FieldValueType.String)
                                {
                                    string address = addressField.Value.AsString();

                                    Console.WriteLine($"  Address: '{address}', with confidence {addressField.Confidence}");
                                }
                            }
                        }
                    }

                    FormField companyNamesFields;
                    if (businessCard.Fields.TryGetValue("CompanyNames", out companyNamesFields))
                    {
                        if (companyNamesFields.Value.ValueType == FieldValueType.List)
                        {
                            foreach (FormField companyNameField in companyNamesFields.Value.AsList())
                            {
                                if (companyNameField.Value.ValueType == FieldValueType.String)
                                {
                                    string companyName = companyNameField.Value.AsString();

                                    Console.WriteLine($"  Company name: '{companyName}', with confidence {companyNameField.Confidence}");
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }
Пример #29
0
        public async Task RecognizeCustomFormsFromFile()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;

            // Firstly, create a trained model we can use to recognize the custom form. Please note that
            // models can also be trained using a graphical user interface such as the Form Recognizer
            // Labeling Tool found here:
            // https://docs.microsoft.com/azure/cognitive-services/form-recognizer/label-tool?tabs=v2-1

            FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            CustomFormModel    model          = await trainingClient.StartTrainingAsync(new Uri(trainingFileUrl), useTrainingLabels : false, "My Model").WaitForCompletionAsync();

            // Proceed with the custom form recognition.

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

            #region Snippet:FormRecognizerRecognizeCustomFormsFromFile
#if SNIPPET
            string modelId  = "<modelId>";
            string filePath = "<filePath>";
#else
            string filePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
            string modelId  = model.ModelId;
#endif

            using var stream = new FileStream(filePath, FileMode.Open);
            var options = new RecognizeCustomFormsOptions()
            {
                IncludeFieldElements = true
            };

            RecognizeCustomFormsOperation operation = await client.StartRecognizeCustomFormsAsync(modelId, stream, options);

            Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync();

            RecognizedFormCollection forms = operationResponse.Value;

            foreach (RecognizedForm form in forms)
            {
                Console.WriteLine($"Form of type: {form.FormType}");
                Console.WriteLine($"Form was analyzed with model with ID: {form.ModelId}");
                foreach (FormField field in form.Fields.Values)
                {
                    Console.WriteLine($"Field '{field.Name}': ");

                    if (field.LabelData != null)
                    {
                        Console.WriteLine($"  Label: '{field.LabelData.Text}'");
                    }

                    Console.WriteLine($"  Value: '{field.ValueData.Text}'");
                    Console.WriteLine($"  Confidence: '{field.Confidence}'");
                }

                // Iterate over tables, lines, and selection marks on each page
                foreach (var page in form.Pages)
                {
                    for (int i = 0; i < page.Tables.Count; i++)
                    {
                        Console.WriteLine($"Table {i+1} on page {page.Tables[i].PageNumber}");
                        foreach (var cell in page.Tables[i].Cells)
                        {
                            Console.WriteLine($"  Cell[{cell.RowIndex}][{cell.ColumnIndex}] has text '{cell.Text}' with confidence {cell.Confidence}");
                        }
                    }
                    Console.WriteLine($"Lines found on page {page.PageNumber}");
                    foreach (var line in page.Lines)
                    {
                        Console.WriteLine($"  Line {line.Text}");
                    }

                    if (page.SelectionMarks.Count != 0)
                    {
                        Console.WriteLine($"Selection marks found on page {page.PageNumber}");
                        foreach (var selectionMark in page.SelectionMarks)
                        {
                            Console.WriteLine($"  Selection mark is '{selectionMark.State}' with confidence {selectionMark.Confidence}");
                        }
                    }
                }
            }
            #endregion

            // Delete the model on completion to clean environment.
            await trainingClient.DeleteModelAsync(model.ModelId);
        }
Пример #30
0
        public async Task RecognizeInvoicesFromUri()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            Uri invoiceUri = FormRecognizerTestEnvironment.CreateUri("Invoice_1.pdf");

            #region 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}");
                }
            }
        }