企业里 80% 的数据是非结构化的:合同扫描件、产品图片、会议截图、PDF 文档……这些数据如果靠人工处理,成本高到离谱。本文实战用多模态大模型 + OCR 搭建自动化处理流水线,把处理效率提升 20 倍。
多模态大模型能干什么?
传统 AI 的局限:OCR 只能识别文字,分类模型只能做单一任务,遇到复杂文档(图文混排、表格、手写)就歇菜。
多模态大模型(GPT-4V、Qwen-VL、Claude 3)的优势:
| 能力 | 传统 OCR | 多模态大模型 |
|---|---|---|
| 识别印刷文字 | ✅ | ✅ |
| 识别手写 | ❌ 准确率低 | ✅ |
| 理解表格结构 | ❌ | ✅ 直接输出 Markdown 表格 |
| 图文混排理解 | ❌ | ✅ |
| 文档问答 | ❌ | ✅ |
| 发票/合同关键信息提取 | 需定制训练 | ✅ 零样本 |
实战一:用 Qwen-VL 做图片内容理解
阿里云的通义千问 VL 模型对中文支持极好,而且 API 价格只有 GPT-4V 的 1/10。
安装依赖
pip install dashscope pillow openai
代码:批量处理产品图片并提取信息
import os
from dashscope import MultiModalConversation
# 配置 API Key(阿里云 DashScope)
os.environ["DASHSCOPE_API_KEY"] = "sk-xxxxxx"
def analyze_product_image(image_path: str) -> dict:
"""
用 Qwen-VL 分析产品图片,提取结构化信息
"""
messages = [
{
"role": "user",
"content": [
{"image": image_path},
{"text": """
请分析这张产品图片,按以下格式输出 JSON:
{
"product_name": "产品名称",
"brand": "品牌",
"price_visible": true/false,
"price": "价格(如果能看到)",
"key_features": ["特征1", "特征2", ...],
"text_in_image": "图片中所有文字内容"
}
只输出 JSON,不要有其他内容。
"""}
]
}
]
response = MultiModalConversation.call(
model="qwen-vl-max", # 最强多模态模型
messages=messages,
result_format="message"
)
import json
result = json.loads(response["output"]["choices"][0]["message"]["content"][0]["text"])
return result
# 批量处理一个文件夹里的产品图
from pathlib import Path
image_dir = Path("./product_images/")
results = []
for img_file in image_dir.glob("*.jpg"):
try:
info = analyze_product_image(str(img_file))
info["file_name"] = img_file.name
results.append(info)
print(f"✅ 处理完成:{img_file.name}")
except Exception as e:
print(f"❌ 处理失败 {img_file.name}:{e}")
# 保存结果
import pandas as pd
df = pd.DataFrame(results)
df.to_excel("product_analysis.xlsx", index=False)
print(f"📊 共处理 {len(results)} 张图片,结果已保存")
实测效果:
- 处理 500 张产品图,人工需要 8 小时,脚本 12 分钟跑完
- 信息提取准确率 92%(主要是手写价格识别偶尔出错)
实战二:发票/合同 OCR + 大模型结构化提取
场景:财务部门每天收到几百张发票截图/扫描件,需要录入系统。
方案架构
发票图片
│
▼
OCR API(腾讯云/阿里云)
│ 原始文本
▼
多模态大模型(结构化提取)
│ JSON 结构化数据
▼
业务系统 API(自动录入)
代码:OCR + 大模型组合拳
from tencentcloud.common import credential
from tencentcloud.ocr.v20181119 import ocr_client, models as ocr_models
import json
from openai import OpenAI
# 1. 用腾讯云 OCR 识别发票文字
def ocr_invoice(image_path: str) -> str:
cred = credential.Credential("AKIDxxx", "SKxxx")
client = ocr_client.OcrClient(cred, "ap-guangzhou")
with open(image_path, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode("utf-8")
req = ocr_models.RecognizeEncryptedIDCardOCRRequest()
req.ImageBase64 = image_base64
# 用增值税发票 OCR(专门优化的模型)
req = ocr_models.VatInvoiceOCRRequest()
req.ImageBase64 = image_base64
resp = client.VatInvoiceOCR(req)
return resp.__str__() # 返回识别出的结构化文本
# 2. 用大模型做二次结构化(OCR 输出 → 标准 JSON)
client = OpenAI(
api_key="sk-xxxx", # 通义千问兼容 OpenAI 格式
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
def extract_invoice_fields(ocr_text: str) -> dict:
prompt = f"""
以下是增值税发票的 OCR 识别结果,请提取以下字段,输出严格 JSON:
- invoice_code: 发票代码
- invoice_number: 发票号码
- date: 开票日期(YYYY-MM-DD格式)
- seller_name: 销售方名称
- seller_tax_id: 销售方税号
- total_amount: 合计金额(不含税)
- total_tax: 税额
- total_with_tax: 价税合计
OCR 识别结果:
{ocr_text}
只输出 JSON,不要解释。
"""
response = client.chat.completions.create(
model="qwen-turbo", # 结构化提取用 turbo 足够,便宜
messages=[{"role": "user", "content": prompt}],
temperature=0.1 # 低温度,保证输出稳定
)
return json.loads(response.choices[0].message.content)
# 完整流水线
import base64
from pathlib import Path
invoice_dir = Path("./invoices/")
records = []
for img in invoice_dir.glob("*.png"):
print(f"处理:{img.name}")
ocr_result = ocr_invoice(str(img))
structured = extract_invoice_fields(ocr_result)
structured["file_name"] = img.name
records.append(structured)
# 批量写入数据库 / Excel
import pandas as pd
df = pd.DataFrame(records)
df.to_sql("invoices", engine, if_exists="append", index=False)
print(f"✅ 批量录入完成,共 {len(records)} 张发票")
实战三:PDF 文档解析(图文混排)
企业里最多的非结构化数据是 PDF:产品手册、技术文档、合同。用多模态大模型可以直接"看懂"PDF。
用 PyMuPDF 把 PDF 转成图片,再调大模型
import fitz # PyMuPDF
from pathlib import Path
def pdf_to_images(pdf_path: str, output_dir: str, dpi: int = 200):
"""把 PDF 每一页转成图片"""
doc = fitz.open(pdf_path)
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
image_paths = []
for page_num in range(len(doc)):
page = doc[page_num]
mat = fitz.Matrix(dpi/72, dpi/72)
pix = page.get_pixmap(matrix=mat)
img_path = output_path / f"page_{page_num+1}.png"
pix.save(str(img_path))
image_paths.append(str(img_path))
return image_paths
# 对整个 PDF 进行问答
def ask_pdf(pdf_path: str, question: str) -> str:
# 1. 转图片
image_paths = pdf_to_images(pdf_path, "./tmp_pdf_pages/")
# 2. 把前 5 页图片发给多模态模型(页数多的话要做 RAG)
from dashscope import MultiModalConversation
content = [{"text": f"请基于以下文档图片回答问题:{question}"}]
for img_path in image_paths[:5]: # 只处理前5页(演示用)
content.append({"image": img_path})
messages = [{"role": "user", "content": content}]
response = MultiModalConversation.call(
model="qwen-vl-max",
messages=messages
)
return response["output"]["choices"][0]["message"]["content"][0]["text"]
# 使用示例
answer = ask_pdf("./产品手册.pdf", "这款产品支持哪些 API 接口?")
print(answer)
成本对比:人工 vs AI 自动化
| 指标 | 人工处理 | AI 自动化 | 提升倍数 |
|---|---|---|---|
| 发票录入速度 | 3 分钟/张 | 2 秒/张 | 90× |
| 产品图信息提取 | 100 张/天/人 | 500 张/分钟 | 240× |
| 错误率 | 约 3% | 约 1% | 准确更高 |
| 成本(以1万张/月计) | ¥15,000(人力) | ¥800(API调用) | 节省 95% |
生产环境注意事项
- 图片预处理:先压缩图片(1280px 宽足矣),能省 70% 的 token 费用
- 并发控制:大模型 API 有 QPS 限制,用
asyncio+ 信号量控制并发 - 异常处理:图片识别失败要有重试机制(最多 3 次)
- 数据安全:发票/合同等敏感图片,建议用私有化部署的 Qwen 或 GPT-4 本地版
import asyncio
from asyncio import Semaphore
async def process_with_concurrency(images: list, max_concurrent: int = 5):
sem = Semaphore(max_concurrent)
async def process_one(img_path):
async with sem:
# 调用大模型 API(异步版本)
result = await analyze_product_image_async(img_path)
return result
tasks = [process_one(img) for img in images]
return await asyncio.gather(*tasks, return_exceptions=True)
小结
多模态大模型 + OCR 的组合,是企业非结构化数据处理的杀手锏方案:
- OCR 负责「看到文字」,大模型负责「理解内容」
- Qwen-VL 中文场景首选,价格友好
- 处理流水线:图片/PDF → OCR → 大模型结构化 → 业务系统录入
- 实际落地时要注意并发控制、图片预处理和异常处理
👤 作者简介
一枚在大中原腹地(河南)卖公有云的从业者,主营腾讯云/阿里云/火山云,曾踩坑无数,现专注AI大模型应用落地。关注公众号「公有云cloud」,围观AI前沿动态~