Esempio n. 1
0
        private async Task <RecognizedForm> AzureReceiptRecognition(String receipt)
        {
            var stream = File.Open(receipt, FileMode.Open);

            var endpoint  = Config.AzureEndpoint;
            var apiKey    = Config.AzureKey;
            var client    = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            var operation = await client.StartRecognizeReceiptsAsync(stream);

            await operation.WaitForCompletionAsync();


            if (!operation.HasValue)
            {
                return(null);
            }

            var form = operation.Value.Single();

            return(form);
        }
        public async Task RecognizeReceiptsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileStream
#if SNIPPET
            string receiptPath = "<receiptPath>";
#else
            string receiptPath = FormRecognizerTestEnvironment.CreatePath("contoso-receipt.jpg");
#endif

            using var stream = new FileStream(receiptPath, FileMode.Open);
            var options = new RecognizeReceiptsOptions()
            {
                Locale = "en-US"
            };

            RecognizeReceiptsOperation operation = await client.StartRecognizeReceiptsAsync(stream, options);

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

            RecognizedFormCollection receipts = operationResponse.Value;

            // To see the list of the supported fields returned by service and its corresponding types, consult:
            // https://aka.ms/formrecognizer/receiptfields

            foreach (RecognizedForm receipt in receipts)
            {
                if (receipt.Fields.TryGetValue("MerchantName", out FormField merchantNameField))
                {
                    if (merchantNameField.Value.ValueType == FieldValueType.String)
                    {
                        string merchantName = merchantNameField.Value.AsString();

                        Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {merchantNameField.Confidence}");
                    }
                }

                if (receipt.Fields.TryGetValue("TransactionDate", out FormField transactionDateField))
                {
                    if (transactionDateField.Value.ValueType == FieldValueType.Date)
                    {
                        DateTime transactionDate = transactionDateField.Value.AsDate();

                        Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {transactionDateField.Confidence}");
                    }
                }

                if (receipt.Fields.TryGetValue("Items", out FormField itemsField))
                {
                    if (itemsField.Value.ValueType == FieldValueType.List)
                    {
                        foreach (FormField itemField in itemsField.Value.AsList())
                        {
                            Console.WriteLine("Item:");

                            if (itemField.Value.ValueType == FieldValueType.Dictionary)
                            {
                                IReadOnlyDictionary <string, FormField> itemFields = itemField.Value.AsDictionary();

                                if (itemFields.TryGetValue("Name", out FormField itemNameField))
                                {
                                    if (itemNameField.Value.ValueType == FieldValueType.String)
                                    {
                                        string itemName = itemNameField.Value.AsString();

                                        Console.WriteLine($"  Name: '{itemName}', with confidence {itemNameField.Confidence}");
                                    }
                                }

                                if (itemFields.TryGetValue("TotalPrice", out FormField itemTotalPriceField))
                                {
                                    if (itemTotalPriceField.Value.ValueType == FieldValueType.Float)
                                    {
                                        float itemTotalPrice = itemTotalPriceField.Value.AsFloat();

                                        Console.WriteLine($"  Total Price: '{itemTotalPrice}', with confidence {itemTotalPriceField.Confidence}");
                                    }
                                }
                            }
                        }
                    }
                }

                if (receipt.Fields.TryGetValue("Total", out FormField totalField))
                {
                    if (totalField.Value.ValueType == FieldValueType.Float)
                    {
                        float total = totalField.Value.AsFloat();

                        Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'");
                    }
                }
            }
            #endregion
        }
Esempio n. 3
0
        public async Task RecognizeReceiptsFromFile()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleCreateClient
            FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            #endregion

            string receiptPath = FormRecognizerTestEnvironment.CreatePath("contoso-receipt.jpg");

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileStream
            using (FileStream stream = new FileStream(receiptPath, FileMode.Open))
            {
                RecognizedFormCollection receipts = await client.StartRecognizeReceiptsAsync(stream).WaitForCompletionAsync();

                // To see the list of the supported fields returned by service and its corresponding types, consult:
                // https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult

                foreach (RecognizedForm receipt in receipts)
                {
                    FormField merchantNameField;
                    if (receipt.Fields.TryGetValue("MerchantName", out merchantNameField))
                    {
                        if (merchantNameField.Value.Type == FieldValueType.String)
                        {
                            string merchantName = merchantNameField.Value.AsString();

                            Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {merchantNameField.Confidence}");
                        }
                    }

                    FormField transactionDateField;
                    if (receipt.Fields.TryGetValue("TransactionDate", out transactionDateField))
                    {
                        if (transactionDateField.Value.Type == FieldValueType.Date)
                        {
                            DateTime transactionDate = transactionDateField.Value.AsDate();

                            Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {transactionDateField.Confidence}");
                        }
                    }

                    FormField itemsField;
                    if (receipt.Fields.TryGetValue("Items", out itemsField))
                    {
                        if (itemsField.Value.Type == FieldValueType.List)
                        {
                            foreach (FormField itemField in itemsField.Value.AsList())
                            {
                                Console.WriteLine("Item:");

                                if (itemField.Value.Type == FieldValueType.Dictionary)
                                {
                                    IReadOnlyDictionary <string, FormField> itemFields = itemField.Value.AsDictionary();

                                    FormField itemNameField;
                                    if (itemFields.TryGetValue("Name", out itemNameField))
                                    {
                                        if (itemNameField.Value.Type == FieldValueType.String)
                                        {
                                            string itemName = itemNameField.Value.AsString();

                                            Console.WriteLine($"    Name: '{itemName}', with confidence {itemNameField.Confidence}");
                                        }
                                    }

                                    FormField itemTotalPriceField;
                                    if (itemFields.TryGetValue("TotalPrice", out itemTotalPriceField))
                                    {
                                        if (itemTotalPriceField.Value.Type == FieldValueType.Float)
                                        {
                                            float itemTotalPrice = itemTotalPriceField.Value.AsFloat();

                                            Console.WriteLine($"    Total Price: '{itemTotalPrice}', with confidence {itemTotalPriceField.Confidence}");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    FormField totalField;
                    if (receipt.Fields.TryGetValue("Total", out totalField))
                    {
                        if (totalField.Value.Type == FieldValueType.Float)
                        {
                            float total = totalField.Value.AsFloat();

                            Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'");
                        }
                    }
                }
            }
            #endregion
        }
 public async Task <RecognizedFormCollection> AnalyzeReceiptFromStream(Stream receipt)
 => await client.StartRecognizeReceiptsAsync(receipt).WaitForCompletionAsync();
Esempio n. 5
0
        public async Task <RecognizedFormCollection> AnalyzeReceiptFromStream(Stream receipt)
        {
            RecognizedFormCollection collection = await _frClient.StartRecognizeReceiptsAsync(receipt).WaitForCompletionAsync();

            return(collection);
        }