Example #1
0
 public OnboardingController()
 {
     _formClient = new FormRecognizerClient(new ApiKeyServiceClientCredentials(FormRecogniserSettings.ApiKey))
     {
         Endpoint = FormRecogniserSettings.Endpoint
     };
 }
 public MlModelService(IFormRecognizerClient formRecognizerClient, CloudBlobClient cloudBlobClient, AzureSettings azureStorageSettings, ILogger logger)
 {
     _formRecognizerClient = formRecognizerClient;
     _cloudBlobClient      = cloudBlobClient;
     _azureStorageSettings = azureStorageSettings;
     _logger = logger;
 }
        private string AnalyzePdfForm(IFormRecognizerClient formClient, Guid formRecognizerModelId, string filePath)
        {
            //TODO: As this is part of .NET Library, writing to console does not makes sense
            // Write to an Exceptions section either in jsonResult or another output and send it to caller
            if (string.IsNullOrEmpty(filePath))
            {
                Console.WriteLine("\nInvalid file path.");
                return(null);
            }

            string jsonResult = "";

            try
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open))
                {
                    //Get the result from an asynchronous call but as a synchnous way by using .Result
                    AnalyzeResult result = formClient.AnalyzeWithCustomModelAsync(formRecognizerModelId, stream, contentType: "application/pdf").Result;
                    jsonResult = TransformAnalyzeResult(result);
                }
            }
            catch (ErrorResponseException e)
            {
                Console.WriteLine("Analyze PDF form : " + e.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Analyze PDF form : " + ex.Message);
            }

            return(jsonResult);
        }
Example #4
0
        // </snippet_train>

        // <snippet_getkeys>
        // Get and display list of extracted keys for training data
        // provided to train the model
        private static async Task GetListOfExtractedKeys(
            IFormRecognizerClient formClient, Guid modelId)
        {
            if (modelId == Guid.Empty)
            {
                Console.WriteLine("\nInvalid model Id.");
                return;
            }

            try
            {
                KeysResult kr = await formClient.GetExtractedKeysAsync(modelId);

                var clusters = kr.Clusters;
                foreach (var kvp in clusters)
                {
                    Console.WriteLine("  Cluster: " + kvp.Key + "");
                    foreach (var v in kvp.Value)
                    {
                        Console.WriteLine("\t" + v);
                    }
                }
            }
            catch (ErrorResponseException e)
            {
                Console.WriteLine("Get list of extracted keys : " + e.Message);
            }
        }
Example #5
0
 /// <summary>
 /// Get Keys
 /// </summary>
 /// <remarks>
 /// Retrieve the keys that were
 /// extracted during the training of the specified model.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='id'>
 /// Model identifier.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <KeysResult> GetExtractedKeysAsync(this IFormRecognizerClient operations, System.Guid id, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.GetExtractedKeysWithHttpMessagesAsync(id, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #6
