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); }
private static async Task CreateModel(string endpoint, string apiKey, string trainingSetUrl, string modelName) { // Create a new model to store in the account Uri trainingSetUri = new Uri(trainingSetUrl); FormTrainingClient client = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); TrainingOperation operation = client.StartTraining(trainingSetUri, useTrainingLabels: true, modelName); Response <CustomFormModel> operationResponse = await operation.WaitForCompletionAsync(); CustomFormModel model = operationResponse.Value; // Get the model that was just created CustomFormModel modelCopy = client.GetCustomModel(model.ModelId); Console.WriteLine($"Custom Model with Id {modelCopy.ModelId} and name {modelCopy.ModelName} recognizes the following form types:"); foreach (CustomFormSubmodel submodel in modelCopy.Submodels) { Console.WriteLine($"Submodel Form Type: {submodel.FormType}"); foreach (CustomFormModelField field in submodel.Fields.Values) { Console.Write($" FieldName: {field.Name}"); if (field.Label != null) { Console.Write($", FieldLabel: {field.Label}"); } Console.WriteLine(""); } } //// Delete the model from the account. //client.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 ManageCustomModels() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; string trainingFileUrl = TestEnvironment.BlobContainerSasUrl; #region Snippet:FormRecognizerSampleManageCustomModels FormTrainingClient client = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); // Check number of models in the FormRecognizer account, and the maximum number of models that can be stored. AccountProperties accountProperties = client.GetAccountProperties(); Console.WriteLine($"Account has {accountProperties.CustomModelCount} models."); Console.WriteLine($"It can have at most {accountProperties.CustomModelLimit} models."); // List the first ten or fewer models currently stored in the account. Pageable <CustomFormModelInfo> models = client.GetCustomModels(); foreach (CustomFormModelInfo modelInfo in models.Take(10)) { Console.WriteLine($"Custom Model Info:"); Console.WriteLine($" Model Id: {modelInfo.ModelId}"); Console.WriteLine($" Model display name: {modelInfo.DisplayName}"); Console.WriteLine($" Is composed model: {modelInfo.Properties.IsComposedModel}"); Console.WriteLine($" Model Status: {modelInfo.Status}"); Console.WriteLine($" Training model started on: {modelInfo.TrainingStartedOn}"); Console.WriteLine($" Training model completed on: {modelInfo.TrainingCompletedOn}"); } // Create a new model to store in the account CustomFormModel model = await client.StartTraining(new Uri(trainingFileUrl), useTrainingLabels : false, new TrainingOptions() { ModelDisplayName = "My new model" }).WaitForCompletionAsync(); // Get the model that was just created CustomFormModel modelCopy = client.GetCustomModel(model.ModelId); Console.WriteLine($"Custom Model with Id {modelCopy.ModelId} and name {modelCopy.DisplayName} recognizes the following form types:"); foreach (CustomFormSubmodel submodel in modelCopy.Submodels) { Console.WriteLine($"Submodel Form Type: {submodel.FormType}"); foreach (CustomFormModelField field in submodel.Fields.Values) { Console.Write($" FieldName: {field.Name}"); if (field.Label != null) { Console.Write($", FieldLabel: {field.Label}"); } Console.WriteLine(""); } } // Delete the model from the account. client.DeleteModel(model.ModelId); #endregion }
public async Task CopyModel() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; string trainingFileUrl = TestEnvironment.BlobContainerSasUrl; string resourceId = TestEnvironment.TargetResourceId; string resourceRegion = TestEnvironment.TargetResourceRegion; #region Snippet:FormRecognizerSample6CreateCopySourceClient //@@ string endpoint = "<source_endpoint>"; //@@ string apiKey = "<source_apiKey>"; var sourcecredential = new AzureKeyCredential(apiKey); var sourceClient = new FormTrainingClient(new Uri(endpoint), sourcecredential); #endregion // For the purpose of this sample, we are going to create a trained model to copy. Please note that // if you already have a model, this is not neccesary. CustomFormModel model = await sourceClient.StartTraining(new Uri(trainingFileUrl)).WaitForCompletionAsync(); string modelId = model.ModelId; #region Snippet:FormRecognizerSample6CreateCopyTargetClient //@@ string endpoint = "<target_endpoint>"; //@@ string apiKey = "<target_apiKey>"; var targetCredential = new AzureKeyCredential(apiKey); var targetClient = new FormTrainingClient(new Uri(endpoint), targetCredential); #endregion #region Snippet:FormRecognizerSample6GetCopyAuthorization //@@ string resourceId = "<resourceId>"; //@@ string resourceRegion = "<region>"; CopyAuthorization targetAuth = await targetClient.GetCopyAuthorizationAsync(resourceId, resourceRegion); #endregion #region Snippet:FormRecognizerSample6ToJson string jsonTargetAuth = targetAuth.ToJson(); #endregion #region Snippet:FormRecognizerSample6FromJson CopyAuthorization targetCopyAuth = CopyAuthorization.FromJson(jsonTargetAuth); #endregion #region Snippet:FormRecognizerSample6CopyModel //@@ string modelId = "<source_modelId>"; CustomFormModelInfo newModel = await sourceClient.StartCopyModelAsync(modelId, targetCopyAuth).WaitForCompletionAsync(); Console.WriteLine($"Original modelID => {modelId}"); Console.WriteLine($"Copied modelID => {newModel.ModelId}"); #endregion }
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}")); } } }
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), useTrainingLabels : false).WaitForCompletionAsync(); // Proceed with the custom form recognition. FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); Uri formUri = FormRecognizerTestEnvironment.CreateUri("Form_1.jpg"); string modelId = model.ModelId; #region Snippet:FormRecognizerSample3RecognizeCustomFormsFromUri //@@ string modelId = "<modelId>"; RecognizedFormCollection forms = await client.StartRecognizeCustomFormsFromUri(modelId, formUri).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}"); } } #endregion // Delete the model on completion to clean environment. trainingClient.DeleteModel(model.ModelId); }
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 = model.ModelId; 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}"); } } } // Delete the model on completion to clean environment. trainingClient.DeleteModel(model.ModelId); }