Exemple #1
0
        public async Task RecognizeCustomFormsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
            string modelId      = "<your model id>";

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                Response <IReadOnlyList <RecognizedForm> > forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

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

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

                        Console.WriteLine($"    Value: '{field.ValueText.Text}");
                        Console.WriteLine($"    Confidence: '{field.Confidence}");
                    }
                }
            }
        }
Exemple #2
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.

            FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            CustomFormModel    model          = await trainingClient.StartTraining(new Uri(trainingFileUrl)).WaitForCompletionAsync();

            // Proceed with the custom form recognition.

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

            string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
            string modelId      = model.ModelId;

            #region Snippet:FormRecognizerRecognizeCustomFormsFromFile
            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                //@@ string modelId = "<modelId>";

                RecognizedFormCollection forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

                /*
                 *
                 */
            }
            #endregion

            // Delete the model on completion to clean environment.
            trainingClient.DeleteModel(model.ModelId);
        }
        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.StartTraining(new Uri(trainingFileUrl), useTrainingLabels : false).WaitForCompletionAsync();

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                RecognizedFormCollection forms = await client.StartRecognizeCustomForms(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 analyzePdfForm(
            FormRecognizerClient recognizerClient,
            string modelId,
            string pdfToAnalyzeLocalPath,
            ModelType processingModelType)
        {
            using (FileStream pdfInputstream = new FileStream(pdfToAnalyzeLocalPath, FileMode.Open))
            {
                RecognizedFormCollection recognizedForms = await recognizerClient
                                                           .StartRecognizeCustomForms(modelId, pdfInputstream)
                                                           .WaitForCompletionAsync();

                switch (processingModelType)
                {
                case ModelType.NAVIGATION:
                    pdfToAnalyzeLocalPath = pdfToAnalyzeLocalPath + "_Navig";
                    break;

                case ModelType.DEFINITION:
                    pdfToAnalyzeLocalPath = pdfToAnalyzeLocalPath + "_Defin";
                    break;
                }
                writeRecognizedFormCollectionToDB(pdfToAnalyzeLocalPath, recognizedForms);
            }
        }
        public async Task <docu3clist> ClassifyDocument(string doc_type, string formUri)
        {
            docu3clist docs    = new docu3clist();
            string     modelId = GetModelID(doc_type);

            try
            {
                WebClient wc         = new WebClient();
                byte[]    imageBytes = wc.DownloadData(formUri);
                var       stream     = new MemoryStream(imageBytes);

                FormRecognizerClient recognizerClient = new FormRecognizerClient(new Uri(endpoint), credential);
                //var forms = await recognizerClient.StartRecognizeCustomFormsFromUri(modelId,new Uri(formUri)).WaitForCompletionAsync();
                //Response<IReadOnlyList<RecognizedForm>>
                var forms = await recognizerClient.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

                foreach (RecognizedForm form in forms.Value)
                {
                    docu3c doc = new docu3c();
                    doc.docID    = formUri;
                    doc.docURL   = formUri;
                    doc.docType  = form.FormType;
                    doc.docProps = new Dictionary <string, docu3cProp>();
                    foreach (FormField field in form.Fields.Values)
                    {
                        if (field != null)
                        {
                            docu3cProp prop = new docu3cProp();
                            prop.Name = field.Name;
                            if (field.LabelText != null)
                            {
                                prop.Label = field.LabelText;
                            }
                            if (field.Name == "doc.type")
                            {
                                prop.Value = field.ValueText.Text.Replace(" ", "_");
                            }
                            else
                            {
                                prop.Value = field.ValueText.Text;
                            }
                            prop.Confidence = field.Confidence;
                            doc.docProps.Add(field.Name, prop);
                        }
                    }
                    docs.Add(doc);
                }

                //docs.html = docu3cAPI.SetDocHTML(docs);
                return(docs);
            }
            catch (Exception ex)
            {
                docu3c doc = new docu3c();
                doc.docParseErrorMsg = ex.Message;
                docs.Add(doc);
                return(docs);
            }
        }
        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.StartTraining(new Uri(trainingFileUrl), useTrainingLabels : true).WaitForCompletionAsync();

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                RecognizedFormCollection forms = await client.StartRecognizeCustomForms(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}");
                    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}"));
                }
            }
        }
Exemple #7
0
        static async Task Main(string[] args)
        {
            try
            {
                // Get configuration settings from AppSettings
                IConfigurationBuilder builder       = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                IConfigurationRoot    configuration = builder.Build();
                string formEndpoint = configuration["FormEndpoint"];
                string formKey      = configuration["FormKey"];
                string modelId      = configuration["ModelId"];

                // Authenticate Form Recognizer Client
                var credential       = new AzureKeyCredential(formKey);
                var recognizerClient = new FormRecognizerClient(new Uri(formEndpoint), credential);

                // Get form url for testing
                string image_file = "test1.jpg";
                using (var image_data = File.OpenRead(image_file))
                {
                    // Use trained model with new form
                    RecognizedFormCollection forms = await recognizerClient
                                                     .StartRecognizeCustomForms(modelId, image_data)
                                                     .WaitForCompletionAsync();

                    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}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        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.StartTraining(new Uri(trainingFileUrl), useTrainingLabels : false).WaitForCompletionAsync();

            // Proceed with the custom form recognition.

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

            string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
            string modelId      = model.ModelId;

            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                RecognizedFormCollection forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

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

            // Delete the model on completion to clean environment.
            trainingClient.DeleteModel(model.ModelId);
        }
        public async Task RecognizeCustomFormsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string formFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg");
            string modelId      = "<your model id>";

            #region Snippet:FormRecognizerRecognizeCustomFormsFromFile
            using (FileStream stream = new FileStream(formFilePath, FileMode.Open))
            {
                Response <IReadOnlyList <RecognizedForm> > forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync();

                /*
                 *
                 */
            }
            #endregion
        }