Azure Databricks 和表单识别器 - 无效图像或密码保护
Azure Databricks and Form Recognizer - Invalid Image or password protected
我正在尝试使用 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)。我的容器的 public 访问级别设置为“容器”。
为了运行代码没有错误,是否需要更改任何配置?
谢谢你,祝你有愉快的一天
在我看来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()
感谢休伯特的回复。
事实证明,在我的 blob 集群中,我有两个 PDF 文件,还有一个保存的表单识别器自定义模型。我删除了 .fott 和 .json 文件,使集群中只有 PDF。我运行之后的代码没有任何问题。
谢谢
我正在尝试使用 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)。我的容器的 public 访问级别设置为“容器”。
为了运行代码没有错误,是否需要更改任何配置?
谢谢你,祝你有愉快的一天
在我看来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()
感谢休伯特的回复。
事实证明,在我的 blob 集群中,我有两个 PDF 文件,还有一个保存的表单识别器自定义模型。我删除了 .fott 和 .json 文件,使集群中只有 PDF。我运行之后的代码没有任何问题。
谢谢