private static async Task TestUrl(string endpoint, string apiKey, string modelId, string url) { FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); Uri formFileUri = new Uri(url); RecognizeCustomFormsOperation operation = await client.StartRecognizeCustomFormsFromUriAsync(modelId, formFileUri); 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 CheckFormTypeinSubmodelAndRecognizedForm(bool labeled) { var client = CreateFormTrainingClient(); var formClient = client.GetFormRecognizerClient(); var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl); TrainingOperation trainingOperation = await client.StartTrainingAsync(trainingFilesUri, labeled); await trainingOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(trainingOperation.HasValue); CustomFormModel model = trainingOperation.Value; Assert.IsNotNull(model.Submodels.FirstOrDefault().FormType); var uri = FormRecognizerTestEnvironment.CreateUri(TestFile.Form1); RecognizeCustomFormsOperation recognizeOperation = await formClient.StartRecognizeCustomFormsFromUriAsync(model.ModelId, uri); await recognizeOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(recognizeOperation.HasValue); RecognizedForm form = recognizeOperation.Value.Single(); Assert.IsNotNull(form.FormType); Assert.AreEqual(form.FormType, model.Submodels.FirstOrDefault().FormType); }
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.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:FormRecognizerSampleRecognizeCustomFormsFromUri #if SNIPPET string modelId = "<modelId>"; Uri formUri = < formUri >; #else Uri formUri = FormRecognizerTestEnvironment.CreateUri("Form_1.jpg"); string modelId = model.ModelId; #endif RecognizeCustomFormsOperation operation = await client.StartRecognizeCustomFormsFromUriAsync(modelId, formUri); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; foreach (RecognizedForm form in forms) { Console.WriteLine($"Form of type: {form.FormType}"); if (form.FormTypeConfidence.HasValue) { Console.WriteLine($"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}': "); 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); }
private static async Task AnalyzeDynamicCustomForm(FormRecognizerClient recognizerClient, string trainingFileUrl, string formUrl) { RecognizeCustomFormsOptions options = new RecognizeCustomFormsOptions(); //train model FormTrainingClient trainingClient = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); CustomFormModel model = await trainingClient.StartTrainingAsync(new Uri(trainingFileUrl), useTrainingLabels : false, $"VIS-Dynamic-Model-{DateTime.Now.ToShortDateString()}-{DateTime.Now.ToLongTimeString()}").WaitForCompletionAsync(); //string modelId = inModelID; string modelId = model.ModelId; //recognize form RecognizeCustomFormsOperation operation = await recognizerClient.StartRecognizeCustomFormsFromUriAsync(modelId, new Uri(formUrl)); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; foreach (RecognizedForm form in forms) { returnString += $"Form of type: {form.FormType}{Environment.NewLine}"; foreach (FormField field in form.Fields.Values) { returnString += $"Field '{field.Name}: "; if (field.LabelData != null) { returnString += $" Label: '{field.LabelData.Text}"; } returnString += $" Value: '{field.ValueData.Text}"; returnString += $" Confidence: '{field.Confidence}{Environment.NewLine}"; } returnString += $"Table data:{Environment.NewLine}"; foreach (FormPage page in form.Pages) { for (int i = 0; i < page.Tables.Count; i++) { FormTable table = page.Tables[i]; //Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns."); foreach (FormTableCell cell in table.Cells) { returnString += $" Cell ({cell.RowIndex}, {cell.ColumnIndex}) contains {(cell.IsHeader ? "header" : "text")}: '{cell.Text}'{Environment.NewLine}"; } } } } // Delete the model on completion to clean environment. await trainingClient.DeleteModelAsync(model.ModelId); }
private static async Task <bool> IsExpensiveAsync(string modelId, Uri documentUri, FormRecognizerClient client) { RecognizeCustomFormsOperation operation = await client.StartRecognizeCustomFormsFromUriAsync(modelId, documentUri); Response <RecognizedFormCollection> response = await operation.WaitForCompletionAsync(); RecognizedForm form = response.Value[0]; if (form.Fields.TryGetValue("totalPrice", out FormField totalPriceField) && totalPriceField.Value.ValueType == FieldValueType.Float) { return(totalPriceField.Confidence > 0.7f && totalPriceField.Value.AsFloat() > 100f); } else { return(false); } }
public async Task RecognizeCustomFormsOperationCanPollFromNewObject() { var client = CreateFormRecognizerClient(out var nonInstrumentedClient); RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false); using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Blank); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } var sameOperation = new RecognizeCustomFormsOperation(operation.Id, nonInstrumentedClient); await sameOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(sameOperation.HasValue); Assert.AreEqual(1, sameOperation.Value.Count); }
private static async Task AnalyzeCustomForm(FormRecognizerClient recognizerClient, string modelID, string formUrl) { RecognizeCustomFormsOptions options = new RecognizeCustomFormsOptions(); //recognize form RecognizeCustomFormsOperation operation = await recognizerClient.StartRecognizeCustomFormsFromUriAsync(modelID, new Uri(formUrl)); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; foreach (RecognizedForm form in forms) { returnString += $"Form of type: {form.FormType}{Environment.NewLine}"; foreach (FormField field in form.Fields.Values) { returnString += $"Field '{field.Name}: "; if (field.LabelData != null) { returnString += $" Label: '{field.LabelData.Text}"; } returnString += $" Value: '{field.ValueData.Text}"; returnString += $" Confidence: '{field.Confidence}{Environment.NewLine}"; } returnString += $"Table data:{Environment.NewLine}"; foreach (FormPage page in form.Pages) { for (int i = 0; i < page.Tables.Count; i++) { FormTable table = page.Tables[i]; //Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns."); foreach (FormTableCell cell in table.Cells) { returnString += $" Cell ({cell.RowIndex}, {cell.ColumnIndex}) contains {(cell.IsHeader ? "header" : "text")}: '{cell.Text}'{Environment.NewLine}"; } } } } }
public async Task RecognizeCustomFormsOperationCanPollFromNewObject() { // Skip instrumenting here because the internal service client passed to the operation object would be made null otherwise, // making the test fail. var client = CreateFormRecognizerClient(skipInstrumenting: true); RecognizeCustomFormsOperation operation; await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels : false); using var stream = FormRecognizerTestEnvironment.CreateStream(TestFile.Blank); using (Recording.DisableRequestBodyRecording()) { operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream); } var sameOperation = new RecognizeCustomFormsOperation(operation.Id, client); await sameOperation.WaitForCompletionAsync(PollingInterval); Assert.IsTrue(sameOperation.HasValue); Assert.AreEqual(1, sameOperation.Value.Count); }
private async Task <List <dynamic> > Evaluate(RecognizeCustomFormsOperation operation) { Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; List <dynamic> res = new(); foreach (RecognizedForm form in forms) { _logger.LogInformation($"Form of type: {form.FormType}"); _logger.LogInformation($"Form analyzed with model ID: {form.ModelId}"); dynamic exo = new System.Dynamic.ExpandoObject(); foreach (FormField field in form.Fields.Values) { ((IDictionary <string, object>)exo).Add(field.Name, new { Value = field?.ValueData?.Text, field?.Confidence }); } res.Add(exo); } return(res); }
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); }
private static async Task AnalyzeCustomFormReturnObj(FormRecognizerClient recognizerClient, string modelID, string formUrl) { RecognizeCustomFormsOptions options = new RecognizeCustomFormsOptions(); //recognize form RecognizeCustomFormsOperation operation = await recognizerClient.StartRecognizeCustomFormsFromUriAsync(modelID, new Uri(formUrl)); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; foreach (RecognizedForm form in forms) { FormResponseObj rObj = new FormResponseObj(); foreach (FormField field in form.Fields.Values) { switch (field.Name) { case "Birthdate": { rObj.Birthdate = field.ValueData.Text; } break; case "Breed": { rObj.Breed = field.ValueData.Text; } break; case "Color": { rObj.Color = field.ValueData.Text; } break; case "DateOfVaccination": { rObj.DateOfVaccination = field.ValueData.Text; } break; case "DueDate": { rObj.DueDate = field.ValueData.Text; } break; case "Duration": { rObj.Duration = field.ValueData.Text; } break; case "LotExpiration": { rObj.LotExpiration = field.ValueData.Text; } break; case "Manufacturer": { rObj.Manufacturer = field.ValueData.Text; } break; case "MicrochipNumber": { rObj.MicrochipNumber = field.ValueData.Text; } break; case "Owner": { rObj.Owner = field.ValueData.Text; } break; case "OwnerAddress": { rObj.OwnerAddress = field.ValueData.Text; } break; case "OwnerPhone": { rObj.OwnerPhone = field.ValueData.Text; } break; case "PetsName": { rObj.PetsName = field.ValueData.Text; } break; case "SerialNumber": { rObj.SerialNumber = field.ValueData.Text; } break; case "Sex": { rObj.Sex = field.ValueData.Text; } break; case "Species": { rObj.Species = field.ValueData.Text; } break; case "TagNumber": { rObj.TagNumber = field.ValueData.Text; } break; case "VaccinationLocation": { rObj.VaccinationLocation = field.ValueData.Text; } break; case "Weight": { rObj.Weight = field.ValueData.Text; } break; } } returnObjects.Add(rObj); } }
public async Task CreateComposedModel() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; string trainingFileUrl = TestEnvironment.BlobContainerSasUrlV2; FormTrainingClient client = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); #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 bool useLabels = true; #if SNIPPET Uri officeSuppliesUri = new Uri("<purchaseOrderOfficeSuppliesUri>"); #else Uri officeSuppliesUri = new Uri(trainingFileUrl); #endif string suppliesModelName = "Purchase order - Office supplies"; TrainingOperation suppliesOperation = await client.StartTrainingAsync(officeSuppliesUri, useLabels, suppliesModelName); Response <CustomFormModel> suppliesOperationResponse = await suppliesOperation.WaitForCompletionAsync(); CustomFormModel officeSuppliesModel = suppliesOperationResponse.Value; #if SNIPPET Uri officeEquipmentUri = new Uri("<purchaseOrderOfficeEquipmentUri>"); #else Uri officeEquipmentUri = new Uri(trainingFileUrl); #endif string equipmentModelName = "Purchase order - Office Equipment"; TrainingOperation equipmentOperation = await client.StartTrainingAsync(officeEquipmentUri, useLabels, equipmentModelName); Response <CustomFormModel> equipmentOperationResponse = await equipmentOperation.WaitForCompletionAsync(); CustomFormModel officeEquipmentModel = equipmentOperationResponse.Value; #if SNIPPET Uri furnitureUri = new Uri("<purchaseOrderFurnitureUri>"); #else Uri furnitureUri = new Uri(trainingFileUrl); #endif string furnitureModelName = "Purchase order - Furniture"; TrainingOperation furnitureOperation = await client.StartTrainingAsync(furnitureUri, useLabels, furnitureModelName); Response <CustomFormModel> furnitureOperationResponse = await furnitureOperation.WaitForCompletionAsync(); CustomFormModel furnitureModel = furnitureOperationResponse.Value; #if SNIPPET Uri cleaningSuppliesUri = new Uri("<purchaseOrderCleaningSuppliesUri>"); #else Uri cleaningSuppliesUri = new Uri(trainingFileUrl); #endif string cleaningModelName = "Purchase order - Cleaning Supplies"; TrainingOperation cleaningOperation = await client.StartTrainingAsync(cleaningSuppliesUri, useLabels, cleaningModelName); Response <CustomFormModel> cleaningOperationResponse = await cleaningOperation.WaitForCompletionAsync(); CustomFormModel cleaningSuppliesModel = cleaningOperationResponse.Value; #endregion #region Snippet:FormRecognizerSampleCreateComposedModelV3 List <string> modelIds = new List <string>() { officeSuppliesModel.ModelId, officeEquipmentModel.ModelId, furnitureModel.ModelId, cleaningSuppliesModel.ModelId }; string purchaseModelName = "Composed Purchase order"; CreateComposedModelOperation operation = await client.StartCreateComposedModelAsync(modelIds, purchaseModelName); Response <CustomFormModel> operationResponse = await operation.WaitForCompletionAsync(); CustomFormModel purchaseOrderModel = operationResponse.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; 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} models"); 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 #region Snippet:FormRecognizerSampleRecognizeCustomFormWithComposedModel #if SNIPPET string purchaseOrderFilePath = "<purchaseOrderFilePath>"; #else string purchaseOrderFilePath = FormRecognizerTestEnvironment.CreatePath("Form_1.jpg"); #endif FormRecognizerClient recognizeClient = client.GetFormRecognizerClient(); using var stream = new FileStream(purchaseOrderFilePath, FileMode.Open); RecognizeCustomFormsOperation recognizeOperation = await recognizeClient.StartRecognizeCustomFormsAsync(purchaseOrderModel.ModelId, stream); Response <RecognizedFormCollection> recognizeOperationResponse = await recognizeOperation.WaitForCompletionAsync(); RecognizedFormCollection forms = recognizeOperationResponse.Value; // 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(officeSuppliesModel.ModelId).ConfigureAwait(false); await client.DeleteModelAsync(officeEquipmentModel.ModelId).ConfigureAwait(false); await client.DeleteModelAsync(furnitureModel.ModelId).ConfigureAwait(false); await client.DeleteModelAsync(cleaningSuppliesModel.ModelId).ConfigureAwait(false); await client.DeleteModelAsync(purchaseOrderModel.ModelId).ConfigureAwait(false); }
public static async Task RunAsync([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log) { //Extracting content type and url of the blob triggering the function var jsondata = JsonConvert.SerializeObject(eventGridEvent.Data); var tmp = new { contentType = "", url = "" }; var data = JsonConvert.DeserializeAnonymousType(jsondata, tmp); //Checking if the trigger was iniatiated for a PDF File. if (data.contentType == "application/pdf") { var pdfUrl = data.url; string endpoint = System.Environment.GetEnvironmentVariable("FormsRecognizerEndpoint", EnvironmentVariableTarget.Process); string apiKey = System.Environment.GetEnvironmentVariable("FormsRecognizerKey", EnvironmentVariableTarget.Process); string contosoStorageConnectionString = System.Environment.GetEnvironmentVariable("ContosoStorageConnectionString", EnvironmentVariableTarget.Process); string cosmosEndpointUrl = System.Environment.GetEnvironmentVariable("CosmosDBEndpointUrl", EnvironmentVariableTarget.Process); string cosmosPrimaryKey = System.Environment.GetEnvironmentVariable("CosmosDBPrimaryKey", EnvironmentVariableTarget.Process); //Create a SAS Link to give Forms Recognizer read access to the document BlobServiceClient blobServiceClient = new BlobServiceClient(contosoStorageConnectionString); BlobContainerClient container = new BlobContainerClient(contosoStorageConnectionString, "claims"); string blobName = pdfUrl.Split('/').Last(); BlobClient blob = container.GetBlobClient(blobName); BlobSasBuilder sasBuilder = new BlobSasBuilder() { BlobContainerName = blob.GetParentBlobContainerClient().Name, BlobName = blob.Name, Resource = "b" }; sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1); sasBuilder.SetPermissions(BlobSasPermissions.Read); Uri sasUri = blob.GenerateSasUri(sasBuilder); var credential = new AzureKeyCredential(apiKey); //Get the latest trained model var formTrainingClient = new FormTrainingClient(new Uri(endpoint), credential); Pageable <CustomFormModelInfo> formsModels = formTrainingClient.GetCustomModels(); var latestModel = (from inc in formsModels orderby inc.TrainingCompletedOn descending select inc).FirstOrDefault(); //Run the document through the model var formRecognizerClient = new FormRecognizerClient(new Uri(endpoint), credential); var options = new RecognizeCustomFormsOptions() { IncludeFieldElements = true }; RecognizeCustomFormsOperation operation = formRecognizerClient.StartRecognizeCustomFormsFromUri(latestModel.ModelId, sasUri, options); Azure.Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection forms = operationResponse.Value; //Insert documents into CosmosDB var cosmosClient = new CosmosClient(cosmosEndpointUrl, cosmosPrimaryKey); var cosmosDatabase = (await cosmosClient.CreateDatabaseIfNotExistsAsync("Contoso")).Database; var cosmosContainer = (await cosmosDatabase.CreateContainerIfNotExistsAsync("Claims", "/InsuredID")).Container; Model.ClaimsDocument processedDocument = new Model.ClaimsDocument(); processedDocument.DocumentDate = new DateTime(int.Parse(blobName.Substring(0, 4)), int.Parse(blobName.Substring(4, 2)), int.Parse(blobName.Substring(6, 2))); processedDocument.PatientName = forms[0].Fields["PatientName"].ValueData?.Text; processedDocument.InsuredID = forms[0].Fields["InsuredID"].ValueData?.Text; processedDocument.Diagnosis = forms[0].Fields["Diagnosis"].ValueData?.Text; decimal.TryParse(forms[0].Fields["TotalCharges"].ValueData?.Text, out decimal totalCharges); processedDocument.TotalCharges = totalCharges; DateTime.TryParse(forms[0].Fields["PatientBirthDate"].ValueData?.Text, out DateTime patientBirthDate); processedDocument.PatientBirthDate = patientBirthDate; decimal.TryParse(forms[0].Fields["AmountPaid"].ValueData?.Text, out decimal amountPaid); processedDocument.AmountPaid = amountPaid; decimal.TryParse(forms[0].Fields["AmountDue"].ValueData?.Text, out decimal amountDue); processedDocument.AmountDue = amountDue; processedDocument.FileName = blobName; if (processedDocument.InsuredID == null) { processedDocument.Id = Guid.NewGuid().ToString(); } else { processedDocument.Id = processedDocument.InsuredID; } try { ItemResponse <Model.ClaimsDocument> cosmosResponse = await cosmosContainer.CreateItemAsync(processedDocument, new PartitionKey(processedDocument.InsuredID)); } catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.Conflict) { //Conflicting EnsurerID is silently ignored for demo purposes. } } log.LogInformation(eventGridEvent.Data.ToString()); }