Python_python圖片識別小程式

來源:互聯網
上載者:User

之前看了,網上一些python 圖片識別的小程式。自己也試著寫個來測試下!

運行環境  Linux  CentOS  +python 2.7 +PIL庫+ Tesseract3.0+pytesser

 

環境搭建:

        Linux下安裝python就不說了,這裡主要說如何安裝pytesser,PIL和Tesseract

        1.檢查系統是否已經安裝以下庫:

            libpng   , libjpeg ,libtiff,zlibg-dev

            #yum list | grep libpng

            #yum list | grep libjpeg

            #yum list | grep libtiff

            #yum list | grep zlibg

       沒安裝上就安裝:

           #yum install libpng

           #yum install libjpeg

           #yum install libtiff

           #yum install zlibg

 

     2.安裝Tesseract:

          下載最新版Tesseract,http://code.google.com/p/tesseract-ocr/downloads/list 我下載的是3.0版本。

          解壓壓縮包:

          #tar -zxvf tesseract-3.00.tar.gz

          進入解壓後的檔案夾:

          #cd tesseract-3.00

         安裝: 

          #./configure --prefix=/opt/tesseract  #使用--prefix 來指定安裝的目錄,我這裡的安裝目錄是/opt/tesseract

         #make

        #make install

        安裝完成後要配置PATH,修改使用者home目錄下的 .profile或者.bash-profile我這裡是修改.bash-profile。在PATH那裡加上 以下內容。

        :/opt/tesseract/bin

        令設定檔生效:

         #. .bash-profile

  3.安裝PIL:

      到PIL首頁下載適合你的python版本的PIL:http://www.pythonware.com/products/pil/

        我python是2.7版本的,是:http://effbot.org/downloads/Imaging-1.1.7.tar.gz

        解壓壓縮包:

        #tar -zxvf Imaging-1.1.7.tar.gz

        進入解壓後的檔案夾:

        #cd Imaging-1.1.7

       安裝:

       #python setup.py install

      

 4.安裝pytesser:

     下載pytesser:http://pytesser.googlecode.com/files/pytesser_v0.0.1.zip 目前只有一個版本。

     解壓壓縮包:

     #unzip pytesser_v0.0.1.zip

     建議建立一個檔案夾,把壓縮包放到檔案夾裡在解壓,因為直接使用unzip來解壓會把壓縮包裡的東西解壓到目前的目錄,不易管理。

5.測試:

    在pytesser目錄下建立img_to_text.py內容如下:

 from pytesser import *             #匯入pytesser檔案

def img_to_text(filename):
    img = Image.open(filename)    #讀取圖片檔案
    img.load()                                #這裡要是不用load()方法,有時會提示找不到object。
    if len(img.split())==4:                #這裡主要把圖片的model分離

       r,g,b,a = img.split()          #把圖片的4個model或是通道付給r,g,b,a(r紅色通道,g綠色通道,b藍色通道,a透明Alpha 色板),PIL在bmp圖片是不支援a通道的。而圖片識別,是先要把圖片轉換成bmp格式在進行識別的。

       img = Image.merge("RGB",(r,g,b))    #去掉a通道,重新組合圖片。

    return image_to_string(img)                #調用pytesser中的image_to_string()方法,進行圖文轉換。方法中用到了tesseract引擎。

if __name__ == '__main__':
    img_to_text()

print "OK"

 

測試:

我這裡那了幾張網上商城的價格圖片進行識別:

 

是原圖:

¥符號不能識別,不過對數字部分沒影響。

測試別的圖片:

 

這張通過轉換成bmp再識別的圖片可以識別出¥符合。不過有時就算轉換成bmp圖片也是不能識別出¥符號。

 

不過要擷取價格可以截取string第3個字元之後的字元。也就是擷取string[2]及其後的字元了。

 

參考文章:  http://www.daniweb.com/software-development/python/threads/253957

                   http://wenyue.me/blog/282   Linux下使用pytesser

                   

 

ps:Tesseract 提供多國語言庫,可以到文章中Tesseract的下載頁面下載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.