1. บทนำ
ในโค้ดแล็บนี้ คุณจะได้เรียนรู้วิธีใช้โปรเซสเซอร์เฉพาะของ Document AI เพื่อจัดประเภทและแยกวิเคราะห์เอกสารเฉพาะด้วย Python สำหรับการจัดประเภทและการแยก เราจะใช้ไฟล์ PDF ตัวอย่างที่มีใบแจ้งหนี้ ใบเสร็จ และใบแจ้งยอดค่าสาธารณูปโภค จากนั้นสำหรับการแยกวิเคราะห์และการแยกเอนทิตี เราจะใช้ใบแจ้งหนี้เป็นตัวอย่าง
กระบวนการและโค้ดตัวอย่างนี้จะใช้ได้กับเอกสารเฉพาะทางที่ Document AI รองรับ
ข้อกำหนดเบื้องต้น
Codelab นี้สร้างขึ้นจากเนื้อหาที่นำเสนอใน Codelab อื่นๆ ของ Document AI
เราขอแนะนำให้คุณทำ Codelab ต่อไปนี้ให้เสร็จก่อนดำเนินการต่อ
- การรู้จำอักขระด้วยภาพ (OCR) ด้วย Document AI และ Python
- การแยกวิเคราะห์แบบฟอร์มด้วย Document AI (Python)
สิ่งที่คุณจะได้เรียนรู้
- วิธีจัดประเภทและระบุจุดแยกสำหรับเอกสารเฉพาะทาง
- วิธีแยกเอนทิตีที่กำหนดสคีมาโดยใช้โปรเซสเซอร์เฉพาะ
สิ่งที่คุณต้องมี
2. การเริ่มตั้งค่า
Codelab นี้ถือว่าคุณได้ทำตามขั้นตอนการตั้งค่า Document AI ที่ระบุไว้ใน Codelab เบื้องต้นแล้ว
โปรดทำตามขั้นตอนต่อไปนี้ก่อนดำเนินการต่อ
นอกจากนี้ คุณจะต้องติดตั้ง Pandas ซึ่งเป็นไลบรารีการวิเคราะห์ข้อมูลยอดนิยมสำหรับ Python ด้วย
pip3 install --upgrade pandas
3. สร้างโปรเซสเซอร์เฉพาะทาง
คุณต้องสร้างอินสแตนซ์ของโปรเซสเซอร์ที่จะใช้สำหรับบทแนะนำนี้ก่อน
- ในคอนโซล ให้ไปที่ภาพรวมแพลตฟอร์ม Document AI
- คลิกสร้างโปรเซสเซอร์ เลื่อนลงไปที่เฉพาะทาง แล้วเลือกตัวแยกเอกสารการจัดซื้อ
- ตั้งชื่อว่า "codelab-procurement-splitter" (หรือชื่ออื่นที่คุณจะจำได้) แล้วเลือกภูมิภาคที่ใกล้ที่สุดในรายการ
- คลิกสร้างเพื่อสร้างโปรเซสเซอร์
- คัดลอกรหัสโปรเซสเซอร์ คุณต้องใช้รหัสนี้ในโค้ดในภายหลัง
- ทำขั้นตอนที่ 2-6 ซ้ำโดยใช้ Invoice Parser (ซึ่งคุณตั้งชื่อว่า "codelab-invoice-parser") ได้
ทดสอบโปรเซสเซอร์ในคอนโซล
คุณสามารถทดสอบตัวแยกวิเคราะห์ใบแจ้งหนี้ในคอนโซลได้โดยการอัปโหลดเอกสาร
คลิกอัปโหลดเอกสาร แล้วเลือกใบแจ้งหนี้ที่จะแยกวิเคราะห์ คุณสามารถดาวน์โหลดและใช้ใบแจ้งหนี้ตัวอย่างนี้ได้หากไม่มีใบแจ้งหนี้ที่พร้อมใช้งาน

เอาต์พุตควรมีลักษณะดังนี้

