引言
图片中的文字提取是办公自动化中的高频需求——扫描件转文字、截图识别、发票信息提取。python 中有两个主流的 ocr 库,这篇从安装到实战完整讲解。
一、方案选择:paddleocr vs tesseract
| 对比 | paddleocr | tesseract |
|---|---|---|
| 中文识别 | ⭐⭐⭐⭐⭐ 优秀 | ⭐⭐⭐ 一般 |
| 安装难度 | ⭐⭐(稍大) | ⭐(简单) |
| 识别速度 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| 准确率 | 95%+ | 80%-90% |
| 版面分析 | ✅ 支持 | ❌ 不支持 |
建议: 中文场景无脑选 paddleocr,英文场景两个都可以。
二、paddleocr
pip install paddlepaddle paddleocr
from paddleocr import paddleocr
ocr = paddleocr(use_angle_cls=true, lang='ch')
result = ocr.ocr('invoice.jpg')
for line in result[0]:
text = line[1][0]
confidence = line[1][1]
print(f'{text} (置信度: {confidence:.2%})')
三、tesseract
pip install pytesseract # 还需要安装 tesseract-ocr 引擎
import pytesseract
from pil import image
text = pytesseract.image_to_string(
image.open('text.png'),
lang='chi_sim+eng'
)
print(text)
四、发票识别案例
from paddleocr import paddleocr
import re
ocr = paddleocr(use_angle_cls=true, lang='ch')
result = ocr.ocr('invoice.jpg')
texts = [line[1][0] for line in result[0]]
full_text = ''.join(texts)
# 提取关键信息
invoice_no = re.search(r'发票号码[::](\d+)', full_text)
total = re.search(r'价税合计[::]?¥?([\d,]+\.\d{2})', full_text)
print(f'发票号码: {invoice_no.group(1) if invoice_no else "未识别"}')
print(f'价税合计: {total.group(0) if total else "未识别"}')
到此这篇关于python实现图片ocr文字识别的两种方案及实战对比的文章就介绍到这了,更多相关python图片ocr文字识别内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论