1. ์๊ฐ
์ด Codelab์์๋ Document AI ์์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ Python์ผ๋ก ํ๊ธฐ ์์์ ํ์ฑํ๋ ๋ฐฉ๋ฒ์ ์์๋ด ๋๋ค.
์ฌ๊ธฐ์๋ ๊ฐ๋จํ ์๋ฃ ์ ์ ์์์ ์๋ก ์ฌ์ฉํ์ง๋ง, ์ด ์ ์ฐจ๋ DocAI์์ ์ง์ํ๋ ์ผ๋ฐํ๋ ๋ชจ๋ ์์์์ ์๋ํฉ๋๋ค.
๊ธฐ๋ณธ ์๊ฑด
์ด Codelab์ ๋ค๋ฅธ Document AI Codelabs์์ ๋ค๋ฃฌ ์ฝํ ์ธ ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค.
๋ค์ Codelab์ ๋จผ์ ์๋ฃํ ํ์ ์งํํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
ํ์ตํ ๋ด์ฉ
- Document AI ์์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ ์ค์บํ ์์์์ ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ๊ณ ์ถ์ถํ๋ ๋ฐฉ๋ฒ
ํ์ํ ํญ๋ชฉ
์ค๋ฌธ์กฐ์ฌ
์ด ํํ ๋ฆฌ์ผ์ ์ด๋ป๊ฒ ์ฌ์ฉํ์ค ๊ณํ์ธ๊ฐ์?
๊ทํ์ Python ์ฌ์ฉ ๊ฒฝํ์ด ์ด๋ค์ง ํ๊ฐํด ์ฃผ์ธ์.
๊ทํ์ Google Cloud ์๋น์ค ์ฌ์ฉ ๊ฒฝํ์ ํ๊ฐํด ์ฃผ์ธ์.
<ph type="x-smartling-placeholder">2. ์ค์ ๋ฐ ์๊ตฌ์ฌํญ
์ด Codelab์์๋ Document AI OCR Codelab์ ๋์ด๋ Document AI ์ค์ ๋จ๊ณ๋ฅผ ์๋ฃํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
๋ค์ ๋จ๊ณ๋ฅผ ์๋ฃํ ํ์ ์งํํ์ธ์.
Python์ฉ ์คํ์์ค ๋ฐ์ดํฐ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Pandas๋ ์ค์นํด์ผ ํฉ๋๋ค.
pip3 install --upgrade pandas
3. ์์ ํ์ ํ๋ก์ธ์ ๋ง๋ค๊ธฐ
๋จผ์ ์ด ํํ ๋ฆฌ์ผ์ Document AI Platform์์ ์ฌ์ฉํ ์์ ํ์ ํ๋ก์ธ์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
- ์ฝ์์์ Document AI Platform ๊ฐ์๋ก ์ด๋ํฉ๋๋ค.
- ํ๋ก์ธ์ ๋ง๋ค๊ธฐ๋ฅผ ํด๋ฆญํ๊ณ ์์ ํ์
๋ฅผ ์ ํํฉ๋๋ค.
- ํ๋ก์ธ์ ์ด๋ฆ์ ์ง์ ํ๊ณ ๋ชฉ๋ก์์ ๋ฆฌ์ ์ ์ ํํฉ๋๋ค.
- ๋ง๋ค๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ ํ๋ก์ธ์๋ฅผ ๋ง๋ญ๋๋ค.
- ํ๋ก์ธ์ ID๋ฅผ ๋ณต์ฌํฉ๋๋ค. ๋์ค์ ์ฝ๋์์ ์ด ID๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
Cloud ์ฝ์์์ ํ๋ก์ธ์ ํ ์คํธ
์ฝ์์์ ๋ฌธ์๋ฅผ ์ ๋ก๋ํ์ฌ ํ๋ก์ธ์๋ฅผ ํ ์คํธํ ์ ์์ต๋๋ค. ๋ฌธ์ ์ ๋ก๋๋ฅผ ํด๋ฆญํ๊ณ ํ์ฑํ ์์์ ์ ํํฉ๋๋ค. ์ฌ์ฉํ ์ ์๋ ์ํ ์์์ด ์๋ ๊ฒฝ์ฐ ์ด ์ํ ์์์ ๋ค์ด๋ก๋ํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ด ํ์๋ฉ๋๋ค.
4. ์ํ ์์ ๋ค์ด๋ก๋
๊ฐ๋จํ ์๋ฃ ์ ์ ์์์ด ํฌํจ๋ ์ํ ๋ฌธ์๊ฐ ์ค๋น๋์ด ์์ต๋๋ค.
๋ค์ ๋งํฌ๋ฅผ ์ฌ์ฉํ์ฌ PDF๋ฅผ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ Cloud Shell ์ธ์คํด์ค์ ์ ๋ก๋ํฉ๋๋ค.
๋๋ gsutil
๋ฅผ ์ฌ์ฉํ์ฌ ๊ณต๊ฐ Google Cloud Storage ๋ฒํท์์ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค.
gsutil cp gs://cloud-samples-data/documentai/codelabs/form-parser/intake-form.pdf .
์๋ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ด Cloud Shell์ ๋ค์ด๋ก๋๋์๋์ง ํ์ธํฉ๋๋ค.
ls -ltr intake-form.pdf
5. ์์ ํค/๊ฐ ์ ์ถ์ถ
์ด ๋จ๊ณ์์๋ ์จ๋ผ์ธ ์ฒ๋ฆฌ API๋ฅผ ์ฌ์ฉํ์ฌ ์ด์ ์ ๋ง๋ ์์ ํ์ ํ๋ก์ธ์๋ฅผ ํธ์ถํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ฌธ์์์ ๋ฐ๊ฒฌ๋ ํค-๊ฐ ์์ ์ถ์ถํฉ๋๋ค.
์จ๋ผ์ธ ์ฒ๋ฆฌ๋ ๋จ์ผ ๋ฌธ์๋ฅผ ๋ณด๋ด๊ณ ์๋ต์ ๊ธฐ๋ค๋ฆฌ๋ ๊ฒ์ ๋๋ค. ์ฌ๋ฌ ํ์ผ์ ์ ์กํ๋ ค๋ ๊ฒฝ์ฐ ๋๋ ํ์ผ ํฌ๊ธฐ๊ฐ ์จ๋ผ์ธ ์ฒ๋ฆฌ ์ต๋ ํ์ด์ง ์๋ฅผ ์ด๊ณผํ๋ ๊ฒฝ์ฐ์๋ ์ผ๊ด ์ฒ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. OCR Codelab์์ ์ด ์์ ์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๊ฒํ ํ ์ ์์ต๋๋ค.
ํ๋ก์ธ์ค ์์ฒญ์ ์ํ ์ฝ๋๋ ํ๋ก์ธ์ ID๋ฅผ ์ ์ธํ ๋ชจ๋ ํ๋ก์ธ์ ์ ํ์ ๋์ผํฉ๋๋ค.
Document ์๋ต ๊ฐ์ฒด์๋ ์ ๋ ฅ ๋ฌธ์์ ํ์ด์ง ๋ชฉ๋ก์ด ํฌํจ๋ฉ๋๋ค.
๊ฐ page
๊ฐ์ฒด์๋ ์์ ์
๋ ฅ๋ ๋ชฉ๋ก๊ณผ ํ
์คํธ ๋ด ์์น๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
๋ค์ ์ฝ๋๋ ๊ฐ ํ์ด์ง๋ฅผ ๋ฐ๋ณตํ์ฌ ๊ฐ ํค, ๊ฐ, ์ ๋ขฐ๋ ์ ์๋ฅผ ์ถ์ถํฉ๋๋ค. ์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ ์ฝ๊ฒ ์ ์ฅํ๊ฑฐ๋ ๋ค๋ฅธ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ ์ ์๋ ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ์ ๋๋ค.
form_parser.py
๋ผ๋ ํ์ผ์ ๋ง๋ค๊ณ ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
form_parser.py
import pandas as pd
from google.cloud import documentai_v1 as 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 trim_text(text: str):
"""
Remove extra space characters from text (blank, newline, tab, etc.)
"""
return text.strip().replace("\n", " ")
PROJECT_ID = "YOUR_PROJECT_ID"
LOCATION = "YOUR_PROJECT_LOCATION" # Format is 'us' or 'eu'
PROCESSOR_ID = "FORM_PARSER_ID" # Create processor in Cloud Console
# The local file in your current working directory
FILE_PATH = "intake-form.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,
)
names = []
name_confidence = []
values = []
value_confidence = []
for page in document.pages:
for field in page.form_fields:
# Get the extracted field names
names.append(trim_text(field.field_name.text_anchor.content))
# Confidence - How "sure" the Model is that the text is correct
name_confidence.append(field.field_name.confidence)
values.append(trim_text(field.field_value.text_anchor.content))
value_confidence.append(field.field_value.confidence)
# Create a Pandas Dataframe to print the values in tabular format.
df = pd.DataFrame(
{
"Field Name": names,
"Field Name Confidence": name_confidence,
"Field Value": values,
"Field Value Confidence": value_confidence,
}
)
print(df)
์ด์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ํ ์คํธ๊ฐ ์ถ์ถ๋์ด ์ฝ์์ ์ถ๋ ฅ๋์ด์ผ ํฉ๋๋ค.
์ํ ๋ฌธ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ๋ฉ๋๋ค.
$ python3 form_parser.py Field Name Field Name Confidence Field Value Field Value Confidence 0 Phone #: 0.999982 (906) 917-3486 0.999982 1 Emergency Contact: 0.999972 Eva Walker 0.999972 2 Marital Status: 0.999951 Single 0.999951 3 Gender: 0.999933 F 0.999933 4 Occupation: 0.999914 Software Engineer 0.999914 5 Referred By: 0.999862 None 0.999862 6 Date: 0.999858 9/14/19 0.999858 7 DOB: 0.999716 09/04/1986 0.999716 8 Address: 0.999147 24 Barney Lane 0.999147 9 City: 0.997718 Towaco 0.997718 10 Name: 0.997345 Sally Walker 0.997345 11 State: 0.996944 NJ 0.996944 ...
6. ํ์ฑ ํ ์ด๋ธ
์์ ํ์๋ ๋ฌธ์ ๋ด์ ํ ์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ ์๋ ์์ต๋๋ค. ์ด ๋จ๊ณ์์๋ ์ ์ํ ๋ฌธ์๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ํ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํฉ๋๋ค. ๋ฐ์ดํฐ๋ฅผ Pandas๋ก ๋ก๋ํ๋ฏ๋ก ๋จ์ผ ๋ฉ์๋ ํธ์ถ๋ก ์ด ๋ฐ์ดํฐ๋ฅผ CSV ํ์ผ๊ณผ ๋ค๋ฅธ ์ฌ๋ฌ ํ์์ผ๋ก ์ถ๋ ฅํ ์ ์์ต๋๋ค.
ํ ์ด๋ธ์ด ํฌํจ๋ ์ํ ์์ ๋ค์ด๋ก๋
์ํ ์์๊ณผ ํ๊ฐ ํฌํจ๋ ์ํ ๋ฌธ์๊ฐ ์์ต๋๋ค.
๋ค์ ๋งํฌ๋ฅผ ์ฌ์ฉํ์ฌ PDF๋ฅผ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ Cloud Shell ์ธ์คํด์ค์ ์ ๋ก๋ํฉ๋๋ค.
๋๋ gsutil
๋ฅผ ์ฌ์ฉํ์ฌ ๊ณต๊ฐ Google Cloud Storage ๋ฒํท์์ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค.
gsutil cp gs://cloud-samples-data/documentai/codelabs/form-parser/form_with_tables.pdf .
์๋ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ด Cloud Shell์ ๋ค์ด๋ก๋๋์๋์ง ํ์ธํฉ๋๋ค.
ls -ltr form_with_tables.pdf
ํ ์ด๋ธ ๋ฐ์ดํฐ ์ถ์ถ
ํ
์ด๋ธ ๋ฐ์ดํฐ์ ๋ํ ์ฒ๋ฆฌ ์์ฒญ์ ํค-๊ฐ ์์ ์ถ์ถํ๋ ๊ฒ๊ณผ ์ ํํ ๋์ผํฉ๋๋ค. ์ฐจ์ด์ ์ ์๋ต์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ ํ๋์
๋๋ค. ํ
์ด๋ธ ๋ฐ์ดํฐ๋ pages[].tables[]
ํ๋์ ์ ์ฅ๋ฉ๋๋ค.
์ด ์์์๋ ๊ฐ ํ์ ํ์ด์ง์ ํ ํค๋ ํ๊ณผ ๋ณธ๋ฌธ ํ์์ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ถ์ถํ ๋ค์ ํ๋ฅผ ์ถ๋ ฅํ๊ณ ํ๋ฅผ CSV ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
table_parsing.py
๋ผ๋ ํ์ผ์ ๋ง๋ค๊ณ ์๋ ์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
table_parsing.py
# type: ignore[1]
"""
Uses Document AI online processing to call a form parser processor
Extracts the tables and data in the document.
"""
from os.path import splitext
from typing import List, Sequence
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 = "YOUR_PROJECT_ID"
LOCATION = "YOUR_PROJECT_LOCATION" # Format is 'us' or 'eu'
PROCESSOR_ID = "FORM_PARSER_ID" # Create processor before running sample
# The local file in your current working directory
FILE_PATH = "form_with_tables.pdf"
# Refer to https://cloud.google.com/document-ai/docs/file-types
# 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)
์ด์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ํ ์คํธ๊ฐ ์ถ์ถ๋์ด ์ฝ์์ ์ถ๋ ฅ๋์ด์ผ ํฉ๋๋ค.
์ํ ๋ฌธ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ๋ฉ๋๋ค.
$ python3 table_parsing.py Page 1 - Table 0 Item Description 0 Item 1 Description 1 1 Item 2 Description 2 2 Item 3 Description 3 Page 1 - Table 1 Form Number: 12345678 0 Form Date: 2020/10/01 1 Name: First Last 2 Address: 123 Fake St
๋ํ ์ฝ๋๋ฅผ ์คํํ๋ ๋๋ ํฐ๋ฆฌ์ ๋ ๊ฐ์ ์ CSV ํ์ผ์ด ์์ด์ผ ํฉ๋๋ค.
$ ls form_with_tables_pg1_tb0.csv form_with_tables_pg1_tb1.csv table_parsing.py
7. ์ถํํฉ๋๋ค
์๊ณ ํ์ จ์ต๋๋ค. Document AI API๋ฅผ ์ฌ์ฉํ์ฌ ํ๊ธฐ ์ ๋ ฅ ์์์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ์ต๋๋ค. ๋ค๋ฅธ ์์ ๋ฌธ์๋ฅผ ์ฌ์ฉํด ๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
์ญ์
์ด ํํ ๋ฆฌ์ผ์์ ์ฌ์ฉํ ๋ฆฌ์์ค ๋น์ฉ์ด Google Cloud ๊ณ์ ์ ์ฒญ๊ตฌ๋์ง ์๋๋ก ํ๋ ค๋ฉด ๋ค์ ์๋ด๋ฅผ ๋ฐ๋ฅด์ธ์.
- Cloud ์ฝ์์์ ๋ฆฌ์์ค ๊ด๋ฆฌ ํ์ด์ง๋ก ์ด๋ํฉ๋๋ค.
- ํ๋ก์ ํธ ๋ชฉ๋ก์์ ํด๋น ํ๋ก์ ํธ๋ฅผ ์ ํํ ํ ์ญ์ ๋ฅผ ํด๋ฆญํฉ๋๋ค.
- ๋ํ์์์์ ํ๋ก์ ํธ ID๋ฅผ ์ ๋ ฅํ ํ ์ข ๋ฃ๋ฅผ ํด๋ฆญํ์ฌ ํ๋ก์ ํธ๋ฅผ ์ญ์ ํฉ๋๋ค.
์์ธํ ์์๋ณด๊ธฐ
๋ค์๊ณผ ๊ฐ์ ํ์ Codelab์์ Document AI ํ์ต์ ๊ณ์ํ์ธ์.
- Document AI(Python)๋ฅผ ์ฌ์ฉํ ์ ๋ฌธ ํ๋ก์ธ์
- Python์ผ๋ก Document AI ํ๋ก์ธ์ ๊ด๋ฆฌ
- Document AI: ์ธ๊ฐ ์ฐธ์ฌํ(Human In The Loop)
๋ฆฌ์์ค
๋ผ์ด์ ์ค
์ด ์์ ๋ฌผ์ Creative Commons Attribution 2.0 ์ผ๋ฐ ๋ผ์ด์ ์ค์ ๋ฐ๋ผ ์ฌ์ฉ์ด ํ๊ฐ๋์์ต๋๋ค.