选型与快速跑通(Tesseract / EasyOCR / PaddleOCR)
目标:在本地装好一个 OCR 库,用几行代码把图片变成文字,先建立手感。
OCR 的完整流程通常包含图像预处理、文字检测、字符识别和后处理。第一期先跳过预处理,直接调用封装好的库,感受原始输出。
1. 经典轻量方案:Tesseract(pytesseract)
Tesseract 是老牌开源引擎,pytesseract 只是 Python 封装,需要先安装系统级的 Tesseract 可执行文件。Windows 上安装后如果不在 PATH 中,需要手动指定路径。
python
# pip install pytesseract pillow import pytesseract from PIL import Image # Windows 常见需要显式指定引擎路径 # pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" img = Image.open("test.png") text = pytesseract.image_to_string(img, lang="chi_sim+eng") print(text)注意:lang 参数需要提前安装对应语言包。在 Ubuntu 上可以用
sudo apt install tesseract-ocr-chi-sim安装简体中文数据。
2. 深度学习开箱方案:EasyOCR
如果不想折腾系统级依赖,EasyOCR 是更省事的选择。它基于 PyTorch,安装完 pip 包后模型会自动下载,支持中英文混合识别。
python
# pip install easyocr import easyocr reader = easyocr.Reader(["ch_sim", "en"]) # 首次运行会下载模型 result = reader.readtext("test.png") for bbox, text, score in result: print(text, f"{score:.2f}")EasyOCR 的返回结果包含坐标、文本和置信度,方便后续按位置过滤或排序。
3. 中文场景优先:PaddleOCR
如果主要处理中文文档、票据或表格,PaddleOCR 在中文场景下的准确率通常更有优势。安装时注意先装 PaddlePaddle 再装 PaddleOCR。
python
# pip install paddlepaddle paddleocr from paddleocr import PaddleOCR ocr = PaddleOCR(use_angle_cls=True, lang="ch") result = ocr.ocr("test.png", cls=True) for line in result[0]: print(line[1][0], f"{line[1][1]:.2f}")第一期建议先用同一张图在三个库上都跑一遍,直观感受速度与准确率的差异,再决定后续深入哪个。
github.0voice