如何在 Python 中构建 OCR
世界充斥着海量的文本信息。 这里有很多非常有价值的内容,如果能更容易获取的话,将会非常有用。
这时,光学字符识别(OCR)技术就派上了用场。 想象一下,计算机能够像人类一样从图像中"读取"文本,这就是计算机视觉,它是计算机科学的一个分支,我们可以训练计算机识别图像中的不同对象。
在本教程中,我们将指导您使用 Python(一种以其简洁性和多功能性而闻名的编程语言)构建自己的 OCR 系统。 借助 Tesseract、 IronOCR和 OpenCV 等库,您很快就能释放从文档图像中提取、操作和处理文本的潜力。
OCR引擎(光学字符识别)的先决条件
在深入探讨构建 OCR 系统的细节之前,您需要准备以下几样东西:
- Python :请确保您的计算机上已安装 Python。 您可以从Python官方网站下载。 2.安装 Tesseract OCR :Tesseract OCR 是由 Google 开发的开源 OCR 引擎。 这是一个功能强大的工具,我们将在项目中使用它。 您可以从GitHub下载 Tesseract 库,并阅读有关 Tesseract OCR 安装过程的信息。
Python 库:本项目将使用两个重要的 Python 库:
pytesseract和opencv-python库。 您可以使用以下命令在命令行提示符或终端中安装它们:pip install pytesseract opencv-pythonpip install pytesseract opencv-pythonSHELL
构建OCR系统的步骤
借助 Python OCR 库和简单的 Python 脚本,您可以使用 Python 代码轻松构建 OCR。
步骤 1:导入库
首先,您需要导入必要的库:
import cv2 # OpenCV library for computer vision
import pytesseract # Tesseract library for OCRimport cv2 # OpenCV library for computer vision
import pytesseract # Tesseract library for OCR步骤 2:读取和处理图像
使用 OpenCV 加载图像并进行预处理以提高 OCR 准确率:
# Load the image using OpenCV
image = cv2.imread('sample_image.png')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding or other preprocessing techniques if needed
# This step helps in enhancing the quality for better OCR results# Load the image using OpenCV
image = cv2.imread('sample_image.png')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding or other preprocessing techniques if needed
# This step helps in enhancing the quality for better OCR results步骤 3:使用 Tesseract 进行 OCR 识别
现在是时候使用 Tesseract OCR 引擎对处理后的图像进行 OCR 识别了:
# Set the path to the Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
# Use pytesseract to perform OCR on the grayscale image
text = pytesseract.image_to_string(gray_image)# Set the path to the Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
# Use pytesseract to perform OCR on the grayscale image
text = pytesseract.image_to_string(gray_image)步骤 4:显示结果
如果要将原始图像和提取的文本可视化,可以使用 OpenCV 来显示它们:
# Display the original image using OpenCV
cv2.imshow('Original Image', image)
cv2.waitKey(0)
# Print the extracted text to the console
print("Extracted Text:", text)
cv2.destroyAllWindows() # Close the OpenCV window# Display the original image using OpenCV
cv2.imshow('Original Image', image)
cv2.waitKey(0)
# Print the extracted text to the console
print("Extracted Text:", text)
cv2.destroyAllWindows() # Close the OpenCV window原始图像
提取的文本
如您所见,结果可能会因图像的质量和复杂性而异,在某些情况下,对于具有复杂结构(例如包含表格)的图像,可能需要额外的训练(类似于机器学习训练)。
IronOCR。
在当今数据爆炸的时代,能够轻松地将印刷文本转换为机器可读内容的能力是一项变革性的能力。
IronOCR是一项尖端技术,它使开发人员能够轻松地将强大的光学字符识别 (OCR) 功能集成到他们的应用程序中。
无论您是从扫描文档中提取数据、自动输入数据还是增强可访问性,IronOCR 都能提供超越传统文本识别界限的全面解决方案。
在本次探索中,我们将深入 IronOCR 领域,揭示其多功能特性,并重点介绍其弥合物理世界和数字世界之间差距的潜力。
安装IronOCR
您可以使用 NuGet 程序包管理器控制台轻松安装 IronOCR,只需运行以下命令即可。
Install-Package IronOcr
IronOCR 也可从官方 NuGet 网站下载。
使用 IronOCR 从图像中提取文本
在本节中,我们将了解如何使用 IronOCR 轻松地从图像中提取文本。 以下是提取图像中文本的源代码。
using IronOcr;
using System;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.AddImage("r3.png");
OcrResult result = ocr.Read(input);
string text = result.Text;
Console.WriteLine(result.Text);
}using IronOcr;
using System;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.AddImage("r3.png");
OcrResult result = ocr.Read(input);
string text = result.Text;
Console.WriteLine(result.Text);
}Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Using input = New OcrInput()
input.AddImage("r3.png")
Dim result As OcrResult = ocr.Read(input)
Dim text As String = result.Text
Console.WriteLine(result.Text)
End Using输出
结论
在本教程中,我们探索了在 Python 中构建光学字符识别 (OCR) 系统的过程,揭示了从图像中提取文本的便捷性。
通过利用 Tesseract 和 OpenCV 等库,我们完成了从加载和预处理图像到利用 Tesseract OCR 引擎进行文本提取等关键步骤。
我们也谈到了精度限制等潜在挑战,而 IronOCR 等先进解决方案旨在解决这些问题。
无论你选择自己动手还是采用复杂的工具,OCR 的世界都以其将图像转换为可操作的文本、简化数据输入和增强可访问性的承诺而吸引着你。 有了这些新知识,你就可以踏上一段将视觉和数字领域无缝融合的旅程。
要开始使用IronOCR,请访问以下链接。 要查看如何从图像中提取文本的完整教程,请访问这里。
如果您今天想免费试用 IronOCR,请务必选择加入 IronOCR 提供的试用版,以便在没有水印的商业环境中探索其所有用途和潜力。 15 天试用期结束后,如需继续使用,只需购买许可证即可。








