체크포인트
Enable the Document AI API
/ 50
Create a processor
/ 50
Form Parsing with Document AI (Python)
GSP1139
Overview
Document AI is a document understanding solution that takes unstructured data (e.g. documents, emails, invoices, forms, etc.) and makes the data easier to understand, analyze, and consume. The API provides structure through content classification, entity extraction, advanced searching, and more.
In this lab, you will learn how to use the Document AI Form Parser to parse a handwritten form with Python. You will use a simple medical intake form as an example, but this procedure will work with any generalized form supported by DocAI.
Objectives
In this lab, you will learn how to perform the following tasks:
- Extract data from a scanned form using the Document AI Form Parser
- Extract key/value pairs from a form using the Document AI Form Parser
- Extract and export CSV data from a form using the Document AI Form Parser
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Enable the Document AI API
Before you can begin using Document AI, you must enable the API.
-
Open Cloud Shell by clicking the Activate Cloud Shell button at the top of the console.
-
In Cloud Shell, run the following commands to enable the API for Document AI.
You should see something like this:
You will also need to install Pandas, an Open Source Data Analysis library for Python.
- Run the following command to install Pandas.
- Run the following command to install the Python client libraries for Document AI.
You should see something like this:
Now, you're ready to use the Document AI API!
Click Check my progress to verify the objective.
Task 2. Create a Form Parser processor
You must first create a Form Parser processor instance to use in the Document AI Platform for this tutorial.
- In the Cloud console, open the Navigation menu (), click View All Products > Artificial Intelligence > Document AI.
- Click Explore Processors, and inside Form Parser, click Create Processor.
-
Give it the name
lab-form-parser
and select the closest region on the list. -
Click Create to create your processor
-
Copy your Processor ID. You must use this in your code later.
Click Check my progress to verify the objective.
Test the processor in the Cloud Console
You can test out your processor in the console by uploading a document.
- Right click the image below, and select Save Image As to download the sample form.
- On the Processor Details page, click Upload Test Document. Select the form you just downloaded.
Your Form Parser processor will process the document and return the parsed form data. It should look something like this:
Task 3. Download the sample form
In this section, you will download a sample document which contains a simple medical intake form.
- Run the following command to download the sample form to your Cloud Shell.
- Confirm the file is downloaded to your Cloud Shell using the below command:
Task 4. Extract form key/value pairs
In this section, you will use the online processing API to call the Form Parser processor you created previously. Then, you will extract the key value pairs found in the document.
Online processing is for sending a single document and waiting for the response. You can also use batch processing if you want to send multiple files or if the file size exceeds the online processing maximum pages.
The code for making a process request is identical for every processor type aside from the Processor ID. The Document response object contains
a list of pages from the input document. Each page
object contains a list of form fields and their locations in the text.
The following code iterates through each page and extracts each key, value and confidence score. This is structured data that can more easily stored in databases or used in other applications.
- In Cloud Shell, create a file called
form_parser.py
and paste the following code into it:
- Replace
YOUR_PROJECT_ID
,YOUR_PROJECT_LOCATION
,YOUR_PROCESSOR_ID
, and theFILE_PATH
with appropriate values for your environment.
FILE_PATH
is the name of the file you downloaded to Cloud Shell in the previous step. If you didn't rename the file, it should be intake-form.pdf
, which you will need to update in the code.
- Run the following command to execute the script:
You should see the following output:
Task 5. Parse tables
The Form Parser is also able to extract data from tables within documents. In this section, you will download a new sample document and extract data from the table. Since you are loading the data into Pandas, this data can be output to a CSV file and many other formats with a single method call.
Download the Sample Form with Tables
We have a sample document which contains a sample form and a table.
- Run the following command to download the sample form to your Cloud Shell.
- Confirm the file is downloaded to your Cloud Shell using the below command:
Extract Table Data
The processing request for table data is exactly the same as for extracting key-value pairs. The difference is which fields you extract the data from in the response. Table data is stored in the pages[].tables[]
field.
This example extracts information about from the table header rows and body rows for each table and page, then prints out the table and saves the table as a CSV file.
- Create a file called
table_parsing.py
and paste the following code into it:
- Replace
YOUR_PROJECT_ID
,YOUR_PROJECT_LOCATION
,YOUR_PROCESSOR_ID
, and theFILE_PATH
with appropriate values for your environment.
FILE_PATH
is the name of the file you downloaded to Cloud Shell in the previous step. If you didn't rename the file, it should be form_with_tables.pdf
, which is the default value and doesn't need to be changed.
- Run the following command to execute the script:
You should see the following output:
You should also have a new CSV file in the directory you are running the code from.
- Run the following command to list the files in your current working directory:
You should see the following output:
Congratulations!
Congratulations, in this lab you've successfully used the Document AI API to extract data from a handwritten form. You also learned how to use the Document AI Python client library to extract key-value pairs from a form and how to extract tabular data from a form with tables.
Next steps / Learn more
Check out the following resources to learn more about Document AI and the Python Client Library:
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated November 13, 2024
Lab Last Tested November 13, 2024
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.