Ejemplo n.º 1
0
        private static async Task TestLocal(string endpoint, string apiKey, string modelId, string filePath)
        {
            FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

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

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

            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}'");
                }
            }
        }
        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).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}");
                    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 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/quickstarts/label-tool

            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);

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

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

            #endregion

            // Delete the model on completion to clean environment.
            await trainingClient.DeleteModelAsync(model.ModelId);
        }
Ejemplo n.º 4
0
        public async Task <List <dynamic> > ScanLocal(string classificationKey, string filePath)
        {
            string classificationModelId = await _distributedCache.GetStringAsync(classificationKey);

            using var stream = new FileStream(filePath, FileMode.Open);
            RecognizeCustomFormsOperation operation = await _client.StartRecognizeCustomFormsAsync(classificationModelId, stream);

            return(await Evaluate(operation));
        }
Ejemplo n.º 5
0
        public async Task OutputModelsTrainedWithLabels()
        {
            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 with labels
            CustomFormModel modelTrainedWithLabels = await trainingClient.StartTrainingAsync(new Uri(trainingFileUrl), useTrainingLabels : true, new TrainingOptions()
            {
                ModelDisplayName = "My Model with labels"
            }).WaitForCompletionAsync();

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

                // With a form recognized by a model trained with labels, the 'field.Name' key will be the label
                // that you gave it at training time.
                // Note that Label data is not returned for model trained with labels, as the trained model
                // contains this information and therefore the service returns the value of the recognized label.
                Console.WriteLine("---------Recognizing forms using models trained with labels---------");
                foreach (RecognizedForm form in forms)
                {
                    Console.WriteLine($"Form of type: {form.FormType}");
                    Console.WriteLine($"Form has form type confidence: {form.FormTypeConfidence.Value}");
                    Console.WriteLine($"Form was analyzed with model with ID: {form.ModelId}");
                    foreach (FormField field in form.Fields.Values)
                    {
                        Console.WriteLine($"Field {field.Name}: ");
                        Console.WriteLine($"    Value: '{field.ValueData.Text}");
                        Console.WriteLine($"    Confidence: '{field.Confidence}");
                    }
                }

                // Find labeled field.
                foreach (RecognizedForm form in forms)
                {
                    // Find the specific labeled field.
                    Console.WriteLine("Find the value for a specific labeled field:");
                    if (form.Fields.TryGetValue("VendorName", out FormField field))
                    {
                        Console.WriteLine($"VendorName is {field.ValueData.Text}");
                    }

                    // Find labeled fields with specific words
                    Console.WriteLine("Find the value for labeled field with specific words:");
                    form.Fields.Where(kv => kv.Key.StartsWith("Ven"))
                    .ToList().ForEach(v => Console.WriteLine($"{v.Key} is {v.Value.ValueData.Text}"));
                    form.Fields.Where(kv => kv.Key.Contains("Name"))
                    .ToList().ForEach(v => Console.WriteLine($"{v.Key} is {v.Value.ValueData.Text}"));
                }
            }
        }
Ejemplo n.º 6
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);
        }
        public async Task CreateComposedModel()
        {
            string endpoint        = TestEnvironment.Endpoint;
            string apiKey          = TestEnvironment.ApiKey;
            string trainingFileUrl = TestEnvironment.BlobContainerSasUrl;

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

            string purchaseOrderOfficeSuppliesUrl   = trainingFileUrl;
            string purchaseOrderOfficeEquipmentUrl  = trainingFileUrl;
            string purchaseOrderFurnitureUrl        = trainingFileUrl;
            string purchaseOrderCleaningSuppliesUrl = trainingFileUrl;

            #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

            //@@ string purchaseOrderOfficeSuppliesUrl = "<purchaseOrderOfficeSupplies>";
            //@@ string purchaseOrderOfficeEquipmentUrl = "<purchaseOrderOfficeEquipment>";
            //@@ string purchaseOrderFurnitureUrl = "<purchaseOrderFurniture>";
            //@@ string purchaseOrderCleaningSuppliesUrl = "<purchaseOrderCleaningSupplies>";

            CustomFormModel purchaseOrderOfficeSuppliesModel   = (await client.StartTrainingAsync(new Uri(purchaseOrderOfficeSuppliesUrl), useTrainingLabels: true, "Purchase order - Office supplies").WaitForCompletionAsync()).Value;
            CustomFormModel purchaseOrderOfficeEquipmentModel  = (await client.StartTrainingAsync(new Uri(purchaseOrderOfficeEquipmentUrl), useTrainingLabels: true, "Purchase order - Office Equipment").WaitForCompletionAsync()).Value;
            CustomFormModel purchaseOrderFurnitureModel        = (await client.StartTrainingAsync(new Uri(purchaseOrderFurnitureUrl), useTrainingLabels: true, "Purchase order - Furniture").WaitForCompletionAsync()).Value;
            CustomFormModel purchaseOrderCleaningSuppliesModel = (await client.StartTrainingAsync(new Uri(purchaseOrderCleaningSuppliesUrl), useTrainingLabels: true, "Purchase order - Cleaning Supplies").WaitForCompletionAsync()).Value;

            #endregion

            #region Snippet:FormRecognizerSampleCreateComposedModel

            List <string> modelIds = new List <string>()
            {
                purchaseOrderOfficeSuppliesModel.ModelId,
                purchaseOrderOfficeEquipmentModel.ModelId,
                purchaseOrderFurnitureModel.ModelId,
                purchaseOrderCleaningSuppliesModel.ModelId
            };

            CustomFormModel purchaseOrderModel = (await client.StartCreateComposedModelAsync(modelIds, "Composed Purchase order").WaitForCompletionAsync()).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 = 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} model{(purchaseOrderModel.Submodels.Count > 1 ? "s" : "")}.");
            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

            string purchaseOrderFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");

            #region Snippet:FormRecognizerSampleRecognizeCustomFormWithComposedModel

            //@@ string purchaseOrderFilePath = "<purchaseOrderFilePath>";
            FormRecognizerClient formRecognizerClient = client.GetFormRecognizerClient();

            RecognizedFormCollection forms;
            using (FileStream stream = new FileStream(purchaseOrderFilePath, FileMode.Open))
            {
                forms = await formRecognizerClient.StartRecognizeCustomFormsAsync(purchaseOrderModel.ModelId, stream).WaitForCompletionAsync();

                // 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(purchaseOrderOfficeSuppliesModel.ModelId).ConfigureAwait(false);

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

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

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

            await client.DeleteModelAsync(purchaseOrderModel.ModelId).ConfigureAwait(false);
        }
Ejemplo n.º 8
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}");
                        }
                    }
                }
            }
        }
 public async Task <RecognizedFormCollection> AnalyzeCustomFormFromStream(Stream form, string modelId)
 => await client.StartRecognizeCustomFormsAsync(modelId, form).WaitForCompletionAsync();
Ejemplo n.º 10
0
        public async Task <RecognizedFormCollection> AnalyzeCustomFormFromStream(Stream form, string modelId)
        {
            RecognizedFormCollection collection = await _frClient.StartRecognizeCustomFormsAsync(modelId, form).WaitForCompletionAsync();

            return(collection);
        }