4. ดาวน์โหลดเอกสารตัวอย่าง
เรามีเอกสารตัวอย่าง 2-3 รายการให้ใช้ใน Lab นี้
คุณดาวน์โหลด PDF ได้โดยใช้ลิงก์ต่อไปนี้ จากนั้นอัปโหลดไปยังอินสแตนซ์ Cloud Shell
หรือจะดาวน์โหลดจาก Bucket ของ Cloud Storage สาธารณะของเราโดยใช้ gsutil ก็ได้
gsutil cp gs://cloud-samples-data/documentai/codelabs/specialized-processors/procurement_multi_document.pdf .
gsutil cp gs://cloud-samples-data/documentai/codelabs/specialized-processors/google_invoice.pdf .
5. จัดประเภทและแยกเอกสาร
ในขั้นตอนนี้ คุณจะใช้ API การประมวลผลออนไลน์เพื่อจัดประเภทและตรวจหาจุดแยกเชิงตรรกะสำหรับเอกสารหลายหน้า
นอกจากนี้ คุณยังใช้ Batch Processing API ได้หากต้องการส่งหลายไฟล์หรือหากไฟล์มีขนาดเกินจำนวนหน้าสูงสุดสำหรับการประมวลผลออนไลน์ คุณดูวิธีดำเนินการนี้ได้ใน Codelab OCR ของ Document AI
โค้ดสำหรับการส่งคำขอ API จะเหมือนกันสำหรับตัวประมวลผลทั่วไป ยกเว้นรหัสตัวประมวลผล
เครื่องมือแยก/เครื่องมือจัดประเภทการจัดซื้อจัดจ้าง
สร้างไฟล์ชื่อ classification.py แล้วใช้โค้ดด้านล่าง
แทนที่ PROCUREMENT_SPLITTER_ID ด้วยรหัสของเครื่องมือประมวลผลการแยกการจัดซื้อที่คุณสร้างไว้ก่อนหน้านี้ แทนที่ YOUR_PROJECT_ID และ YOUR_PROJECT_LOCATION ด้วยรหัสโปรเจ็กต์ Cloud และสถานที่ตั้งของโปรเซสเซอร์ตามลำดับ
classification.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 file:
file_content = file.read()
# Load Binary Data into Document AI RawDocument Object
raw_document = documentai.RawDocument(content=file_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
PROJECT_ID = "YOUR_PROJECT_ID"
LOCATION = "YOUR_PROJECT_LOCATION" # Format is 'us' or 'eu'
PROCESSOR_ID = "PROCUREMENT_SPLITTER_ID" # Create processor in Cloud Console
# The local file in your current working directory
FILE_PATH = "procurement_multi_document.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,
)
print("Document processing complete.")
types = []
confidence = []
pages = []
# Each Document.entity is a classification
for entity in document.entities:
classification = entity.type_
types.append(classification)
confidence.append(f"{entity.confidence:.0%}")
# entity.page_ref contains the pages that match the classification
pages_list = []
for page_ref in entity.page_anchor.page_refs:
pages_list.append(page_ref.page)
pages.append(pages_list)
# Create a Pandas Dataframe to print the values in tabular format.
df = pd.DataFrame({"Classification": types, "Confidence": confidence, "Pages": pages})
print(df)
เอาต์พุตควรมีลักษณะดังนี้
$ python3 classification.py
Document processing complete.
Classification Confidence Pages
0 invoice_statement 100% [0]
1 receipt_statement 98% [1]
2 other 81% [2]
3 utility_statement 100% [3]
4 restaurant_statement 100% [4]
โปรดทราบว่าตัวแยก/ตัวแยกประเภทการจัดซื้อระบุประเภทเอกสารในหน้า 0-1 และ 3-4 ได้อย่างถูกต้อง
หน้า 2 มีแบบฟอร์มการรับผู้ป่วยทั่วไป ดังนั้นเครื่องมือคัดแยกจึงระบุได้อย่างถูกต้องว่าเป็น other
6. ดึงข้อมูลเอนทิตี
ตอนนี้คุณสามารถดึงข้อมูลเอนทิตีที่จัดรูปแบบสคีมาจากไฟล์ได้แล้ว ซึ่งรวมถึงคะแนนความเชื่อมั่น พร็อพเพอร์ตี้ และค่าที่ทำให้เป็นมาตรฐาน
โค้ดสำหรับการส่งคำขอ API จะเหมือนกับขั้นตอนก่อนหน้า และสามารถทำได้ด้วยคำขอออนไลน์หรือคำขอแบบเป็นกลุ่ม
เราจะเข้าถึงข้อมูลต่อไปนี้จากนิติบุคคล
- ประเภทเอนทิตี
- (เช่น
invoice_date,receiver_name,total_amount)
- (เช่น
- ค่าดิบ
- ค่าข้อมูลตามที่แสดงในไฟล์เอกสารต้นฉบับ
- ค่าที่ปรับให้เป็นมาตรฐาน
- ค่าข้อมูลในรูปแบบที่ปรับให้เป็นมาตรฐานและรูปแบบมาตรฐาน (หากมี)
- นอกจากนี้ยังอาจรวมถึงการเพิ่มประสิทธิภาพจากกราฟความรู้ขององค์กร
- ค่าความเชื่อมั่น
- โมเดล "มั่นใจ" เพียงใดว่าค่าต่างๆ ถูกต้อง
เอนทิตีบางประเภท เช่น line_item ยังมีพร็อพเพอร์ตี้ซึ่งเป็นเอนทิตีที่ซ้อนกัน เช่น line_item/unit_price และ line_item/description ด้วย
ตัวอย่างนี้จะทำให้โครงสร้างที่ซ้อนกันเรียบขึ้นเพื่อให้ดูได้ง่าย
เครื่องมือแยกวิเคราะห์ใบแจ้งหนี้
สร้างไฟล์ชื่อ extraction.py แล้วใช้โค้ดด้านล่าง
แทนที่ INVOICE_PARSER_ID ด้วยรหัสของโปรเซสเซอร์ตัวแยกวิเคราะห์ใบแจ้งหนี้ที่คุณสร้างไว้ก่อนหน้านี้ และใช้ไฟล์ google_invoice.pdf
extraction.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 file:
file_content = file.read()
# Load Binary Data into Document AI RawDocument Object
raw_document = documentai.RawDocument(content=file_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
PROJECT_ID = "YOUR_PROJECT_ID"
LOCATION = "YOUR_PROJECT_LOCATION" # Format is 'us' or 'eu'
PROCESSOR_ID = "INVOICE_PARSER_ID" # Create processor in Cloud Console
# The local file in your current working directory
FILE_PATH = "google_invoice.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,
)
types = []
raw_values = []
normalized_values = []
confidence = []
# Grab each key/value pair and their corresponding confidence scores.
for entity in document.entities:
types.append(entity.type_)
raw_values.append(entity.mention_text)
normalized_values.append(entity.normalized_value.text)
confidence.append(f"{entity.confidence:.0%}")
# Get Properties (Sub-Entities) with confidence scores
for prop in entity.properties:
types.append(prop.type_)
raw_values.append(prop.mention_text)
normalized_values.append(prop.normalized_value.text)
confidence.append(f"{prop.confidence:.0%}")
# Create a Pandas Dataframe to print the values in tabular format.
df = pd.DataFrame(
{
"Type": types,
"Raw Value": raw_values,
"Normalized Value": normalized_values,
"Confidence": confidence,
}
)
print(df)
เอาต์พุตควรมีลักษณะดังนี้
$ python3 extraction.py
Type Raw Value Normalized Value Confidence
0 vat $1,767.97 100%
1 vat/tax_amount $1,767.97 1767.97 USD 0%
2 invoice_date Sep 24, 2019 2019-09-24 99%
3 due_date Sep 30, 2019 2019-09-30 99%
4 total_amount 19,647.68 19647.68 97%
5 total_tax_amount $1,767.97 1767.97 USD 92%
6 net_amount 22,379.39 22379.39 91%
7 receiver_name Jane Smith, 83%
8 invoice_id 23413561D 67%
9 receiver_address 1600 Amphitheatre Pkway\nMountain View, CA 94043 66%
10 freight_amount $199.99 199.99 USD 56%
11 currency $ USD 53%
12 supplier_name John Smith 19%
13 purchase_order 23413561D 1%
14 receiver_tax_id 23413561D 0%
15 supplier_iban 23413561D 0%
16 line_item 9.99 12 12 ft HDMI cable 119.88 100%
17 line_item/unit_price 9.99 9.99 90%
18 line_item/quantity 12 12 77%
19 line_item/description 12 ft HDMI cable 39%
20 line_item/amount 119.88 119.88 92%
21 line_item 12 399.99 27" Computer Monitor 4,799.88 100%
22 line_item/quantity 12 12 80%
23 line_item/unit_price 399.99 399.99 91%
24 line_item/description 27" Computer Monitor 15%
25 line_item/amount 4,799.88 4799.88 94%
26 line_item Ergonomic Keyboard 12 59.99 719.88 100%
27 line_item/description Ergonomic Keyboard 32%
28 line_item/quantity 12 12 76%
29 line_item/unit_price 59.99 59.99 92%
30 line_item/amount 719.88 719.88 94%
31 line_item Optical mouse 12 19.99 239.88 100%
32 line_item/description Optical mouse 26%
33 line_item/quantity 12 12 78%
34 line_item/unit_price 19.99 19.99 91%
35 line_item/amount 239.88 239.88 94%
36 line_item Laptop 12 1,299.99 15,599.88 100%
37 line_item/description Laptop 83%
38 line_item/quantity 12 12 76%
39 line_item/unit_price 1,299.99 1299.99 90%
40 line_item/amount 15,599.88 15599.88 94%
41 line_item Misc processing fees 899.99 899.99 1 100%
42 line_item/description Misc processing fees 22%
43 line_item/unit_price 899.99 899.99 91%
44 line_item/amount 899.99 899.99 94%
45 line_item/quantity 1 1 63%
7. ไม่บังคับ: ลองใช้โปรเซสเซอร์เฉพาะทางอื่นๆ
คุณใช้ Document AI สำหรับการจัดซื้อเพื่อจัดประเภทเอกสารและแยกวิเคราะห์ใบแจ้งหนี้เรียบร้อยแล้ว นอกจากนี้ Document AI ยังรองรับโซลูชันเฉพาะทางอื่นๆ ที่แสดงไว้ที่นี่ด้วย
คุณสามารถทำตามขั้นตอนเดียวกันและใช้รหัสเดียวกันเพื่อจัดการโปรเซสเซอร์เฉพาะทาง
หากต้องการลองใช้โซลูชันเฉพาะทางอื่นๆ คุณสามารถเรียกใช้ Lab อีกครั้งด้วยโปรเซสเซอร์ประเภทอื่นๆ และเอกสารตัวอย่างเฉพาะทาง
เอกสารตัวอย่าง
ตัวอย่างเอกสารที่คุณใช้ลองใช้โปรเซสเซอร์เฉพาะทางอื่นๆ ได้มีดังนี้
Solution | ประเภทโปรเซสเซอร์ | เอกสาร |
Identity | ||
บริการสินเชื่อรถยนต์ | ||
บริการสินเชื่อรถยนต์ | ||
สัญญา |
คุณดูเอกสารตัวอย่างและเอาต์พุตของโปรเซสเซอร์อื่นๆ ได้ในเอกสารประกอบ
8. ขอแสดงความยินดี
ขอแสดงความยินดี คุณใช้ Document AI เพื่อจัดประเภทและดึงข้อมูลจากเอกสารเฉพาะทางได้สำเร็จแล้ว เราขอแนะนำให้คุณทดลองใช้เอกสารประเภทอื่นๆ ที่เฉพาะเจาะจง
การล้างข้อมูล
โปรดดำเนินการดังนี้เพื่อเลี่ยงไม่ให้เกิดการเรียกเก็บเงินกับบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในบทแนะนำนี้
- ใน Cloud Console ให้ไปที่หน้าจัดการทรัพยากร
- เลือกโปรเจ็กต์ในรายการโปรเจ็กต์ แล้วคลิกลบ
- ในกล่องโต้ตอบ ให้พิมพ์รหัสโปรเจ็กต์ แล้วคลิกปิดเพื่อลบโปรเจ็กต์
ดูข้อมูลเพิ่มเติม
เรียนรู้เพิ่มเติมเกี่ยวกับ Document AI ด้วย Codelab ที่เกี่ยวข้องต่อไปนี้
- การจัดการโปรเซสเซอร์ของ Document AI ด้วย Python
- Document AI: Human in the Loop
- Document AI Workbench: การฝึกเพิ่มเติม
- Document AI Workbench: โปรเซสเซอร์ที่กำหนดเอง
แหล่งข้อมูล
- อนาคตของเอกสาร - เพลย์ลิสต์ YouTube
- เอกสารประกอบเกี่ยวกับ Document AI
- ไลบรารีของไคลเอ็นต์ Python สำหรับ Document AI
- ตัวอย่าง Document AI
ใบอนุญาต
ผลงานนี้ได้รับอนุญาตภายใต้สัญญาอนุญาตครีเอทีฟคอมมอนส์สำหรับยอมรับสิทธิของผู้สร้าง (Creative Commons Attribution License) 2.0 แบบทั่วไป