![](/img/trans.png)
[英]Document AI: google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument
[英]Google Document AI api authentication error
我正在尝试使用谷歌提供的文档 AI API 中的发票解析器。 即使我已经按照他们文档中的所有必需步骤进行操作,我仍然会收到以下错误。 我已经更新了我的 python 包,相应地安装了所有东西,设置了所有环境变量,专门为此创建了一个服务帐户,但我不断收到以下错误。
我在 Linux mint 21 上运行这个
我的代码
import os
import google.auth
credentials, project = google.auth.default()
print(credentials)
print(project)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = '/home/build/invoice-text-extraction/service_account.json'
LOCATION = 'us'
PROJECT_ID = 'my_project'
PROCESSOR_ID = 'akjdh2513h1j2'
FILE_PATH = 'files/Final Collection 251835416.pdf'
def quickstart(project_id: str, location: str, processor_id: str, file_path: str):
from google.cloud import documentai as documentai
# You must set the api_endpoint if you use a location other than 'us', e.g.:
opts = {}
if location == "eu":
opts = {"api_endpoint": "eu-documentai.googleapis.com"}
client = documentai.DocumentProcessorServiceClient(client_options=opts, credentials=credentials)
# The full resource name of the processor, e.g.:
# projects/project-id/locations/location/processor/processor-id
# You must create new processors in the Cloud Console first
name = f"projects/{project_id}/locations/{location}/processors/{processor_id}"
# Read the file into memory
with open(file_path, "rb") as image:
image_content = image.read()
document = {"content": image_content, "mime_type": "application/pdf"}
# Configure the process request
request = {"name": name, "raw_document": document}
result = client.process_document(request=request)
document = result.document
document_pages = document.pages
# For a full list of Document object attributes, please reference this page: https://googleapis.dev/python/documentai/latest/_modules/google/cloud/documentai_v1beta3/types/document.html#Document
# Read the text recognition output from the processor
print("The document contains the following paragraphs:")
for page in document_pages:
paragraphs = page.paragraphs
for paragraph in paragraphs:
print(paragraph)
paragraph_text = get_text(paragraph.layout, document)
print(f"Paragraph text: {paragraph_text}")
def get_text(doc_element: dict, document: dict):
"""
Document AI identifies form fields by their offsets
in document text. This function converts offsets
to text snippets.
"""
response = ""
# If a text segment spans several lines, it will
# be stored in different text segments.
for segment in doc_element.text_anchor.text_segments:
start_index = (
int(segment.start_index)
if segment in doc_element.text_anchor.text_segments
else 0
)
end_index = int(segment.end_index)
response += document.text[start_index:end_index]
return response
quickstart(PROCESSOR_ID, LOCATION, PROCESSOR_ID, FILE_PATH)
Traceback (most recent call last):
File "main.py", line 71, in <module>
quickstart(PROCESSOR_ID, LOCATION, PROCESSOR_ID, FILE_PATH)
File "main.py", line 19, in quickstart
client = documentai.DocumentProcessorServiceClient(client_options=opts)
File "/usr/local/lib/python3.8/dist-packages/google/cloud/documentai_v1/services/document_processor_service/client.py", line 364, in __init__
self._transport = Transport(
File "/usr/local/lib/python3.8/dist-packages/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py", line 166, in __init__
self._grpc_channel = type(self).create_channel(
File "/usr/local/lib/python3.8/dist-packages/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py", line 218, in create_channel
return grpc_helpers.create_channel(
File "/usr/local/lib/python3.8/dist-packages/google/api_core/grpc_helpers.py", line 306, in create_channel
composite_credentials = _create_composite_credentials(
File "/usr/local/lib/python3.8/dist-packages/google/api_core/grpc_helpers.py", line 236, in _create_composite_credentials
credentials = google.auth.credentials.with_scopes_if_required(
TypeError: with_scopes_if_required() got an unexpected keyword argument 'default_scopes'
我正在使用他们在此处提供的 python 的代码示例。
由于这是最初发布的,文档 AI API 添加了一个功能来指定处理请求中的field_mask
,这限制了文档 object output 中返回的字段。这可以减少某些请求的延迟,因为响应将更小。
https://cloud.google.com/document-ai/docs/send-request#online-processor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.