图片信息抽取
图片信息抽取服务提供智能信息抽取能力,可从合同、票据与表单中自动识别并提取金额、日期、主体等关键字段,支持自定义字段配置,提升数据处理效率与准确性。
根据图片提取信息
根据详尽的文本描述(prompt),提取指定JSON格式的结构化信息。
Python
import requests
import os
import mimetypes
url = "https://maas-api.uat.hivoice.cn/v1/ocr/image/extract"
api_key = os.environ.get("API_KEY")
headers = {"Authorization": f"Bearer {api_key}"}
IMAGE_PATH = "invoice.png"
PROMPT = """请提取图片中的信息,严格按 JSON 格式输出。
Schema 如下:
{
"invoice_no": "发票号码",
"date": "开票日期",
"items": [{"name": "项目名称", "total": "金额"}]
}"""
payload = {
"model": "u1-ocr-extract",
"prompt": PROMPT
}
with open(IMAGE_PATH, "rb") as image_file:
files = {
"image": (
os.path.basename(IMAGE_PATH),
image_file,
mimetypes.guess_type(IMAGE_PATH)[0]
)
}
response = requests.post(url, headers=headers, data=payload, files=files)
result = response.json()
print(f"{result['content']}")