vb.netazureazure-form-recognizer

Azure Document Intelligence / client.AnalyzeDocumentAsync does not work


client.AnalyzeDocumentFromUriAsync works fine.

But when I submit the stream of the exact same local file as the FileFromUri, it does not return any fields. Why is that?

    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  :-(
        

Why is no documents property transmitted?


Solution

  • Errors have been encountered where the AnalyzeDocumentAsync method does not return any fields when analyzing a document from a stream, while it works fine when analyzing from a URI. You can modify your code to ensure the correct stream position.

    With 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 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://")
            Dim operation As AnalyzeDocumentOperation = Await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri)
            Dim result As AnalyzeResult = operation.Value
    
            ' 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
        End Function
    End Module
    
    

    Output: enter image description here

    Without URL from Local path:

    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