軟件安裝筆記3:tesseract-ocr for mac和homebrew


一、下載地址:

tesseract github下載地址:https://github.com/tesseract-ocr/tesseract/wiki

二、安裝步驟

官方對於mac版本提供了兩種安裝方式:brew 和macports

macports 安裝可以參考:https://blog.csdn.net/Cloudox_/article/details/72841935

此處選擇brew安裝,參照下圖

安裝homebrew

參見官網

過程會比較慢,等等就好。

如果不想等,可以參考:https://blog.csdn.net/qq_35624642/article/details/79682979

安裝好后,查看版本:brew --version

mac 上一般可以在/usr/local 路徑上找到homebrew 的相關文件

接下來執行:

brew install tesseract 此處只選擇安裝tesseract

brew install --with-training-tools tesseract //安裝tesseract, 同時安裝訓練工具
brew install --all-languages tesseract //安裝tesseract,同時它還會安裝所有語言 不推薦,可以自己選擇安裝
brew install --all-languages --with-training-tools tesseract //安裝附加組件

 即可自動安裝完畢,且獨立生成文件夾,以后卸載也很方便,有點類似虛擬環境

 

/usr/local/Cellar/tesseract/4.0.0_1/share/tessdata/  這個路徑下面放識別的語言包

如果上面沒有自己想要的,可以上https://github.com/tesseract-ocr/tessdata 這里進行下載

注:數字和英文組合的驗證碼就用eng.traineddata/enm.traineddata,中文的話用chi_sim.traineddata。如果上面提供的語言包識別不是很准,可以訓練自己的語言包,這里不在展開,后續在研究。

 

三、測試

方法一:

tesseract 的調用相對簡單,如下圖

只要在終端執行:

tesseract image.png result

就會在當前目錄生成一個result.txt文件,里面即為識別的結果。

准確率還挺高的。

 

方法二:

通過pytesseract模塊

pip install pytesseract

pytesseract模塊一般與PIL模塊一起使用,用於打開圖片

安裝好pytesseract 后,要進行一個tesseract_cmd 設置,要不然容易報錯誤:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

解決辦法,打開本地安裝pytesseract包中的pytesseract.py文件

在第35行中,把tesseract_cmd = 'tesseract' 后面的路徑改為自己本地tesseract執行文件的路徑。如我本機的文件路徑為:

tesseract_cmd = '/usr/local/Cellar/tesseract/4.0.0_1/bin/tesseract'

 

 生成test.py文件。

from PIL import Image
import pytesseract

if __name__=='__main__':
    text = pytesseract.image_to_string(Image.open('image.png'),lang='eng')
    print(text)

運行結果:

 

 

參考:https://blog.csdn.net/wodedipang_/article/details/84585914

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM