跳至頁尾內容
OCR工具

如何在 Python 中建構 OCR

世界充斥著海量的文字訊息。 這裡有很多非常有價值的內容,如果能更容易取得的話,將會非常有用。

這時,光學字元辨識(OCR)技術就派上了用場。 想像一下,電腦能夠像人類一樣從圖像中"讀取"文本,這就是電腦視覺,它是電腦科學的一個分支,我們可以訓練電腦辨識圖像中的不同物件。

在本教程中,我們將指導您使用 Python(一種以其簡潔性和多功能性而聞名的程式語言)來建立自己的 OCR 系統。 透過 Tesseract、 IronOCR和 OpenCV 等函式庫,您很快就能釋放從文件影像中提取、處理和編輯文字的潛力。

OCR引擎(光學字元辨識)的先決條件

在深入探討建造 OCR 系統的細節之前,您需要先準備以下幾件事:

  1. Python:請確保您的電腦上已安裝 Python。 您可以從Python官方網站下載。 2.安裝 Tesseract OCR: Tesseract OCR 是由 Google 開發的開源 OCR 引擎。 這是一個功能強大的工具,我們將在專案中使用它。 您可以從GitHub下載 Tesseract 庫,並閱讀有關 Tesseract OCR 安裝過程的資訊。
  2. Python 函式庫:本專案將使用兩個重要的 Python 函式庫: pytesseractopencv-python函式庫。 您可以使用以下命令在命令列提示符或終端機中安裝它們:

    pip install pytesseract opencv-python
    pip install pytesseract opencv-python
    SHELL

如何在 Python 中建構 OCR:圖 1

建構OCR系統的步驟

透過 Python OCR 函式庫和簡單的 Python 腳本,您可以使用 Python 程式碼輕鬆建立 OCR。

步驟 1:導入庫

首先,您需要匯入必要的庫:

import cv2  # OpenCV library for computer vision
import pytesseract  # Tesseract library for OCR
import cv2  # OpenCV library for computer vision
import pytesseract  # Tesseract library for OCR
PYTHON

步驟 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
PYTHON

步驟 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)
PYTHON

步驟 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
PYTHON

原始影像

如何在 Python 中建構 OCR:圖 2

提取的文本

如何在 Python 中建構 OCR:圖 3

如您所見,結果可能會因圖像的品質和複雜性而異,在某些情況下,對於具有複雜結構(例如包含表格)的圖像,可能需要額外的訓練(類似於機器學習訓練)。

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);
}
$vbLabelText   $csharpLabel

輸出

如何在 Python 中建構 OCR:圖 4

結論

在本教程中,我們探索了在 Python 中建立光學字元辨識 (OCR) 系統的過程,揭示了從圖像中提取文字的便利性。

透過利用 Tesseract 和 OpenCV 等函式庫,我們完成了從載入和預處理影像到利用 Tesseract OCR 引擎進行文字擷取等關鍵步驟。

我們也談到了精度限制等潛在挑戰,而 IronOCR 等先進解決方案旨在解決這些問題。

無論你選擇自己動手還是採用複雜的工具,OCR 的世界都以其將圖像轉換為可操作的文字、簡化資料輸入和增強可訪問性的承諾而吸引著你。 有了這些新知識,你就可以踏上一段將視覺和數位領域無縫融合的旅程。

要開始使用IronOCR,請造訪以下連結。 要查看如何從圖像中提取文字的完整教程,請訪問這裡

如果您今天想免費試用 IronOCR,請務必選擇加入 IronOCR 提供的試用版,以便在沒有浮水印的商業環境中探索其所有用途和潛力。 15 天試用期結束後,如需繼續使用,只需購買許可證即可。

坎納奧帕特·烏東潘特
軟體工程師
在成為軟體工程師之前,Kannapat 在日本北海道大學完成了環境資源專業的博士學位。在攻讀博士學位期間,他還加入了生物生產工程系下屬的車輛機器人實驗室。 2022 年,他憑藉 C# 技能加入了 Iron Software 的工程團隊,專注於 IronPDF 的開發。 Kannapat 非常珍惜這份工作,因為他可以直接向 IronPDF 大部分程式碼的編寫者學習。除了與同事學習之外,Kannapat 也享受在 Iron Software 工作的社交氛圍。工作之餘,Kannapat 通常會玩 PS5 遊戲或重溫《最後生還者》。