OCR 工具

Easyocr 與 Tesseract (OCR 功能比較)

發佈 2024年12月15日
分享:

介紹

光學字符識別(光學字符識別)是將掃描的紙質文件、PDF 文件或相機拍攝的高解析度圖像處理成可列印和搜尋的數據的技術。 提取文本特徵的識別和形態操作允許光學字符識別自動化資料輸入,加快資訊處理過程並提高準確性。

OCR 掃描文件,識別字符,如字母、數字或符號,並將其轉換為機器可讀格式。 其用途包括書籍數位化、表單處理、文件工作流程自動化,以及提高對於盲人可及性的改善。 隨著深度學習和人工智慧的發展,OCR引擎在識別複雜格式方面變得非常準確,例如手寫、多語言文件,甚至是低品質的圖像。

流行的OCR工具和庫,如EasyOCR、Tesseract OCR、Keras-OCR以及IronOCR,通常用於將此功能整合到現代應用程式中。

簡易 OCR

簡易 OCR是一個開源的Python庫,旨在使從圖像中提取文本變得簡單且高效。 它使用深度學習技術,並支持超過80種語言,包括拉丁語、中文、阿拉伯語等。 其 API 非常簡單,任何人都可以輕鬆將 OCR 預測功能集成到他們的應用程序中,而無需過多設定。使用 EasyOCR Tesseract,您可以進行簡單的文檔數位化、車牌識別,甚至從圖片中提取文字。

損壞的圖片 從Pixabay添加,從你的文件中選擇或拖放圖片到這裡。

EasyOCR以其對印刷和手寫文字的識別能力而聞名。 大多數其他的OCR解決方案不提供此功能。 它還可以處理多行文本,並且適用於任何圖像類型,即使是低質量的圖像。 因此,它在現實世界的使用情況中具有很強的穩定性,只依賴少量的依賴項。 它輕量且能在現代硬體上高效運行,無需 GPU。 這使得EasyOCR對於可能需要靈活OCR功能的開發人員來說頗具吸引力。

EasyOCR 的功能

許多功能使 EasyOCR 成為全面且真正強大的 OCR 工具:

辨識超過 80 種語言: EasyOCR 可以閱讀中文、日文、韓文、阿拉伯文、基於拉丁語的語言,以及其他許多書寫複雜字詞與複雜語言。

它將識別來自圖像的手寫和打印文本,因此進一步擴大潛在應用的範圍。

先進的深度學習技術: 認證支持強大的先進深度學習技術演算法,具有高效能和精確度,特別是在嘈雜或失真的文字佈局和圖像中。

簡單的 API: 非常易於實施的 API,讓用戶可以在不需進一步配置的情況下,快速將 OCR 功能整合到應用程式中。

多行文字偵測: 識別多行文字; 適用於文件、書籍或多行標誌。

輕量化: 它在中央處理器(CPU)上運行良好,並且可以利用圖形處理器(GPU)來提升性能,但即便是基本硬體也能夠運作。

圖像預處理: 包含主要的圖像預處理工具,用於清理來自雜訊或低解析度圖像的 OCR 輸出。

靈活部署: 可以在多個平台上運行,並且相對容易嵌入到 Python 應用程序中。

安裝

EasyOCR 可以透過 pip 安裝,pip 是 Python 的套件管理工具。 首先,確保所有的依賴項已滿足。

首先是 PyTorch 庫:torch 和 torchvision。 Torch 和 torchvision 都可以與 EasyOCR 一起安裝:

安裝 EasyOCR: 要安裝它,只需打開終端機或命令行並輸入命令:

pip install easyocr
pip install easyocr
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'pip install easyocr
VB   C#

損壞的圖片 從Pixabay添加,從你的文件中選擇或拖放圖片到這裡。

安裝 PyTorch(如果尚未安裝)(EasyOCR 所需): EasyOCR 運行於 PyTorch。 如果在您的環境中自動安裝,請安裝特定版本; 遵循官方的 PyTorch 安裝指引指南.

現在您已準備好使用EasyOCR進行文本提取任務。

使用 EasyOCR 對圖像進行 OCR(光學字符識別)

