public async Task StronglyTypingARecognizedForm()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string receiptUri = FormRecognizerTestEnvironment.CreateUriString("contoso-receipt.jpg");

            #region Snippet:FormRecognizerSampleStronglyTypingARecognizedForm
            RecognizedFormCollection recognizedForms = await client.StartRecognizeReceiptsFromUri(new Uri(receiptUri)).WaitForCompletionAsync();

            foreach (RecognizedForm recognizedForm in recognizedForms)
            {
                Receipt receipt = new Receipt(recognizedForm);

                if (receipt.MerchantName != null)
                {
                    string merchantName = receipt.MerchantName;
                    Console.WriteLine($"Merchant Name: '{merchantName}', with confidence {receipt.MerchantName.Confidence}");
                }

                if (receipt.TransactionDate != null)
                {
                    DateTime transactionDate = receipt.TransactionDate;
                    Console.WriteLine($"Transaction Date: '{transactionDate}', with confidence {receipt.TransactionDate.Confidence}");
                }

                foreach (ReceiptItem item in receipt.Items)
                {
                    Console.WriteLine("Item:");

                    if (item.Name != null)
                    {
                        string name = item.Name;
                        Console.WriteLine($"    Name: '{name}', with confidence {item.Name.Confidence}");
                    }

                    if (item.TotalPrice != null)
                    {
                        float totalPrice = item.TotalPrice;
                        Console.WriteLine($"    Total Price: '{totalPrice}', with confidence {item.TotalPrice.Confidence}");
                    }
                }

                if (receipt.Total != null)
                {
                    float total = receipt.Total;
                    Console.WriteLine($"Total: '{total}', with confidence {receipt.Total.Confidence}");
                }
            }
            #endregion
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AnalyzeReceiptV2(string receiptUri)
        {
            try
            {
                FormRecognizerClient     recognizerClient = AuthenticateClient();
                RecognizedFormCollection receipts         = await recognizerClient.StartRecognizeReceiptsFromUri(new Uri(receiptUri)).WaitForCompletionAsync();

                if (receipts != null)
                {
                    return(new OkObjectResult(receipts));
                }
                return(new BadRequestObjectResult("No result"));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.ToString()));
            }
        }
Ejemplo n.º 3
0
        public async Task BadRequestSnippet()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            #region Snippet:FormRecognizerBadRequest
            try
            {
                RecognizedReceiptCollection receipts = await client.StartRecognizeReceiptsFromUri(new Uri("http://invalid.uri")).WaitForCompletionAsync();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.ToString());
            }
            #endregion
        }
        public async Task RecognizeReceiptsFromUri()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string receiptUri = FormRecognizerTestEnvironment.JpgReceiptUri;

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileFromUri
            RecognizedReceiptCollection receipts = await client.StartRecognizeReceiptsFromUri(new Uri(receiptUri)).WaitForCompletionAsync();

            foreach (var receipt in receipts)
            {
                USReceipt usReceipt = receipt.AsUSReceipt();

                string   merchantName               = usReceipt.MerchantName?.Value ?? default;
                DateTime transactionDate            = usReceipt.TransactionDate?.Value ?? default;
                IReadOnlyList <USReceiptItem> items = usReceipt.Items ?? default;
                float subtotal = usReceipt.Subtotal?.Value ?? default;
                float tax      = usReceipt.Tax?.Value ?? default;
                float tip      = usReceipt.Tip?.Value ?? default;
                float total    = usReceipt.Total?.Value ?? default;

                Console.WriteLine($"Recognized USReceipt fields:");
                Console.WriteLine($"    Merchant Name: '{merchantName}', with confidence {usReceipt.MerchantName.Confidence}");
                Console.WriteLine($"    Transaction Date: '{transactionDate}', with confidence {usReceipt.TransactionDate.Confidence}");

                for (int i = 0; i < items.Count; i++)
                {
                    USReceiptItem item = usReceipt.Items[i];
                    Console.WriteLine($"    Item {i}:  Name: '{item.Name.Value}', Quantity: '{item.Quantity?.Value}', Price: '{item.Price?.Value}'");
                    Console.WriteLine($"    TotalPrice: '{item.TotalPrice.Value}'");
                }

                Console.WriteLine($"    Subtotal: '{subtotal}', with confidence '{usReceipt.Subtotal.Confidence}'");
                Console.WriteLine($"    Tax: '{tax}', with confidence '{usReceipt.Tax.Confidence}'");
                Console.WriteLine($"    Tip: '{tip}', with confidence '{usReceipt.Tip?.Confidence ?? 0.0f}'");
                Console.WriteLine($"    Total: '{total}', with confidence '{usReceipt.Total.Confidence}'");
            }
            #endregion
        }
    // </snippet_getcontent_print>

    // <snippet_receipt_call>
    private static async Task AnalyzeReceipt(
        FormRecognizerClient recognizerClient, string receiptUri)
    {
        RecognizedFormCollection receipts = await recognizerClient.StartRecognizeReceiptsFromUri(new Uri(receiptUrl)).WaitForCompletionAsync();

        // </snippet_receipt_call>

        // <snippet_receipt_print>
        foreach (RecognizedForm receipt in receipts)
        {
            FormField merchantNameField;
            if (receipt.Fields.TryGetValue("MerchantName", out merchantNameField))
            {
                if (merchantNameField.Value.ValueType == 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.ValueType == 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.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();

                            FormField itemNameField;
                            if (itemFields.TryGetValue("Name", out itemNameField))
                            {
                                if (itemNameField.Value.ValueType == 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.ValueType == 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.ValueType == FieldValueType.Float)
                {
                    float total = totalField.Value.AsFloat();

                    Console.WriteLine($"Total: '{total}', with confidence '{totalField.Confidence}'");
                }
            }
        }
    }
        public async Task RecognizeReceiptsFromUri()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string receiptUri = FormRecognizerTestEnvironment.CreateUriString("contoso-receipt.jpg");

            #region Snippet:FormRecognizerSampleRecognizeReceiptFileFromUri
            RecognizedReceiptCollection receipts = await client.StartRecognizeReceiptsFromUri(new Uri(receiptUri)).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 (RecognizedReceipt receipt in receipts)
            {
                FormField merchantNameField;
                if (receipt.RecognizedForm.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.RecognizedForm.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.RecognizedForm.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.RecognizedForm.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
        }