public async Task StartRecognizeBusinessCardsSendsUserSpecifiedLocale(string locale)
        {
            var mockResponse = new MockResponse(202);

            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, "host/prebuilt/businesscards/analyzeResults/00000000000000000000000000000000"));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options       = new FormRecognizerClientOptions()
            {
                Transport = mockTransport
            };
            var client = CreateInstrumentedClient(options);

            using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.BusinessCardJpg);
            var recognizeOptions = new RecognizeBusinessCardsOptions {
                Locale = locale
            };
            await client.StartRecognizeBusinessCardsAsync(stream, recognizeOptions);

            var requestUriQuery = mockTransport.Requests.Single().Uri.Query;

            var localeQuery = "locale=";
            var index       = requestUriQuery.IndexOf(localeQuery);
            var length      = requestUriQuery.Length - (index + localeQuery.Length);

            Assert.AreEqual(locale, requestUriQuery.Substring(index + localeQuery.Length, length));
        }
Пример #2
0
        //analyze the business card (sends filtered data) value + tag, example: "Emails" "*****@*****.**"
        private async Task AnalyzeBusinessCardPath()
        {
            using (var stream = new FileStream(imagePath, FileMode.Open))
            {
                try
                {
                    var options = new RecognizeBusinessCardsOptions()
                    {
                        Locale = "en-US"
                    };

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

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

                    RecognizedFormCollection businessCards = operationResponse.Value;

                    //only 1 business Card so we take the first page
                    addValues(businessCards[0]);
                }
                catch (Exception ex)
                {
                    throw new FormRecognizerException(ex.ToString());
                }
            }
        }
Пример #3
0
        public async Task StartRecognizeBusinessCardsWithSupportedLocale()
        {
            var client  = CreateFormRecognizerClient();
            var options = new RecognizeBusinessCardsOptions()
            {
                IncludeFieldElements = true,
                Locale = FormRecognizerLocale.EnUS
            };
            RecognizeBusinessCardsOperation operation;

            using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.BusinessCardJpg);
            using (Recording.DisableRequestBodyRecording())
            {
                operation = await client.StartRecognizeBusinessCardsAsync(stream, options);
            }

            RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync();

            var businessCard = recognizedForms.Single();

            ValidatePrebuiltForm(
                businessCard,
                includeFieldElements: true,
                expectedFirstPageNumber: 1,
                expectedLastPageNumber: 1);

            Assert.Greater(businessCard.Fields.Count, 0);

            var businessCardPage = businessCard.Pages.Single();

            Assert.Greater(businessCardPage.Lines.Count, 0);
            Assert.AreEqual(0, businessCardPage.SelectionMarks.Count);
            Assert.AreEqual(0, businessCardPage.Tables.Count);
        }
Пример #4
0
        public async Task StartRecognizeBusinessCardsIncludeFieldElements()
        {
            var client  = CreateFormRecognizerClient();
            var options = new RecognizeBusinessCardsOptions()
            {
                IncludeFieldElements = true
            };
            RecognizeBusinessCardsOperation operation;

            using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.BusinessCardJpg);
            using (Recording.DisableRequestBodyRecording())
            {
                operation = await client.StartRecognizeBusinessCardsAsync(stream, options);
            }

            RecognizedFormCollection recognizedForms = await operation.WaitForCompletionAsync();

            var businessCardsForm = recognizedForms.Single();

            ValidatePrebuiltForm(
                businessCardsForm,
                includeFieldElements: true,
                expectedFirstPageNumber: 1,
                expectedLastPageNumber: 1);
        }
        public void StartRecognizeBusinessCardsRespectsTheCancellationToken()
        {
            var client  = CreateInstrumentedClient();
            var options = new RecognizeBusinessCardsOptions {
                ContentType = FormContentType.Pdf
            };

            using var stream             = new MemoryStream(Array.Empty <byte>());
            using var cancellationSource = new CancellationTokenSource();
            cancellationSource.Cancel();

            Assert.ThrowsAsync(Is.InstanceOf <OperationCanceledException>(), async() => await client.StartRecognizeBusinessCardsAsync(stream, recognizeBusinessCardsOptions: options, cancellationToken: cancellationSource.Token));
        }
        public async Task StartRecognizeBusinessCardsSendsUserSpecifiedContentType()
        {
            var mockResponse = new MockResponse(202);
            mockResponse.AddHeader(new HttpHeader(Constants.OperationLocationHeader, "host/prebuilt/businessCard/analyzeResults/00000000000000000000000000000000"));

            var mockTransport = new MockTransport(new[] { mockResponse, mockResponse });
            var options = new FormRecognizerClientOptions() { Transport = mockTransport };
            var client = CreateInstrumentedClient(options);

            using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.InvoiceLeTiff);
            var recognizeOptions = new RecognizeBusinessCardsOptions { ContentType = FormContentType.Jpeg };
            await client.StartRecognizeBusinessCardsAsync(stream, recognizeOptions);

            var request = mockTransport.Requests.Single();

            Assert.True(request.Headers.TryGetValue("Content-Type", out var contentType));
            Assert.AreEqual("image/jpeg", contentType);
        }
Пример #7
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
        }
Пример #8
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);
            }
        }