从Azure Blob触发函数中调用Azure Forms Recognizer出错

时间:2019-09-03 08:49:27

标签: microsoft-cognitive

从Azure函数(Blob触发器)中调用Azure Forms Recognizer .NET SDK时出现错误(BadRequest)

我基本上是在文档中C#快速入门中使用示例代码。我已经训练了模型,并且可以使用.URL从.NET Core命令行应用程序成功调用Form Recognizer。我现在想触发相同的流程,但是当PDF上传到Azure存储Blob时。在函数中使用相同的modelId,订阅和端点时,调用AnalyzeWithCustomModelAsync()时出现BadRequest错误。我在myBlob Stream上尝试了许多变体,创建了FileStream,MemoryStream,并使用传递给Function的uri创建了新的Stream等。

[FunctionName("ImportInvoice")]
        public static async System.Threading.Tasks.Task RunAsync([BlobTrigger("originals/{name}", Connection = "invoiceConn")]Stream myBlob, string name, Uri uri, string blobTrigger, ILogger log)
        {
            log.LogInformation($"ImportInvoice Function triggered by new invoice: {name} size: {myBlob.Length} Bytes");
            log.LogInformation($"Invoice Uri: {uri.AbsoluteUri}");
            log.LogInformation($"System.IO.Stream.CanRead is: {myBlob.CanRead}");

            try
            {
                IFormRecognizerClient formClient = new FormRecognizerClient(new ApiKeyServiceClientCredentials(subscriptionKey))
                {
                    Endpoint = formRecognizerEndpoint
                };

                using (Stream invoiceStream = myBlob)
                {
                    log.LogInformation($"About to analyse with custom model {modelId}");

                    // Fails here with BadRequest
                    AnalyzeResult result = await formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf");

                    log.LogInformation("Invoice analysed");

                    foreach (var page in result.Pages)
                    {
                        foreach (var kv in page.KeyValuePairs)
                        {
                            if (kv.Key.Count > 0 && kv.Value.Count > 0)
                                log.LogInformation(kv.Key[0].Text + ": " + kv.Value[0].Text);
                        }
                    }
                }
            }
            catch (ErrorResponseException e)
            {
                log.LogInformation("Analyze PDF form : " + e.Message);
            }
            catch (Exception ex)
            {
                log.LogInformation("Analyze PDF form : " + ex.Message);
            }
        }

我正在传递modelId,将Stream传递给函数(myBlob)和正确的内容类型(application / pdf)。我希望formClient.AnalyzeWithCustomModelAsync(modelId,myBlob,contentType:“ application / pdf”)调用能够成功并查看键和值(同一代码在函数外部起作用),但我得到以下信息:

Executing 'ImportInvoice' (Reason='New blob detected: originals/NewCareInvoice5.pdf', Id=63f26c79-f269-4ea8-b6e8-8b4f6d6eb6e2)
ImportInvoice Function triggered by new invoice: NewCareInvoice5.pdf size: 134507 Bytes
Invoice Uri: https://myinvoices.blob.core.windows.net/originals/NewCareInvoice5.pdf
System.IO.Stream.CanRead is: True
About to analyse with custom model <correct model id shown here>
Analyze PDF form : Operation returned an invalid status code 'BadRequest'

我假设问题出在Stream上,但是我转了一圈试图引用它,或者创建或复制一个新的Stream,似乎丢失了一些东西。我无法获得有关该错误的更多详细信息(例如,来自InnerException)。

任何帮助,不胜感激!

2 个答案:

答案 0 :(得分:0)

Microsoft已将表单识别更新为V2.0预览,带有更新的API和教程,请尝试使用新版本。 http://aka.ms/formrecognizer

答案 1 :(得分:0)

这是Form Recognizer SDK上的错误。 我希望它能尽快修复。

https://github.com/MicrosoftDocs/azure-docs/issues/40938