OCR 工具 如何在 Python 中建立 OCR Kannapat Udonpant 更新日期:6月 22, 2025 Download IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 這個世界充斥著大量的文字資訊。 有許多寶貴的內容,如果能稍微更容易獲得,將會是非常有用的。 這就是光學字符識別(OCR)技術發揮作用的地方。 想象一下,電腦能夠像人類一樣從圖像中'讀取'文字,這就是計算機視覺,它是計算機科學的一個分支,我們可以訓練電腦識別和辨認圖像中的不同主體。 在本教程中,我們將引導您使用以簡單性和多功能性著稱的程序語言Python來構建自己的OCR系統。 借助像Tesseract、IronOCR和OpenCV等庫,您很快就能夠釋放從文檔圖像中提取、處理和處理文字的潛力。 OCR引擎(光學字符識別)的先決條件 在開始構建我們的OCR系統之前,您需要準備以下幾樣東西: Python:確保您的電腦上已安裝Python。 您可以從官方Python網站下載。 安裝Tesseract OCR:Tesseract OCR是由Google開發的開源OCR引擎。 這是我們專案中將用到的一個強大工具。 您可以從GitHub下載Tesseract庫並了解Tesseract OCR的安裝過程。 Python庫:我們在此專案中將使用兩個重要的Python庫:pytesseract和opencv-python庫。 您可以在命令行提示符或終端中使用以下命令安裝它們: pip install pytesseract opencv-python pip install pytesseract opencv-python SHELL 構建OCR系統的步驟 使用Python代碼和Python OCR庫及一個簡單的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 原始圖像 提取的文字 如您所見,結果可能會根據圖像的質量和複雜性而有所不同,在某些情況下,對於包含複雜結構(如包含表格)的圖像,可能需要額外的訓練(類似於機器學習訓練)。 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 $vbLabelText $csharpLabel 輸出 結論 在本教程中,我們已經探索了構建光學字符識別(OCR)系統的過程,揭示了以驚人的輕鬆從圖像中提取文本的能力。 通過使用Tesseract和OpenCV等庫,我們已經經歷了從加載和預處理圖像到使用Tesseract OCR引擎進行文本提取的基本步驟。 我們還提及了象IronOCR這樣的高級解決方案旨在解決的潛在挑戰,如準確性限制。 無論您選擇DIY路線還是採用複雜工具,OCR的世界都充滿了將圖像轉換為可實用文本、簡化數據輸入並提高無障礙性的可能性。 擁有這些新知識,您已經準備好踏上將視覺和數字領域無縫融合的旅程。 To get started with IronOCR visit the following link. 要查看如何從圖像中提取文字的完整教程,請訪問此處。 如果您想今天免費試用IronOCR,請務必選擇IronOCR提供的試用,以便在無水印的商業環境中探索其所有用途和潛力。 試用結束後的15天內繼續使用,您只需購買一個許可證即可。 Kannapat Udonpant 立即與工程團隊聊天 軟體工程師 在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。 相關文章 更新日期 6月 22, 2025 Power Automate OCR 開發者教學 這項光學字符識別技術應用於文件數字化、自動化 PDF 數據提取和輸入、發票處理和使掃描的 PDF 可搜尋。 閱讀更多 更新日期 6月 22, 2025 Easyocr vs Tesseract(OCR 功能比較) 流行的 OCR 工具和函式庫,像 EasyOCR,Tesseract OCR,Keras-OCR 和 IronOCR,被普遍用於將此功能集成到現代應用中。 閱讀更多 更新日期 6月 22, 2025 如何將圖片轉換為文本 在當前的數位時代,將基於影像的內容轉換為易於閱讀的可編輯、可搜尋的文本 閱讀更多 阿拉伯文識別工具:提高準確性如何在 C# 中掃描為可編輯文本
更新日期 6月 22, 2025 Easyocr vs Tesseract(OCR 功能比較) 流行的 OCR 工具和函式庫,像 EasyOCR,Tesseract OCR,Keras-OCR 和 IronOCR,被普遍用於將此功能集成到現代應用中。 閱讀更多