import easyocr
import matplotlib.pyplot as plt
import cv2
# Initialize the EasyOCR reader
reader = easyocr.Reader(['en'])  # Specify the languages (e.g., 'en' for English)
# Load the image
image_path = 'sample_image.png'  # Path to the image
image = cv2.imread(image_path)
# Perform OCR on the image
result = reader.readtext(image_path)
# Print the detected text and its bounding boxes
for (bbox, text, prob) in result:
    print(f"Detected Text: {text} (Confidence: {prob:.4f})")
# Optionally, display the image with bounding boxes around the detected text
for (bbox, text, prob) in result:
    # Unpack the bounding box
    top_left, top_right, bottom_right, bottom_left = bbox
    top_left = tuple(map(int, top_left))
    bottom_right = tuple(map(int, bottom_right))
    # Draw a rectangle around the text
    cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
# Convert the image to RGB (since OpenCV loads images in BGR by default)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image with bounding boxes
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
import easyocr
import matplotlib.pyplot as plt
import cv2
# Initialize the EasyOCR reader
reader = easyocr.Reader(['en'])  # Specify the languages (e.g., 'en' for English)
# Load the image
image_path = 'sample_image.png'  # Path to the image
image = cv2.imread(image_path)
# Perform OCR on the image
result = reader.readtext(image_path)
# Print the detected text and its bounding boxes
for (bbox, text, prob) in result:
    print(f"Detected Text: {text} (Confidence: {prob:.4f})")
# Optionally, display the image with bounding boxes around the detected text
for (bbox, text, prob) in result:
    # Unpack the bounding box
    top_left, top_right, bottom_right, bottom_left = bbox
    top_left = tuple(map(int, top_left))
    bottom_right = tuple(map(int, bottom_right))
    # Draw a rectangle around the text
    cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
# Convert the image to RGB (since OpenCV loads images in BGR by default)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image with bounding boxes
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
#Initialize the EasyOCR reader
#Load the image
#Perform OCR on the image
#Print the detected text and its bounding boxes
#Optionally, display the image with bounding boxes around the detected text
	#Unpack the bounding box
	#Draw a rectangle around the text
#Convert the image to RGB (since OpenCV loads images in BGR by default)
#Display the image with bounding boxes
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import easyocr import TryCast(matplotlib.pyplot, plt) import cv2 reader = easyocr.Reader(['en']) # Specify the languages(e.g., 'en' for English) image_path = 'sample_image.png' # Path @to the image image = cv2.imread(image_path) result = reader.readtext(image_path) for(bbox, text, prob) in result: print(f"Detected Text: {text} (Confidence: {prob:.4f})") for(bbox, text, prob) in result: top_left, top_right, bottom_right, bottom_left = bbox top_left = tuple(map(int, top_left)) bottom_right = tuple(map(int, bottom_right)) cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) plt.imshow(image_rgb) plt.axis('off') plt.show()
VB   C#

下圖是由上述程式碼生成的輸出。

Easyocr 與 Tesseract(OCR 功能比較):圖 3 - EasyOCR 輸出

Tesseract

Tesseract是最受歡迎的開源光學字符識別引擎之一,具有多種超參數選項。 可以使用 pytesseract 從 Python 應用程式中訪問。 Tesseract 的開發由惠普公司開始,但後來由 Google 改進。 它具有高度的多功能性,能夠從圖像和 PDF 中提取出超過 100 種語言的文本。 Python 的包裝器允許通過 pytesseract 與 Tesseract 無縫互動。

Easyocr 與 Tesseract 的比較 (OCR 功能比較):圖 4 - Tesseract

Tesseract 是機器印刷和文字檢測與提取的領導者,具有多語言識別能力,支持對新字體進行訓練,並能進行文字排版分析。 它主要支持印刷文本,但在此之外對手寫文本的處理也相當不錯,但準確性程度中等偏高。 Tesseract 被廣泛應用於文件數位化、掃描收據、自動化數據輸入以及創建可搜索的 PDF。 將所有這些靈活性應用於 Python 和 Tesseract,對於從事 OCR 相關任務的開發人員來說,可能是一個非常強大的組合。

Tesseract OCR 的特色

pytesseract的一些最重要功能是:

多語言支援: Tesseract 可以識別超過 100 種語言,且 pytesseract 提供了相當簡單的多語言光學字符識別功能以用於 Python 腳本。它還支援對額外的自定義字體或語言進行訓練,從而將功能擴展到更多的語言或字體。

