Key Extraction · API v3
สูตรสำเร็จ
ตัวอย่างสั้นๆ ที่คัดลอกไปใช้ได้ — ปรับชื่อไฟล์และ API key ตามสภาพแวดล้อม
ใบแจ้งหนี้เป็น JSON + bbox
import time, requestsAPI = "https://backend.aksonocr.com/api/v3/key-extract"headers = {"X-API-Key": "<YOUR_API_KEY>"}with open("invoice.pdf", "rb") as f:file_id = requests.post(f"{API}/files", headers=headers, files={"files": f}).json()["files"][0]["id"]job_id = requests.post(f"{API}/jobs",headers={**headers, "Idempotency-Key": "invoice-md-001"},json={"inputs": [{"file_id": file_id}],"data_schema": {"type": "object","additionalProperties": False,"properties": {"invoice_no": {"type": "string", "description": "เลขที่ใบแจ้งหนี้"},"total": {"type": "number", "description": "ยอดรวม"},"line_items": {"type": "array","description": "รายการสินค้า","items": {"type": "object","additionalProperties": False,"properties": {"description": {"type": "string"},"amount": {"type": "number"},},},},},},"output": {"evidence": "bbox"},},).json()["id"]while True:status = requests.get(f"{API}/jobs/{job_id}", headers=headers).json()["status"]if status in {"completed", "failed", "cancelled", "expired"}:breaktime.sleep(2)result = requests.get(f"{API}/jobs/{job_id}/result", headers=headers).json()print(result["data"])print(result["field_metadata"]["/invoice_no"]["regions"][0]["bbox"])
วาด overlay จาก bbox
คูณค่า 0–1 ด้วยความกว้าง/สูงของหน้าจาก pages[]
from PIL import Image, ImageDraw# image: the page you sent (e.g. the invoice) opened with Pillowimage = Image.open("invoice.jpg").convert("RGB")draw = ImageDraw.Draw(image)width, height = image.sizefor pointer, meta in result["field_metadata"].items():for region in meta["regions"]:x1, y1, x2, y2 = region["bbox"]draw.rectangle([x1 * width, y1 * height, x2 * width, y2 * height],outline="#ec4899",width=3,)image.save("invoice-overlay.jpg")
ดึงเฉพาะหน้า 3–5 ของ PDF
import requestsAPI = "https://backend.aksonocr.com/api/v3/key-extract"headers = {"X-API-Key": "<YOUR_API_KEY>"}with open("book.pdf", "rb") as document:file_id = requests.post(f"{API}/files", headers=headers, files={"files": document}).json()["files"][0]["id"]job = requests.post(f"{API}/jobs",headers={**headers, "Idempotency-Key": "pages-3-5"},json={"inputs": [{"file_id": file_id, "pages": [3, 4, 5]}],"data_schema": {"type": "object","properties": {"title": {"type": "string"}},},"output": {"evidence": "bbox"},},).json()print(job["id"])
ไม่ต้องการ bbox
ส่ง output.evidence: "none" เมื่อต้องการแค่ค่าใน data — regions จะเป็นอาร์เรย์ว่าง
{"inputs": [{ "file_id": "file_01J9..." }],"data_schema": {"type": "object","properties": {"invoice_no": { "type": "string" }}},"output": { "evidence": "none" }}