這是一個(gè)最簡(jiǎn)單的圖像識(shí)別,將圖片加載后直接利用Python的一個(gè)識(shí)別引擎進(jìn)行識(shí)別
將圖片中的數(shù)字通過(guò) pytesseract.image_to_string(image)識(shí)別后將結(jié)果存入到本地的txt文件中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#-*-encoding:utf-8-*- import pytesseract from PIL import Image class GetImageDate( object ): def m( self ): image = Image. open (u "C:\\a.png" ) text = pytesseract.image_to_string(image) return text def SaveResultToDocument( self ): text = self .m() f = open (u "C:\\Verification.txt" , "w" ) print text f.write( str (text)) f.close() g = GetImageDate() g.SaveResultToDocument() |
具體想要實(shí)現(xiàn)上面的代碼需要安裝兩個(gè)包和一個(gè)引擎
在安裝之前需要先安裝好Python,pip并配置好環(huán)境變量
所有包的安裝都是通過(guò)pip來(lái)安裝的,需要在windows PowerShell中進(jìn)行,并且是在 C:\Python27\Scripts目錄下
1.第一個(gè)包: pytesseract
1
|
pip install pytesseract |
若是出現(xiàn)安裝錯(cuò)誤的情況,安裝不了的時(shí)候,可以將命令改為 pip.exe install pytesseract來(lái)安裝
若是將pip修改為pip.exe安裝成功后,那么下文的所有pip都需要改為pip.exe
2.第二個(gè)包:PIL安裝
1
|
pip install PIL |
若是失敗了可以如下修改 pip install PILLOW
3.安裝識(shí)別引擎tesseract-ocr
下載 tesseract-ocr,進(jìn)行默認(rèn)安裝
安裝完成后需要配置環(huán)境變量,在系統(tǒng)變量path后增加 tesseract-ocr的安裝地址C:\Program Files (x86)\Tesseract-OCR;
一切都安裝完成后運(yùn)行上述代碼,會(huì)發(fā)現(xiàn)報(bào)錯(cuò),此時(shí)需要
至此結(jié)束。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/Goldxwang/article/details/74719348