用於圖像轉文字轉換:pytesseract 從多種格式的圖像(如 PNG、JPEG、BMP、GIF 和 TIFF)中提取文本內容,以對多種不同類型的來源進行光學字符識別(OCR)。

從 PDF 轉換為可搜索的 PDF: Tesseract 會讀取 PDF 文件的文本並使該 PDF 可搜索。 用戶可以搜尋和索引掃描文件的內容。

它可以讀取複雜的文本佈局,包括多欄文檔、表格以及包含文本和圖像的混合內容。 因此,它可以更準確地從非標準佈局中提取文本。

手寫文字識別功能: pytesseract 主要用於印刷文本; 然而,準確度的水平取決於所使用手寫字的質量和清晰度。

自訂設定: 使用者可以透過 pytesseract 傳遞自訂的 Tesseract 設定參數,以便對 OCR 的性能進行微調,並根據需要為文本提供某些屬性,以適當的識別模式或圖像進行微調。

Simple API: 這是一個基於pytesseract的簡單API,因此開發者可以輕鬆地以較少的代碼將OCR添加到他們的Python專案中。

此程式庫可以很好地與其他程式庫協作,例如 OpenCV 和 PIL。(Python 圖像庫),或使用 NumPy 處理影像預處理,以提高 OCR 的準確性。

安裝

安裝 Tesseract 後,使用 pip 安裝 pytesseract 套件:

pip install pytesseract
pip install pytesseract
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'pip install pytesseract
VB   C#

Easyocr與Tesseract(OCR功能比較):圖5 - pytesseract

使用 pytesseract 進行 OCR 圖像識別

import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example: Read text from an image
image = Image.open('sample_image.png')
text = pytesseract.image_to_string(image)
print(text)
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example: Read text from an image
image = Image.open('sample_image.png')
text = pytesseract.image_to_string(image)
print(text)
#Example: Read text from an image
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import pytesseract from PIL import Image pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>' image = Image.open('sample_image.png') text = pytesseract.image_to_string(image) print(text)
VB   C#

以下是從上述程式碼生成的輸出。

Easyocr 與 Tesseract(OCR 功能比較):圖 6 - Tesseract 輸出

IronOCR

一個強大的光學字符識別庫讓 .NET 開發人員能夠利用IronOCR以有效的方式從圖像、PDF及更多文件格式中提取文本。 先進的算法確保即使在複雜的佈局或多語環境中也具有非常高的準確性。 支持 JPEG、PNG、GIF 和 TIFF 格式。 該程式庫提供可變的設置,可以通過設置如影像解析度或文本方向等參數來對OCR引擎過程進行微調。

圖像預處理功能確保更高品質的輸入圖像可以轉換為更高的識別準確性,並進一步將輸出文件轉換為可搜索的 PDF,以便於信息檢索。 因此,IronOCR 能夠如此輕鬆地整合到 Web 應用中,是開發人員希望在多個領域中實施最值得信賴的文字提取和文件數位化解決方案的最佳工具。

IronOCR 的功能

高準確性: 它使用複雜的先進算法,無論文檔的複雜程度或使用各種字體,均能提供高準確度的文字識別。

支援多種格式: 除了 PDF,它還接受 JPEG、PNG、GIF 和 TIFF 等圖像格式,以確保能在各種應用中執行預期功能。

支援多語言識別: 支援多種語言,在不同的語言環境中能夠提供準確的文本提取結果。

文本佈局保留: 這可保留文件的原始佈局,使提取的文本具有相同的佈局,這對清晰閱讀很重要。

可配置的光學字符識別 (OCR): 它提供了可配置的影像解析度、文字方向等參數,使開發人員能夠將影像特定的 OCR 工具優化到一定程度。

圖像預處理: 它配備了一些基本工具,旨在增強圖像,包括去除噪聲、調整對比度和調整大小,所有這些都是為了提高OCR的準確性。

可搜尋的 PDF: 掃描圖像和文件可以直接轉換為可搜尋的 PDF,以實現高效的數據管理和檢索。

簡易整合: 它允許輕鬆整合到 .NET 中,從而讓用戶相當容易地添加 OCR 功能。

批次處理: 它適用於同時處理多張圖片或文件。 它在處理大量數據時非常有用。

安裝

安裝過程相當簡單:打開 NuGet 套件管理器為解決方案並啟動一個新的 Visual Studio 專案。 只需输入「IronOCR」并查找列表。然后,选择最新版本的 IronOCR 并点击安装。

