光学字符识别(OCR)是一种从图像、扫描文档甚至手写笔记中提取可读文本的技术。在Python中,OCR工具经过多年发展,最新版本的这些库现在提供了更强大、高效的解决方案。
本文将介绍Python中排名前七的OCR库,突出它们的优势、独特功能,并提供代码示例,帮助您入门。
1. Tesseract OCR(pytesseract)
Tesseract无疑是Python生态系统中最流行、最广泛使用的OCR库。最初由HP开发,现在由Google维护,Tesseract提供了针对100多种语言的高质量OCR功能。
主要特点:
- 开源且免费使用。
- 支持多种语言,包括非拉丁字母表。
- 识别图像、扫描文档和PDF中的文本。
- 可通过自定义训练数据进行定制化,适用于特定用例。
- 与预处理工具如OpenCV协同工作,提高准确性。
要在Linux上安装Tesseract OCR,请根据您的发行版执行以下步骤:
sudo apt install tesseract-ocr [On Debian, Ubuntu and Mint] sudo yum install tesseract [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] sudo emerge -a sys-apps/tesseract [On Gentoo Linux] sudo apk add tesseract [On Alpine Linux] sudo pacman -S tesseract [On Arch Linux] sudo zypper install tesseract [On OpenSUSE] sudo pkg install tesseract [On FreeBSD]
安装Tesseract后,如果您想要在Python中使用它,您需要使用pip软件包管理器安装pytesseract软件包。
pip3 install pytesseract OR pip install pytesseract
以下是一个使用Tesseract OCR与pytesseract
库从图像中提取文本的Python示例代码。
import pytesseract from PIL import Image # Load an image img = Image.open("image_sample.png") # Use Tesseract to extract text text = pytesseract.image_to_string(img) # Print the extracted text print(text)
2. EasyOCR
EasyOCR是另一个出色的Python OCR库,支持80多种语言,适合初学者使用。它基于深度学习技术构建,是那些想要利用现代OCR技术的人的绝佳选择。
主要特点:
- 深度学习模型具有高准确性。
- 支持广泛的语言。
- 可检测垂直和多语言图像中的文本。
- 简单易懂的API。
要在Linux上安装EasyOCR,您可以根据您的发行版使用以下pip
命令。
pip3 install easyocr OR pip install easyocr
安装完成后,您可以使用EasyOCR从图像中提取文本。
import easyocr # Initialize the OCR reader reader = easyocr.Reader(['en']) # Extract text from an image result = reader.readtext('image_sample.png') # Print the extracted text for detection in result: print(detection[1])
3. OCRopus
OCRopus是由Google开发的开源OCR系统。虽然它主要用于历史文献和图书,但OCRopus也可以应用于各种文本提取任务。
主要特点:
- 专注于文档布局分析和文本提取。
- 考虑了模块化,便于定制。
- 可以处理多页文档和大型数据集。
以下是一个从图像中提取文本的Python示例代码。
import subprocess # Use OCRopus to process an image subprocess.run(['ocropus', 'identify', 'image_sample.png'])
4. PyOCR
PyOCR是围绕几个OCR引擎(包括Tesseract和CuneiForm)的Python封装器。它为将OCR功能集成到Python应用程序提供了简单的接口。
主要特点:
- 可以与多个OCR引擎进行交互。
- 提供了用于文本提取的简单API。
- 可以与图像预处理库结合以获得更好的结果。
PyOCR需要Tesseract(OCR引擎)和Pillow(图像处理库)。您可以使用以下命令安装它们:
sudo apt install tesseract-ocr [On Debian, Ubuntu and Mint] sudo yum install tesseract [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] sudo emerge -a sys-apps/tesseract [On Gentoo Linux] sudo apk add tesseract [On Alpine Linux] sudo pacman -S tesseract [On Arch Linux] sudo zypper install tesseract [On OpenSUSE] sudo pkg install tesseract [On FreeBSD]
现在,您可以使用pip
安装pyocr
和pillow
库:
pip3 install pyocr pillow OR pip install pyocr pillow
以下是一个使用PyOCR和Tesseract从图像中提取文本的Python示例:
import pyocr from PIL import Image # Choose the OCR tool (Tesseract or CuneiForm) tool = pyocr.get_available_tools()[0] # Load the image img = Image.open('image_sample.png') # Extract text from the image text = tool.image_to_string(img) # Print the extracted text print(text)
5. PaddleOCR
PaddleOCR是由PaddlePaddle开发的OCR库,是一个深度学习框架。它支持80多种语言,并且由于使用深度学习模型,具有尖端的准确性。
主要特点:
- 高性能,特别适用于具有复杂背景的图像。
- 支持文本检测、识别和布局分析。
- 包括多种语言的预训练模型。
要在Linux中安装PaddleOCR,请使用:
pip3 install paddlepaddle paddleocr OR pip install paddlepaddle paddleocr
以下是一个使用paddleocr库从图像中提取文本的Python示例:
from paddleocr import PaddleOCR # Initialize the OCR ocr = PaddleOCR(use_angle_cls=True, lang='en') # Perform OCR on an image result = ocr.ocr('image_sample.png', cls=True) # Print the extracted text for line in result[0]: print(line[1])
6. Kraken
Kraken是专为历史和多语言文本设计的高性能OCR库。它构建在OCRopus之上,并提供用于复杂布局和文本提取的附加功能。
主要特点:
- 最适合于古籍和多语言OCR。
- 处理复杂的文本布局和历史字体。
- 使用机器学习提高识别准确性。
在Linux中安装 Kraken,使用如下命令:
pip3 install kraken OR pip install kraken
以下是一个使用 kraken 库从图像中提取文本的Python示例:
import kraken # Load the model and recognize text text = kraken.binarize("image_sample.png") # Print the recognized text print(text)
7. Textract (AWS)
AWS Textract 是亚马逊基于云的OCR服务,可以分析文档和表单,并具有高准确性地提取文本。它与其他AWS服务无缝集成。
主要特点:
- 基于云的OCR,具有可扩展的解决方案。
- 支持文档结构分析,包括表格和表单。
- 与AWS服务集成,用于进一步数据处理。
在Linux中安装 Textract,使用如下命令:
pip3 install boto3 OR pip install boto3
以下是一个示例Python脚本,使用 AWS Textract 从文档(例如扫描的PDF或图像文件)中提取文本。
import boto3 # Initialize a Textract client client = boto3.client('textract') # Path to the image or PDF file you want to analyze file_path = 'path_to_your_file.png' # Replace with your file path # Open the file in binary mode with open(file_path, 'rb') as document: # Call Textract to analyze the document response = client.detect_document_text(Document={'Bytes': document.read()}) # Print the extracted text for item in response['Blocks']: if item['BlockType'] == 'LINE': print(item['Text'])
结论
在Python中选择正确的OCR库取决于特定的用例、语言要求以及正在处理的文档的复杂性。无论您是处理历史文档、多语言文本还是简单的扫描PDF,这些库都提供了强大的文本提取工具。
对于初学者来说,Tesseract 和 EasyOCR 是极好的起点,因为它们易于使用且被广泛采用。然而,对于更高级或专业的任务,像 PaddleOCR、OCRopus 和 Kraken 这样的库提供了更大的灵活性和准确性。
Source:
https://www.tecmint.com/python-text-extraction-from-images/