1. 程式人生 > >python 圖像識別

python 圖像識別

img code pro encoding cati urn 加載 doc ins

這是一個最簡單的圖像識別,將圖片加載後直接利用Python的一個識別引擎進行識別 將圖片中的數字通過 pytesseract.image_to_string(image)識別後將結果存入到本地的txt文件中 技術分享圖片
 1 #-*-encoding:utf-8-*-
 2 import pytesseract
 3 from PIL import Image
 4 
 5 class GetImageDate(object):
 6     def m(self):
 7         image = Image.open(u"a.png")
 8         text = pytesseract.image_to_string(image)
 9         return text
10 
11     def SaveResultToDocument(self):
12         text = self.m()
13         f = open(u"Verification.txt","w")
14         print text
15         f.write(str(text))
16         f.close()
17 
18 g = GetImageDate()
19 g.SaveResultToDocument()
技術分享圖片 具體想要實現上面的代碼需要安裝兩個包和一個引擎 在安裝之前需要先安裝好Python,pip並配置好環境變量 所有包的安裝都是通過pip來安裝的,需要在windows PowerShell中進行,並且是在 C:\Python27\Scripts目錄下 1.第一個包: pytesseract pip install pytesseract 若是出現安裝錯誤的情況,安裝不了的時候,可以將命令改為 pip.exe install pytesseract來安裝 若是將pip修改為pip.exe安裝成功後,那麽下文的所有pip都需要改為pip.exe 2.第二個包:PIL安裝 pip install PIL 若是失敗了可以如下修改 pip install PILLOW 3.安裝識別引擎tesseract-ocr
https://github.com/tesseract-ocr/tesseract 下載 tesseract-ocr,進行默認安裝 安裝完成後需要配置環境變量,在系統變量path後增加 tesseract-ocr的安裝地址C:\Program Files (x86)\Tesseract-OCR; 一切都安裝完成後運行上述代碼,會發現報錯,此時需要 技術分享圖片 至此結束

python 圖像識別