GoogleCloudPlatform / GoogleCloudPlatform/document-ai-samples

error running the code u provided to use document ai form parser

Open
#971 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
332
Forks
116
PR merge metrics
No merged PRs in 30d

Description

i'm trying to make a project of form parser and i did what all it takes as provided in ur lab and when i run the code ( i will write it down) it didn't generate an output as expected and i have tried to get the error and i don't know why, even ai assistant didn't help me well, so can u plz help me urgently?

here is the code:

import pandas as pd
from google.cloud import documentai

def online_process(
project_id: str,
location: str,
processor_id: str,
file_path: str,
mime_type: str,
) -> documentai.Document:
"""
Processes a document using the Document AI Online Processing API.
"""

opts = {"api_endpoint": f"{location}-documentai.googleapis.com"}

# Instantiates a client
documentai_client = documentai.DocumentProcessorServiceClient(client_options=opts)

# 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
resource_name = documentai_client.processor_path(project_id, location, processor_id)

# Read the file into memory
with open(file_path, "rb") as image:
image_content = image.read()

# Load Binary Data into Document AI RawDocument Object
raw_document = documentai.RawDocument(
content=image_content, mime_type=mime_type
)

# Configure the process request
request = documentai.ProcessRequest(
name=resource_name, raw_document=raw_document
)

# Use the Document AI client to process the sample form
result = documentai_client.process_document(request=request)

return result.document

def get_table_data(
rows: Sequence[documentai.Document.Page.Table.TableRow], text: str
) -> List[List[str]]:
"""
Get Text data from table rows
"""
all_values: List[List[str]] = []
for row in rows:
current_row_values: List[str] = []
for cell in row.cells:
current_row_values.append(
text_anchor_to_text(cell.layout.text_anchor, text)
)
all_values.append(current_row_values)
return all_values

def text_anchor_to_text(text_anchor: documentai.Document.TextAnchor, text: str) -> str:
"""
Document AI identifies table data by their offsets in the entirety of the
document's text. This function converts offsets to a string.
"""
response = ""
# If a text segment spans several lines, it will
# be stored in different text segments.
for segment in text_anchor.text_segments:
start_index = int(segment.start_index)
end_index = int(segment.end_index)
response += text[start_index:end_index]
return response.strip().replace("\n", " ")

PROJECT_ID = "crypto-canyon-448716-s0"
LOCATION = "us" # Format is 'us' or 'eu'
PROCESSOR_ID = "51900352c3a79d07" # Create processor in Cloud Console

# The local file in your current working directory
FILE_PATH = "1-15.pdf"
# Refer to https://cloud.google.com/document-ai/docs/processors-list
# for supported file types
MIME_TYPE = "application/pdf"

document = online_process(
project_id=PROJECT_ID,
location=LOCATION,
processor_id=PROCESSOR_ID,
file_path=FILE_PATH,
mime_type=MIME_TYPE,
)

header_row_values: List[List[str]] = []
body_row_values: List[List[str]] = []

# Input Filename without extension
output_file_prefix = splitext(FILE_PATH)[0]

for page in document.pages:
for index, table in enumerate(page.tables):
header_row_values = get_table_data(table.header_rows, document.text)
body_row_values = get_table_data(table.body_rows, document.text)

# Create a Pandas Dataframe to print the values in tabular format.
df = pd.DataFrame(
data=body_row_values,
columns=pd.MultiIndex.from_arrays(header_row_values),
)

print(f"Page {page.page_number} - Table {index}")
print(df)

# Save each table as a CSV file
output_filename = f"{output_file_prefix}_pg{page.page_number}_tb{index}.csv"
df.to_csv(output_filename, index=False)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.