Easyocr 與 Tesseract (OCR 功能比較):圖 7 - 安裝法語語言包

使用 IronOCR 的範例程式碼

using IronOcr;
class Program
{
    static void Main(string[] args)
    {
        // Initialize IronTesseract engine
        var Ocr = new IronTesseract();
        // Add multiple languages
        Ocr.Language = OcrLanguage.English;
        // Path to the image
        var inputFile = @"path\to\your\image.png";
        // Read the image and perform OCR
        using (var input = new OcrInput(inputFile))
        {
            // Perform OCR
            var result = Ocr.Read(input);
            // Display the result
            Console.WriteLine("Text:");
            Console.WriteLine(result.Text);
        }
    }
}
using IronOcr;
class Program
{
    static void Main(string[] args)
    {
        // Initialize IronTesseract engine
        var Ocr = new IronTesseract();
        // Add multiple languages
        Ocr.Language = OcrLanguage.English;
        // Path to the image
        var inputFile = @"path\to\your\image.png";
        // Read the image and perform OCR
        using (var input = new OcrInput(inputFile))
        {
            // Perform OCR
            var result = Ocr.Read(input);
            // Display the result
            Console.WriteLine("Text:");
            Console.WriteLine(result.Text);
        }
    }
}
Imports IronOcr
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Initialize IronTesseract engine
		Dim Ocr = New IronTesseract()
		' Add multiple languages
		Ocr.Language = OcrLanguage.English
		' Path to the image
		Dim inputFile = "path\to\your\image.png"
		' Read the image and perform OCR
		Using input = New OcrInput(inputFile)
			' Perform OCR
			Dim result = Ocr.Read(input)
			' Display the result
			Console.WriteLine("Text:")
			Console.WriteLine(result.Text)
		End Using
	End Sub
End Class
VB   C#

比較評估

高精確度

IronOCR 在處理複雜的版面配置、有噪點的圖像和低解析度文本時,比其最近的競爭對手 Tesseract 或 EasyOCR 更準確。 內建的圖像預處理工具,如降噪和對比調整,都增加了在實際應用中實現準確性的可能性。

多格式和版面保持

IronOCR 表現超越這兩個,因為它輕鬆處理任何格式的圖像、PDF 文件,以及在保留原始文檔結構和格式的同時處理多欄佈局。 因此,它最適合在更傳統的數位化項目中使用,因為保持版面結構是一種優勢。

它有一個很大的優勢,即可以將圖像和掃描文件直接轉換為完全可搜尋的PDF檔,而不依賴於額外的工具或庫,如Tesseract和EasyOCR。

IronOCR 提供先進的預處理功能

即使是最差質量的圖像,使用此功能也能產生高效能的OCR準確度。 這將減少對其他庫如 OpenCV 的依賴,因此成為優質文字提取的一站式解決方案。

可擴展性和性能

針對高速、資源效率的OCR進行優化,IronOCR保證大型文件處理工作的可擴展性,這是任何企業應用程序的首要任務。

支援與更新

IronOCR 提供商業支持,這對實際更新和漏洞修復的增加以及長期專門協助具有積極作用,因此能保證長期的可靠性和最新的 OCR 進展,而這些是開源競爭對手如 Tesseract 和 EasyOCR 所缺乏的。

結論

在最重要的OCR庫中,毫無疑問,IronOCR是最好的,因為它具有卓越的準確性和易於整合的優點,更不用說其他功能,例如圖像預處理和創建可搜索的PDF。 它可以精確處理複雜的版面和噪聲較大的圖像,並保持文件結構完整,開箱即用支持多種語言,相較於像 Tesseract 和 EasyOCR 這樣的開源解決方案。

包含的工具以及與 .NET 和 Python 的無縫整合確保這是開發人員想在任何項目中整合高質量 OCR 的全方位套件。 憑藉其良好的性能、可擴展性和商業支持,它在一些小型和大型文檔數字化工程中也具有優勢,因此成為可靠且高效文本識別的終極選擇。

若要了解更多關於 IronOCR 的資訊及其運作方式,您可以造訪此文檔頁面,其中 IronOCR 提供免費試用。 如需了解有關Iron Software產品的更多信息,請參閱程式庫套件頁面.

< 上一頁
Power Automate OCR(開發者教程)
下一個 >
如何將圖片轉換為文字