我正在嘗試使用 Databricks 自動化 Azure 表單識別器程序。我會將我的 pdf 或 jpg 檔案放在 blob 中并在 Databricks 中運行一個代碼,該代碼會將檔案發送到表單識別器,執行資料識別并將結果放入 blob 中的新 csv 檔案中。
這是代碼:
1. Install packages to cloud
%pip install azure.storage.blob
%pip install azure.ai.formrecognizer
2. Connect to Azure Storage Container
from azure.storage.blob import ContainerClient
container_url = "https://nameofmystorageaccount.blob.core.windows.net/pdf-raw"
container = ContainerClient.from_container_url(container_url)
for blob in container.list_blobs():
blob_url = container_url "/" blob.name
print(blob_url)
3. Enable Cognitive Services
import requests
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
endpoint = "https://nameofmyendpoint.cognitiveservices.azure.com/"
key = "nameofmykey"
form_recognizer_client = FormRecognizerClient(endpoint, credential=AzureKeyCredential(key))
4. Send files to Cognitive Services
import pandas as pd
field_list = ["InvoiceDate","InvoiceID","Items","VendorName"]
df = pd.DataFrame(columns=field_list)
for blob in container.list_blobs():
blob_url = container_url "/" blob.name
poller = form_recognizer_client.begin_recognize_invoices_from_url(invoice_url=blob_url)
invoices = poller.result()
print("Scanning " blob.name "...")
for idx, invoice in enumerate(invoices):
single_df = pd.DataFrame(columns=field_list)
for field in field_list:
entry = invoice.fields.get(field)
if entry:
single_df[field] = [entry.value]
single_df['FileName'] = blob.name
df = df.append(single_df)
df = df.reset_index(drop=True)
df
前三個步驟運行沒有問題,但我在第四步收到以下錯誤訊息:(InvalidImage) The input data is not a valid image or password protected. 此錯誤涉及行poller = form_recognizer_client.begin_recognize_invoices_from_url(invoice_url=blob_url)
當我手動執行此程序時,我的 blob 中的 PDF 沒有密碼并且可以在表單識別器中正確運行。我只使用兩個小 PDF 進行測驗,并嘗試使用不同的 PDF。
我使用 Azure 的免費訂閱。我的 Databricks 集群具有不受限制的策略,采用單節點集群模式。運行時版本為 9.1 LTS(Apache 3.1.2,Scala 2.12)。我的容器的公共訪問級別設定為“容器”。
是否有任何配置需要更改才能正確運行代碼?
謝謝你,祝你有美好的一天
uj5u.com熱心網友回復:
在我看來 url 不是公開可用的,并且無法正確下載。
最好的方法是傳遞整個檔案并使用不同的方法:
form_recognizer_client = FormRecognizerClient(endpoint, credential)
with open("<path to your invoice>", "rb") as fd:
invoice = fd.read()
poller = form_recognizer_client.begin_recognize_invoices(invoice)
result = poller.result()
uj5u.com熱心網友回復:
感謝您的回應休伯特。
原來,在我的 blob 集群中,我有兩個 PDF 檔案,還有一個保存的表單識別器自定義模型。我洗掉了 .fott 和 .json 檔案,只在集群中包含 PDF。之后我運行代碼沒有任何問題。
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388000.html