0
 /// <summary>
 /// Get Models
 /// </summary>
 /// <remarks>
 /// Get information about all trained custom models
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ModelsResult> GetCustomModelsAsync(this IFormRecognizerClient operations, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.GetCustomModelsWithHttpMessagesAsync(null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #7
0
 /// <summary>
 /// This interface is used for getting the analysis results of a 'Batch Read
 /// Receipt' operation. The URL to this interface should be retrieved from the
 /// 'Operation-Location' field returned from the 'Batch Read Receipt'
 /// operation.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='operationId'>
 /// Id of read operation returned in the response of a 'Batch Read Receipt'
 /// operation.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ReadReceiptResult> GetReadReceiptResultAsync(this IFormRecognizerClient operations, string operationId, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.GetReadReceiptResultWithHttpMessagesAsync(operationId, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #8
0
 /// <summary>
 /// Train Model
 /// </summary>
 /// <remarks>
 /// Create and train a custom model. The train request must include a source
 /// parameter that is either an externally accessible Azure Storage blob
 /// container Uri (preferably a Shared Access Signature Uri) or valid path to a
 /// data folder in a locally mounted drive. When local paths are specified,
 /// they must follow the Linux/Unix path format and be an absolute path rooted
 /// to the input mount configuration
 /// setting value e.g., if '{Mounts:Input}' configuration setting value is
 /// '/input' then a valid source path would be '/input/contosodataset'. All
 /// data to be trained is expected to be directly under the source folder.
 /// Subfolders are not supported. Models are trained using documents that are
 /// of the following content type - 'application/pdf', 'image/jpeg' and
 /// 'image/png'."
 /// Other type of content is ignored.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='trainRequest'>
 /// Request object for training.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <TrainResult> TrainCustomModelAsync(this IFormRecognizerClient operations, TrainRequest trainRequest, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.TrainCustomModelWithHttpMessagesAsync(trainRequest, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Example #9
0
        // Analyze PNG form data
        private static async Task AnalyzePngForm(
            IFormRecognizerClient formClient, Guid modelId, string pngFormFile)
        {
            if (string.IsNullOrEmpty(pngFormFile))
            {
                Console.WriteLine("\nInvalid pngFormFile.");
                return;
            }

            try
            {
                using (FileStream stream = new FileStream(pngFormFile, FileMode.Open))
                {
                    AnalyzeResult result = await formClient.AnalyzeWithCustomModelAsync(modelId, stream, contentType : "image/png");

                    Console.WriteLine("\nExtracted data from:" + pngFormFile);
                    DisplayAnalyzeResult(result);
                }
            }
            catch (ErrorResponseException e)
            {
                Console.WriteLine("Analyze PNG form  " + e.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Analyze PNG  form  : " + ex.Message);
            }
        }
Example #10
0
 /// <summary>
 /// Batch Read Receipt operation. The response contains a field called
 /// 'Operation-Location', which contains the URL that you must use for your
 /// 'Get Read Receipt Result' operation.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='url'>
 /// Publicly reachable URL of an image.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <BatchReadReceiptHeaders> BatchReadReceiptAsync(this IFormRecognizerClient operations, string url, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BatchReadReceiptWithHttpMessagesAsync(url, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Headers);
     }
 }
Example #11
0
        // </snippet_maintask>

        // <snippet_train>
        // Train model using training form data (pdf, jpg, png files)
        private static async Task <Guid> TrainModelAsync(
            IFormRecognizerClient formClient, string trainingDataUrl)
        {
            if (!Uri.IsWellFormedUriString(trainingDataUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid trainingDataUrl:\n{0} \n", trainingDataUrl);
                return(Guid.Empty);
            }

            try
            {
                TrainResult result = await formClient.TrainCustomModelAsync(new TrainRequest(trainingDataUrl));

                ModelResult model = await formClient.GetCustomModelAsync(result.ModelId);

                DisplayModelStatus(model);

                return(result.ModelId);
            }
            catch (ErrorResponseException e)
            {
                Console.WriteLine("Train Model : " + e.Message);
                return(Guid.Empty);
            }
        }
Example #12
0
 /// <summary>
 /// Analyze Form
 /// </summary>
 /// <remarks>
 /// Extract key-value pairs from a given document. The input document must be
 /// of one of the supported content types - 'application/pdf', 'image/jpeg' or
 /// 'image/png'. A success response is returned in JSON.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='id'>
 /// Model Identifier to analyze the document with.
 /// </param>
 /// <param name='formStream'>
 /// A pdf document or image (jpg,png) file to analyze.
 /// </param>
 /// <param name='keys'>
 /// An optional list of known keys to extract the values for.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <AnalyzeResult> AnalyzeWithCustomModelAsync(this IFormRecognizerClient operations, System.Guid id, Stream formStream, IList <string> keys = default(IList <string>), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.AnalyzeWithCustomModelWithHttpMessagesAsync(id, formStream, keys, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 public DocumentAnalyzerService(IFormRecognizerClient formRecognizerClient, IMlModelService mlModelService, IFileBuilder fileBuilder, IS3FileService s3FileService, ILogger logger)
 {
     _formRecognizerClient = formRecognizerClient;
     _mlModelService       = mlModelService;
     _fileBuilder          = fileBuilder;
     _s3FileService        = s3FileService;
     _logger = logger;
 }
Example #14
0
 private static async Task DeleteModel(IFormRecognizerClient formClient, Guid modelId)
 {
     try
     {
         await formClient.DeleteCustomModelAsync(modelId);
     }
     catch (ErrorResponseException e)
     {
     }
 }
Example #15
0
        // Delete a model
        private static async Task DeleteModel(IFormRecognizerClient formClient, Guid modelId)
        {
            try {
                Console.Write("Deleting model: {0}...", modelId.ToString());
                await formClient.DeleteCustomModelAsync(modelId);

                Console.WriteLine("done.\n");
            } catch (ErrorResponseException e) {
                Console.WriteLine("Delete model : " + e.Message);
            }
        }
Example #16
0
 private async Task <AnalyzeResult> AnalyzePdfForm(IFormRecognizerClient formClient, Guid modelId, Stream pdfFormFile)
 {
     try
     {
         return(await formClient.AnalyzeWithCustomModelAsync(modelId, pdfFormFile, contentType : "application/pdf"));
     }
     catch (ErrorResponseException e)
     {
         return(null);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Example #17
0
        // Get and display list of trained the models
        private static async Task GetListOfModels(IFormRecognizerClient formClient)
        {
            try {
                ModelsResult models = await formClient.GetCustomModelsAsync();

                foreach (ModelResult m in models.ModelsProperty)
                {
                    Console.WriteLine(m.ModelId + " " + m.Status + " " + m.CreatedDateTime + " " +
                                      m.LastUpdatedDateTime);
                }
                Console.WriteLine();
            } catch (ErrorResponseException e) {
                Console.WriteLine("Get list of models : " + e.Message);
            }
        }
        public async Task <Guid> TrainModelAsync(IFormRecognizerClient client, string sasUrl)
        {
            ModelResult model = null;

            try
            {
                TrainResult result = await client.TrainCustomModelAsync(new TrainRequest(sasUrl));

                model = await client.GetCustomModelAsync(result.ModelId);

                //Check if successful
            }
            catch (ErrorResponseException e)
            {
                LogError(e);
                return(Guid.Empty);
            }
            return(model.ModelId);
        }
        public async Task <AnalyzeResult> AnalyzeForm(IFormRecognizerClient client, Guid modelId, string filePath)
        {
            AnalyzeResult result = null;
            //Default to PDF
            string contentType = "application/pdf";

            switch (Path.GetExtension(filePath).ToLower().Replace(".", ""))
            {
            case "pdf":
                contentType = "application/pdf";
                break;

            case "jpg":
                contentType = "image/jpeg";
                break;

            case "png":
                contentType = "image/png";
                break;

            default:
                break;
            }

            try
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    result = await client.AnalyzeWithCustomModelAsync(modelId, stream, contentType : contentType);
                }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }

            //used the following to serialize the result object and use for testing
            //var jsonVersion = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            //System.IO.File.WriteAllText(Shared.FormRecognitionService.PATH_TO_PROJECT_ROOT + @"\Code\SampleData\SerializedObjects\AnalyzedResult.json", jsonVersion);

            return(result);
        }
Example #20
0
        private static async Task <Guid> TrainModelAsync(IFormRecognizerClient formClient, string trainingDataUrl)
        {
            if (!Uri.IsWellFormedUriString(trainingDataUrl, UriKind.Absolute))
            {
                return(Guid.Empty);
            }

            try
            {
                var result = await formClient.TrainCustomModelAsync(new TrainRequest(trainingDataUrl));

                var model = await formClient.GetCustomModelAsync(result.ModelId);

                return(result.ModelId);
            }
            catch (ErrorResponseException e)
            {
                return(Guid.Empty);
            }
        }
Example #21
0
 /// <summary>
 /// Delete Model
 /// </summary>
 /// <remarks>
 /// Delete model artifacts.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='id'>
 /// The identifier of the model to delete.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task DeleteCustomModelAsync(this IFormRecognizerClient operations, System.Guid id, CancellationToken cancellationToken = default(CancellationToken))
 {
     (await operations.DeleteCustomModelWithHttpMessagesAsync(id, null, cancellationToken).ConfigureAwait(false)).Dispose();
 }
 public AnalysisService(IOptions <Settings> settings)
 {
     _settings   = settings.Value;
     _formClient = CreateFormRecognizerClient();
 }