天天看點

Python下Tesseract Ocr引擎及安裝介紹,tesseractocr

Python下Tesseract Ocr引擎及安裝介紹,tesseractocr

1、Tesseract介紹

tesseract 是一個google支援的開源ocr項目,其項目位址:https://github.com/tesseract-ocr/tesseract,目前最新的源碼可以在這裡下載下傳。

實際使用tesseract ocr也有兩種方式:1- 動态庫方式 

libtesseract

  2  - 執行程式方式 

tesseract

.exe

由于本人也是python菜鳥一個,是以方式1暫時不會,隻好采取方式2。

2、Tesseract安裝包下載下傳

Tesseract的release版本下載下傳位址:https://github.com/tesseract-ocr/tesseract/wiki/Downloads,這裡需要注意這一段話:

Currently, there is no official Windows installer for newer versions.

意思就是官方不提供最新版windows平台安裝包,隻有相對略老的3.02.02版本,其下載下傳位址:https://sourceforge.net/projects/tesseract-ocr-alt/files/。

最新版3.03和3.05版本,都是三方維護和管理的安裝包,有好幾個發行機構,分别是:

3rd party Windows exe's/installer

  • binaries compiled by @egorpugin (ref issue # 209)https://www.dropbox.com/s/8t54mz39i58qslh/tesseract-3.05.00dev-win32-vc19.zip?dl=1

    You have to install VC2015 x86 redist from microsoft.com in order to run them. Leptonica is built with all libs except for libjp2k.

  • https://github.com/UB-Mannheim/tesseract/wiki
  • http://domasofan.spdns.eu/tesseract/

總結一下:

1、官方釋出的3.02版本下載下傳位址:http://downloads.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Ftesseract-ocr-alt%2Ffiles%2F&ts=1464880498&use_mirror=jaist

2、德國曼海姆大學發行的3.05版本下載下傳位址,http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.00dev.exe

3、Simon Eigeldinger (@DomasoFan) 維護的另一個版本:http://3.onj.me/tesseract/,值得稱道的是,這個網址裡還有一個比較詳細的說明。

以上版本如果在下載下傳時發現無法下載下傳,可以首先試試迅雷,其次,可能就需要FQ了。

本人使用的是官方釋出的3.02版本,也就是連結1.

3、Tesseract ocr使用說明

安裝之後,預設目錄C:\Program Files (x86)\Tesseract-OCR,你需要把這個路徑放到你作業系統的path搜尋路徑中,否則後面使用起來會不友善。

在安裝目錄C:\Program Files (x86)\Tesseract-OCR下可以看到 tesseract.exe這個指令行執行程式。

tesseract文法如下:

例如:tesseract 1.png output-l eng -psm 7 ,表示采取單行文本方式,使用英語字庫識别1.png這個圖檔檔案,識别結果輸出到目前目錄output.txt檔案中。

1 D:\python\lnypcg\test>tesseract
 2 Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]
 3 
 4 pagesegmode values are:
 5 0 = Orientation and script detection (OSD) only.
 6 1 = Automatic page segmentation with OSD.
 7 2 = Automatic page segmentation, but no OSD, or OCR
 8 3 = Fully automatic page segmentation, but no OSD. (Default)
 9 4 = Assume a single column of text of variable sizes.
10 5 = Assume a single uniform block of vertically aligned text.
11 6 = Assume a single uniform block of text.
12 7 = Treat the image as a single text line. #-psm 7 表示用單行文本識别
13 8 = Treat the image as a single word.
14 9 = Treat the image as a single word in a circle.
15 10 = Treat the image as a single character.
16 -l lang and/or -psm pagesegmode must occur before anyconfigfile. #-l eng 代表使用英語識别
17 
18 Single options:
19   -v --version: version info
20   --list-langs: list available languages for tesseract engine
           

4、Tesseract ocr使用執行個體

現在有一個經過灰階處理之後的驗證碼檔案

Python下Tesseract Ocr引擎及安裝介紹,tesseractocr

,在指令行中調用tesseract.exe實作預設,并把識别出來的文本輸出到output.txt文本檔案中。

(如何灰階處理,在python裡可以使用PIL庫,先挖個坑,下次寫。)

1 D:\python\lnypcg\test>dir
 2  驅動器 D 中的卷沒有标簽。
 3  卷的序列号是 36D9-CDC7
 4 
 5  D:\python\lnypcg\test 的目錄
 6 
 7 2016-06-02  23:28    <DIR>          .
 8 2016-06-02  23:28    <DIR>          ..
 9 2016-06-02  22:02               462 1.png
10                1 個檔案            462 位元組
11                2 個目錄 25,733,357,568 可用位元組
12 
13 D:\python\lnypcg\test>tesseract 1.png output -l eng
14 Tesseract Open Source OCR Engine v3.02 with Leptonica
15 
16 D:\python\lnypcg\test>type output.txt
17 7572
18 
19 
20 D:\python\lnypcg\test>
           

總結,tesseract是一個挺不錯的OCR引擎,目前的問題是最新的中文資料相對較少,過時、不準确的資訊偏多,把這幾天的琢磨的結果分享給大家,希望對大家有所幫助。