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 :-(
Почему не передается имущество документов?
Были обнаружены ошибки, когда метод AnalyzeDocumentAsync
не возвращает ни одного поля при анализе документа из потока, хотя при анализе из URI он работает нормально. Вы можете изменить свой код, чтобы обеспечить правильное положение потока.
С 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
Выход:
Без 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