logo

Python OCR实战:pytesseract与pyddleocr工具对比及代码详解

作者:carzy2025.09.26 19:26浏览量:0

简介:本文详细介绍了Python中两个主流OCR库pytesseract和pyddleocr的实现原理、安装配置及使用方法,通过对比两者在图像处理、文字识别准确率、语言支持等方面的差异,帮助开发者选择最适合的OCR解决方案。

Python实现OCR:pytesseract和pyddleocr(附代码)

引言

OCR(Optical Character Recognition,光学字符识别)技术能够将图片中的文字转换为可编辑的文本格式,广泛应用于文档数字化、自动化办公、车牌识别等领域。在Python生态中,pytesseract和pyddleocr是两个常用的OCR库,前者基于Tesseract引擎,后者是百度开源的深度学习OCR工具。本文将详细介绍两者的安装、配置及使用方法,并通过代码示例展示其实际应用。

一、pytesseract:基于Tesseract的OCR工具

1.1 pytesseract简介

pytesseract是Tesseract OCR引擎的Python封装,Tesseract由Google维护,支持超过100种语言的识别,包括中文、英文等。其核心优势在于开源免费、跨平台(Windows/Linux/macOS)且支持命令行调用。

1.2 安装与配置

安装步骤

  1. 安装Tesseract引擎

    • Windows:下载Tesseract安装包,安装时勾选中文语言包(chi_sim.traineddata)。
    • Linux(Ubuntu):sudo apt install tesseract-ocr tesseract-ocr-chi-sim
    • macOS:brew install tesseract
  2. 安装pytesseract库

    1. pip install pytesseract

配置环境变量

  • Windows需将Tesseract的安装路径(如C:\Program Files\Tesseract-OCR)添加到系统PATH
  • 在代码中指定Tesseract路径(可选):
    1. import pytesseract
    2. pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

1.3 基本使用

示例代码

  1. from PIL import Image
  2. import pytesseract
  3. # 读取图片
  4. image = Image.open("example.png")
  5. # 识别图片中的文字(默认英文)
  6. text = pytesseract.image_to_string(image)
  7. print("英文识别结果:\n", text)
  8. # 指定中文识别
  9. text_chinese = pytesseract.image_to_string(image, lang="chi_sim")
  10. print("中文识别结果:\n", text_chinese)

参数详解

  • lang:指定语言包(如chi_sim为简体中文,eng为英文)。
  • config:调整识别参数,例如:
    1. # 启用PSM(页面分割模式)6,假设图片为统一文本块
    2. custom_config = r'--oem 3 --psm 6'
    3. text = pytesseract.image_to_string(image, config=custom_config)

1.4 优缺点分析

  • 优点:开源免费、支持多语言、适合简单场景。
  • 缺点:对复杂背景、倾斜文字的识别率较低,需手动预处理图像(如二值化、去噪)。

二、pyddleocr:基于深度学习的OCR工具

2.1 pyddleocr简介

pyddleocr是百度开源的PaddleOCR框架的Python封装,基于深度学习模型(CRNN+CTC),支持中英文、多语言、表格识别等功能。其核心优势在于高精度、支持复杂场景(如手写体、倾斜文字)。

2.2 安装与配置

安装步骤

  1. 安装PaddlePaddle

    1. pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

    (根据官方文档选择GPU/CPU版本)

  2. 安装pyddleocr

    1. pip install paddleocr

2.3 基本使用

示例代码

  1. from paddleocr import PaddleOCR
  2. # 初始化OCR(支持中英文)
  3. ocr = PaddleOCR(use_angle_cls=True, lang="ch") # use_angle_cls启用角度分类
  4. # 识别图片
  5. result = ocr.ocr("example.png", cls=True)
  6. # 输出结果
  7. for line in result:
  8. print(line[0][0], line[1][0]) # 坐标和文字

参数详解

  • lang:指定语言(ch为中文,en为英文,fr为法语等)。
  • use_angle_cls:是否检测文字方向(适用于倾斜图片)。
  • det_model_dir/rec_model_dir:自定义检测/识别模型路径。

2.4 高级功能

表格识别

  1. ocr = PaddleOCR(use_angle_cls=True, lang="ch", table_lang="ch")
  2. result = ocr.ocr("table.png", cls=True, table=True)

批量处理

  1. import os
  2. from paddleocr import PaddleOCR
  3. ocr = PaddleOCR()
  4. image_dir = "images/"
  5. for img_name in os.listdir(image_dir):
  6. img_path = os.path.join(image_dir, img_name)
  7. result = ocr.ocr(img_path)
  8. print(f"图片 {img_name} 的识别结果:\n", result)

2.5 优缺点分析

  • 优点:高精度、支持复杂场景、内置预处理(如角度矫正)。
  • 缺点:依赖深度学习框架,首次运行需下载模型(约200MB),对硬件要求较高。

三、pytesseract与pyddleocr对比

维度 pytesseract pyddleocr
技术原理 传统图像处理+规则匹配 深度学习(CRNN+CTC)
语言支持 100+种(需下载语言包) 中英文为主,支持多语言
识别准确率 简单场景高,复杂场景低 复杂场景高(如手写体)
运行速度 快(无模型加载) 慢(首次加载模型)
适用场景 文档扫描、简单图片 复杂背景、倾斜文字、表格识别

四、实际应用建议

  1. 简单场景:优先选择pytesseract,例如识别清晰的扫描件。
  2. 复杂场景:使用pyddleocr,例如识别手机拍摄的倾斜发票、手写笔记。
  3. 性能优化
    • 对pytesseract,预处理图像(灰度化、二值化)可提升准确率。
    • 对pyddleocr,使用GPU加速(安装GPU版PaddlePaddle)。

五、完整代码示例

pytesseract示例

  1. from PIL import Image, ImageFilter
  2. import pytesseract
  3. def preprocess_image(image_path):
  4. image = Image.open(image_path)
  5. # 灰度化
  6. image = image.convert("L")
  7. # 二值化
  8. image = image.point(lambda x: 0 if x < 140 else 255)
  9. return image
  10. image = preprocess_image("complex.png")
  11. text = pytesseract.image_to_string(image, lang="chi_sim", config="--psm 6")
  12. print("预处理后识别结果:\n", text)

pyddleocr示例

  1. from paddleocr import PaddleOCR
  2. # 初始化(使用GPU)
  3. ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=True)
  4. # 识别并保存结果到JSON
  5. result = ocr.ocr("handwritten.jpg", cls=True, rec_batch_num=6)
  6. with open("result.json", "w", encoding="utf-8") as f:
  7. import json
  8. json.dump(result, f, ensure_ascii=False, indent=4)

结论

pytesseract适合轻量级、多语言支持的场景,而pyddleocr在复杂场景下表现更优。开发者可根据项目需求选择合适的工具,或结合两者优势(例如用pytesseract快速处理简单图片,用pyddleocr处理疑难图片)。未来,随着深度学习模型的优化,OCR技术的准确率和效率将进一步提升。

相关文章推荐

发表评论