c# - 使用自定义模型通过 C# 中的 Forms Recognizer V3 nuget 从表中提取数据

标签 c# azure azure-web-app-service azure-form-recognizer

在表单识别器中创建一个自定义模型,在其中创建两种类型的标签,一种是“字段”类型,另一种是我逐个单元地容纳的“表格”类型。一旦我的模型经过训练和测试,我就开始在 .net 6 中创建一个 API,它将执行信息的分析和提取,并可以创建一个 Web 应用程序。

我只有一个问题,在下面的代码中,它返回给我一个 Json,乍一看我认为一切都是正确的,因为它甚至创建了一个客户端,其中显示了通过键/值/置信度获得的所有信息。

我的代码:

            AzureKeyCredential azureKey = new(_formRecognizerKey);
            DocumentAnalysisClient analysisClient = new(new Uri(_formRecognizerEndPoint), azureKey);
            AnalyzeDocumentOperation analyzeDocumentOperation = await analysisClient.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, _formRecognizerCustomModelId, new Uri(urlblob));
            AnalyzeResult result = analyzeDocumentOperation.Value;
            foreach (AnalyzedDocument document in result.Documents)
            {
                foreach (KeyValuePair<string, DocumentField> fieldKvp in document.Fields)
                {
                    string fielName = fieldKvp.Key;
                    DocumentField field = fieldKvp.Value;¿
                    response.DocumentsFields.Add(new DocumentFieldResult { Key = fielName, Values = field.Content, Confidence = confidence });
                }
            }

在表的键中,它们返回空值,我调试了以下行中的代码:

AnalyzeResult result = analyzeDocumentOperation.Value;
            foreach (AnalyzedDocument document in result.Documents)
                   foreach (KeyValuePair<string, DocumentField> fieldKvp in document.Fields)
                {

结果 objt 完美地返回所有内容,我需要的所有信息都在那里,但由于我注释掉了标记为表的信息,因此该值显示为 null。 我尝试测试文档中的代码,但我发现这不是我需要的,因为我对进行 OCR 分析不感兴趣,我感兴趣 该分析基于我的模型。

现在...有了所有评论,表格是否应该被区别对待?在调试中,我看到那些字段中的 FieldType 以字符串形式出现,对于标记为表的字段,它们以 FieldType:List 形式出现。

最佳答案

您可以使用以下语法提取 Azure Form Recognizer v3.0 中自定义模型中定义的动态表的内容

              if (document.Fields.TryGetValue("<dynamicTableFieldName>", out DocumentField itemsField))
                    {
                        if (itemsField.FieldType == DocumentFieldType.List)
                        {
                            foreach (DocumentField itemField in itemsField.Value.AsList())
                            {
                                Console.WriteLine("Item:");

                                if (itemField.FieldType == DocumentFieldType.Dictionary)
                                {
                                    IReadOnlyDictionary<string, DocumentField> itemFields = itemField.Value.AsDictionary();

                                    if (itemFields.TryGetValue("<dynamicTableColumnName>", out DocumentField itemDescriptionField))
                                    {
                                        if (itemDescriptionField.FieldType == DocumentFieldType.String)
                                        {
                                            string itemDescription = itemDescriptionField.Value.AsString();

                                            Console.WriteLine($"  Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}");
                                        }
                                    }

                                   
                                }
                            }
                        }
                    }

我从这个链接 [here]得到了这个代码

关于c# - 使用自定义模型通过 C# 中的 Forms Recognizer V3 nuget 从表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76105532/

相关文章:

c# - Json.NET Windows Store App认证崩溃

azure - 如何在 Mac 上的 Visual Studio Code 中本地运行在 Visual Studio 2019 Windows 中创建的多个 C# Az 函数应用程序?

visual-studio - Visual Studio 无法检索服务 'Microsoft.VisualStudio.WindowsAzure.Authentication.IAzureRMTenantService'

c# - 如何在 Windows 10(十月更新)中获取剪贴板历史记录?

c# - UserHostAddress 的代码是否相同?

c# - 磁盘访问的子域托管问题 (C#/ASP.NET 3.5)

python-2.7 - 无法在 Azure LAMP shell 服务器上使用 pip 安装 Python 包

dns - Azure Front Door 配置的站点返回 "Services not available"

azure - 将子域重定向到 Azure 上的目录

azure - 如何使用 Azure .NET Fluent SDK 获取 Azure 应用服务 TLS 配置信息?