diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md b/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md index 62a916816570..bb3d739afbd5 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md @@ -179,6 +179,8 @@ foreach (FormPage page in formPages.Value) Recognize and extract form fields and other content from your custom forms, using models you train with your own form types. ```C# Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri +string modelId = ""; + Response> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync(); foreach (RecognizedForm form in forms.Value) { diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeCustomForms.md b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeCustomForms.md index 90bdc5df364b..03ceed325eb7 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeCustomForms.md +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeCustomForms.md @@ -22,6 +22,8 @@ var client = new FormRecognizerClient(new Uri(endpoint), credential); To recognize form fields and other content from your custom forms from a given file at a URI, use the `StartRecognizeCustomFormsFromUri` method. The returned value is a collection of `RecognizedForm` objects -- one for each page in the submitted document. ```C# Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri +string modelId = ""; + Response> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync(); foreach (RecognizedForm form in forms.Value) { @@ -48,6 +50,8 @@ To recognize form fields and other content from your custom forms from a file st ```C# Snippet:FormRecognizerRecognizeCustomFormsFromFile using (FileStream stream = new FileStream(formFilePath, FileMode.Open)) { + string modelId = ""; + Response> forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync(); /* * diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs index 868ad0f4f5ba..7aa48fc4c9e5 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; using Azure.AI.FormRecognizer.Models; using Azure.AI.FormRecognizer.Training; -using Azure.Core.TestFramework; using NUnit.Framework; using NUnit.Framework.Internal; @@ -21,7 +20,7 @@ namespace Azure.AI.FormRecognizer.Tests /// These tests have a dependency on live Azure services and may incur costs for the associated /// Azure subscription. /// - public class FormRecognizerClientLiveTests : RecordedTestBase + public class FormRecognizerClientLiveTests : FormRecognizerLiveTestBase { /// /// Initializes a new instance of the class. @@ -29,8 +28,6 @@ public class FormRecognizerClientLiveTests : RecordedTestBaseA flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods. public FormRecognizerClientLiveTests(bool isAsync) : base(isAsync) { - Sanitizer = new FormRecognizerRecordedTestSanitizer(); - Matcher = new FormRecognizerRecordMatcher(); } /// @@ -49,22 +46,6 @@ private FormRecognizerClient CreateInstrumentedFormRecognizerClient() return InstrumentClient(client); } - /// - /// Creates a with the endpoint and API key provided via environment - /// variables and instruments it to make use of the Azure Core Test Framework functionalities. - /// - /// The instrumented . - private FormTrainingClient CreateInstrumentedFormTrainingClient() - { - var endpoint = new Uri(TestEnvironment.Endpoint); - var credential = new AzureKeyCredential(TestEnvironment.ApiKey); - - var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions()); - var client = new FormTrainingClient(endpoint, credential, options); - - return InstrumentClient(client); - } - /// /// Verifies that the is able to connect to the Form /// Recognizer cognitive service and perform operations. diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientLiveTests.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientLiveTests.cs index b5483b114314..6d409255f903 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientLiveTests.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormTrainingClient/FormTrainingClientLiveTests.cs @@ -17,7 +17,7 @@ namespace Azure.AI.FormRecognizer.Tests /// These tests have a dependency on live Azure services and may incur costs for the associated /// Azure subscription. /// - public class FormTrainingClientLiveTests : RecordedTestBase + public class FormTrainingClientLiveTests : FormRecognizerLiveTestBase { /// /// Initializes a new instance of the class. @@ -25,24 +25,6 @@ public class FormTrainingClientLiveTests : RecordedTestBaseA flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods. public FormTrainingClientLiveTests(bool isAsync) : base(isAsync) { - Sanitizer = new FormRecognizerRecordedTestSanitizer(); - Matcher = new FormRecognizerRecordMatcher(); - } - - /// - /// Creates a with the endpoint and API key provided via environment - /// variables and instruments it to make use of the Azure Core Test Framework functionalities. - /// - /// The instrumented . - private FormTrainingClient CreateInstrumentedClient() - { - var endpoint = new Uri(TestEnvironment.Endpoint); - var credential = new AzureKeyCredential(TestEnvironment.ApiKey); - - var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions()); - var client = new FormTrainingClient(endpoint, credential, options); - - return InstrumentClient(client); } [Test] @@ -50,7 +32,7 @@ private FormTrainingClient CreateInstrumentedClient() [TestCase(false)] public async Task StartTraining(bool labeled) { - var client = CreateInstrumentedClient(); + var client = CreateInstrumentedFormTrainingClient(); var trainingFiles = new Uri(TestEnvironment.BlobContainerSasUrl); TrainingOperation operation; @@ -100,7 +82,7 @@ public async Task StartTraining(bool labeled) [Test] public async Task StartTrainingError() { - var client = CreateInstrumentedClient(); + var client = CreateInstrumentedFormTrainingClient(); var containerUrl = new Uri("https://someUrl"); @@ -128,7 +110,7 @@ public async Task StartTrainingError() [TestCase(false)] public async Task TrainingOps(bool labeled) { - var client = CreateInstrumentedClient(); + var client = CreateInstrumentedFormTrainingClient(); var trainingFiles = new Uri(TestEnvironment.BlobContainerSasUrl); TrainingOperation operation; diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/Infrastructure/FormRecognizerLiveTestBase.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/Infrastructure/FormRecognizerLiveTestBase.cs new file mode 100644 index 000000000000..a429d0a7544a --- /dev/null +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/Infrastructure/FormRecognizerLiveTestBase.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Azure.AI.FormRecognizer.Training; +using Azure.Core.TestFramework; + +namespace Azure.AI.FormRecognizer.Tests +{ + public class FormRecognizerLiveTestBase : RecordedTestBase + { + public FormRecognizerLiveTestBase(bool isAsync) : base(isAsync) + { + Sanitizer = new FormRecognizerRecordedTestSanitizer(); + Matcher = new FormRecognizerRecordMatcher(); + } + + /// + /// Creates a with the endpoint and API key provided via environment + /// variables and instruments it to make use of the Azure Core Test Framework functionalities. + /// + /// The instrumented . + protected FormTrainingClient CreateInstrumentedFormTrainingClient() + { + var endpoint = new Uri(TestEnvironment.Endpoint); + var credential = new AzureKeyCredential(TestEnvironment.ApiKey); + + var options = Recording.InstrumentClientOptions(new FormRecognizerClientOptions()); + var client = new FormTrainingClient(endpoint, credential, options); + + return InstrumentClient(client); + } + } +} diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromFile.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromFile.cs index 811be863076b..d8cf26dff028 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromFile.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromFile.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Azure.AI.FormRecognizer.Models; using Azure.AI.FormRecognizer.Tests; +using Azure.AI.FormRecognizer.Training; using Azure.Core.TestFramework; using NUnit.Framework; @@ -15,16 +16,26 @@ namespace Azure.AI.FormRecognizer.Samples public partial class FormRecognizerSamples : SamplesBase { [Test] - [Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")] 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)).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 = ""; + string modelId = model.ModelId; using (FileStream stream = new FileStream(formFilePath, FileMode.Open)) { @@ -46,6 +57,9 @@ public async Task RecognizeCustomFormsFromFile() } } } + + // Delete the model on completion to clean environment. + trainingClient.DeleteModel(model.ModelId); } } } diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromUri.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromUri.cs index 3c9ca4eae00b..08e71b0ba915 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromUri.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample3_RecognizeCustomFormsFromUri.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Azure.AI.FormRecognizer.Models; using Azure.AI.FormRecognizer.Tests; +using Azure.AI.FormRecognizer.Training; using Azure.Core.TestFramework; using NUnit.Framework; @@ -14,18 +15,29 @@ namespace Azure.AI.FormRecognizer.Samples public partial class FormRecognizerSamples : SamplesBase { [Test] - [Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")] public async Task RecognizeCustomFormsFromUri() { 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)).WaitForCompletionAsync(); + + // Proceed with the custom form recognition. FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); string formUri = FormRecognizerTestEnvironment.CreateUri("Form_1.jpg"); - string modelId = ""; + string modelId = model.ModelId; #region Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri + //@@ string modelId = ""; Response> forms = await client.StartRecognizeCustomFormsFromUri(modelId, new Uri(formUri)).WaitForCompletionAsync(); foreach (RecognizedForm form in forms.Value) @@ -45,6 +57,9 @@ public async Task RecognizeCustomFormsFromUri() } } #endregion + + // Delete the model on completion to clean environment. + trainingClient.DeleteModel(model.ModelId); } } } diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/SampleSnippets.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/SampleSnippets.cs index 980221fe3b20..1178d0aa752c 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/SampleSnippets.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/SampleSnippets.cs @@ -112,27 +112,39 @@ public async Task RecognizeReceiptFromFile() } [Test] - [Ignore("Need to revisit how to pass the modelId. Issue https://github.com/Azure/azure-sdk-for-net/issues/11493")] 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 = ""; + string modelId = model.ModelId; #region Snippet:FormRecognizerRecognizeCustomFormsFromFile using (FileStream stream = new FileStream(formFilePath, FileMode.Open)) { + //@@ string modelId = ""; + Response> forms = await client.StartRecognizeCustomForms(modelId, stream).WaitForCompletionAsync(); /* * */ } #endregion + + // Delete the model on completion to clean environment. + trainingClient.DeleteModel(model.ModelId); } } }