Azure 文档智能/客户端。AnalyzeDocumentAsync 不起作用

问题描述 投票:0回答:1

client.AnalyzeDocumentFromUriAsync 工作正常。

但是当我提交与 FileFromUri 完全相同的本地文件的流时,它不会返回任何字段。这是为什么?

    Dim invoiceUri As Uri = New Uri("https://blablabla.ch/testfile_xy.pdf")
        Dim operation As AnalyzeDocumentOperation = Await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri)
        Dim result As AnalyzeResult = operation.Value
        
        Dim filePath = "c:\users\myname\desktop\testfile_xy.pdf" 
        Dim stream = New IO.FileStream(filePath, IO.FileMode.Open)
        Dim operation2 As AnalyzeDocumentOperation = Await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", stream)
        Dim result2 As AnalyzeResult = operation2.Value
        stream.Close()

    Debug.Print(result.Documents.Count) '1  :-)
        Debug.Print(result2.Documents.Count) '0  :-(
        

为什么没有传输文档属性?

vb.net azure azure-form-recognizer
1个回答
0
投票

在从流分析文档时,

AnalyzeDocumentAsync
方法不会返回任何字段,但在从 URI 分析时却工作正常,因此遇到了错误。您可以修改代码以确保正确的流位置。

  • 使用 URL 和本地路径是两种不同的方法。
  • 我们不应该同时使用两者,除非我们想要同时来自本地和 URL 源的结果。该 URL 应有效。例如,我使用此示例 URL 和 Azure 存储 Blob SAS URL。

附网址:

Imports System
Imports System.IO
Imports System.Threading.Tasks
Imports Azure
Imports Azure.AI.FormRecognizer
Imports Azure.AI.FormRecognizer.DocumentAnalysis
Imports Azure.AI.FormRecognizer.Models

Module Program
    Sub Main(args As String())
        MainAsync().GetAwaiter().GetResult()
    End Sub

    Async Function MainAsync() As Task
        Dim filePath = "c:\users\myname\desktop\testfile_xy.pdf"
        Dim endpoint As String = "<endpoint>" ' Replace <endpoint> with your Form Recognizer endpoint
        Dim apiKey As String = "<apiKey>" ' Replace <apiKey> with your Form Recognizer API key

        Dim client As New DocumentAnalysisClient(New Uri(endpoint), New AzureKeyCredential(apiKey))

        ' Analyzing document from URI
        Dim invoiceUri As Uri = New Uri("https://blablabla.ch/testfile_xy.pdf")
        Dim operation As AnalyzeDocumentOperation = Await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri)
        Dim result As AnalyzeResult = operation.Value

        ' Analyzing document from local file stream
        Dim stream = New IO.FileStream(filePath, IO.FileMode.Open)
        stream.Seek(0, SeekOrigin.Begin) ' Reset the position of the stream to the beginning
        Dim operation2 As AnalyzeDocumentOperation = Await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", stream)
        Dim result2 As AnalyzeResult = operation2.Value
        stream.Close()

        ' Displaying results
        Console.WriteLine("Results from analyzing document from URI:")
        Console.WriteLine($"Document count: {result.Documents.Count}")
        For Each document As AnalyzedDocument In result.Documents
            Console.WriteLine($"Document of type: {document.DocumentType}")
            For Each field In document.Fields
                Console.WriteLine($"Field '{field.Key}': ")
                Console.WriteLine($"  Content: '{field.Value.Content}'")
                Console.WriteLine($"  Confidence: '{field.Value.Confidence}'")
            Next
        Next

        Console.WriteLine()

        Console.WriteLine("Results from analyzing document from local file stream:")
        Console.WriteLine($"Document count: {result2.Documents.Count}")
        For Each document As AnalyzedDocument In result2.Documents
            Console.WriteLine($"Document of type: {document.DocumentType}")
            For Each field In document.Fields
                Console.WriteLine($"Field '{field.Key}': ")
                Console.WriteLine($"  Content: '{field.Value.Content}'")
                Console.WriteLine($"  Confidence: '{field.Value.Confidence}'")
            Next
        Next
    End Function
End Module

输出: enter image description here

没有来自本地路径的 URL:

Imports System
Imports System.IO
Imports System.Threading.Tasks
Imports Azure
Imports Azure.AI.FormRecognizer
Imports Azure.AI.FormRecognizer.DocumentAnalysis
Imports Azure.AI.FormRecognizer.Models

Module Program
    Sub Main(args As String())
        Dim filePath As String = "C://Users//sample-1.pdf" ' Replace <filePath> with the actual path to your file

        If Not File.Exists(filePath) Then
            Console.WriteLine($"File not found: {filePath}")
            Return
        End If

        Dim endpoint As String = "<endpoint>";' Replace <endpoint> with your Form Recognizer endpoint
        Dim apiKey As String = "<apiKey>" ' Replace <apiKey> with your Form Recognizer API key

        Dim client As New DocumentAnalysisClient(New Uri(endpoint), New AzureKeyCredential(apiKey))

        Using stream As New FileStream(filePath, FileMode.Open)
            Dim operation As AnalyzeDocumentOperation = client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", stream).Result
            Dim result As AnalyzeResult = operation.Value

            ' Add your processing logic here
            ' For example, accessing the extracted data:
            For Each document As AnalyzedDocument In result.Documents
                Console.WriteLine($"Document of type: {document.DocumentType}")

                For Each field In document.Fields
                    Console.WriteLine($"Field '{field.Key}': ")
                    Console.WriteLine($"  Content: '{field.Value.Content}'")
                    Console.WriteLine($"  Confidence: '{field.Value.Confidence}'")
                Next
            Next
        End Using
    End Sub
End Module

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.