我為什么不用pytesseract來識(shí)別驗(yàn)證碼?據(jù)各大論壇反映,pytesseract的識(shí)別效果很一般。百度云人工智能的技術(shù)當(dāng)然毋庸置疑,識(shí)別效果確實(shí)很棒。我使用此接口,并不只是識(shí)別驗(yàn)證碼。我做了什么?
給公司文員寫了一個(gè)圖片轉(zhuǎn)文字的腳步,也就是PDF轉(zhuǎn)Word,效果一級(jí)棒,親測準(zhǔn)確率99%,轉(zhuǎn)換之后只需修改格式即可
我自己用來寫爬蟲程序,有些網(wǎng)站信息是用圖片展示的,先爬取圖片再識(shí)別,幾乎無誤。
在此,以識(shí)別驗(yàn)證碼的方式記錄下過程。
需求:
識(shí)別網(wǎng)站登錄的驗(yàn)證碼,如:

在百度智能云平臺(tái),提供許許多多的接口,有興趣的,自己去看百度智能云https://cloud.baidu.com/doc/OCR/s/Ek3h7xypm
實(shí)現(xiàn)步驟:
首先,登錄自己的百度賬號(hào),或者注冊(cè)一個(gè),進(jìn)去后,點(diǎn)擊管理控制臺(tái)
-
選擇產(chǎn)品服務(wù)——人工智能——文字識(shí)別——應(yīng)用列表——?jiǎng)?chuàng)建應(yīng)用,填寫相關(guān)信息并立即創(chuàng)建
[圖片上傳失敗...(image-5c25f5-1592829704080)]
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">應(yīng)用名稱:自己隨便寫
應(yīng)用類型:自己隨便寫
接口選擇:當(dāng)然是文字識(shí)別
文字識(shí)別包名:看自己,我選擇不需要
應(yīng)用描述:自己隨便寫</pre>在應(yīng)用列表可以看到自己剛創(chuàng)建的應(yīng)用了,這里的三個(gè)參數(shù):AppID、API Key、Secret Key,正是我們所需要的
-
下面免費(fèi)給大家分享,我提交工單,與百度智能云的工程師溝通,在凌晨4點(diǎn)時(shí)終于得到親測無誤的python代碼,期間花了8個(gè)小時(shí)時(shí)間。
代碼先貼,隨后解釋
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">def captcha(self, cap):
app_id = '你的AppID'
api_key = '你的API Key'
secret_key = '你的Secret Key'
client = AipOcr(app_id, api_key, secret_key)在上面代碼中,常量APP_ID在百度智能云控制臺(tái)中創(chuàng)建,常量API_KEY與SECRET_KEY是在創(chuàng)建完畢應(yīng)用后,系統(tǒng)分配給用戶的,均為字符串,用于標(biāo)識(shí)用戶,為訪問做簽名驗(yàn)證,可在AI服務(wù)控制臺(tái)中的應(yīng)用列表中查看。
client.setConnectionTimeoutInMillis(2000)#這兩行代碼至關(guān)重要
client.setSocketTimeoutInMillis(60000)#8個(gè)小時(shí)的成果
?
def get_file_content(img):
with open(img, 'rb')as f:
return f.read()
?
try:
result = client.basicAccurate(get_file_content(cap))#通用識(shí)別出來的結(jié)果,
except Exception:
sleep(0.2)
self.captcha(cap)
else:#以下代碼根據(jù)自己需求來改
if 'words_result' not in result.keys():
return self.captcha(cap)
else:
text = result['words_result']
cap_res = ''
for i in text[0]['words']:
try:
int(i)
except ValueError:
pass
else:
cap_res += i
return cap_res</pre>分析代碼:
-
首先,安裝兩個(gè)庫PIL和aip,并導(dǎo)入。這兩個(gè)庫安裝有些特殊,如果使用的python3直接使用pip install PIL或pip install aip是無法安裝的。
-
PIL:使用Pillow庫來安裝
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n149" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">pip install Pillow</pre>
PIL(Python Imaging Library)是Python一個(gè)強(qiáng)大方便的圖像處理庫,名氣也比較大。不過只支持到Python 2.7。
Pillow是PIL的一個(gè)派生分支,但如今已經(jīng)發(fā)展成為比PIL本身更具活力的圖像處理庫
-
aip:我們打開百度智能云的管理控制臺(tái)后,可以看到技術(shù)文檔有說明
- <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="py" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">pip install baidu-aip</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="py" cid="n194" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">from PIL import Image
from aip import AipOcr</pre>
-
-
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="py" cid="n200" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">client.setConnectionTimeoutInMillis(2000)#這兩行代碼至關(guān)重要
client.setSocketTimeoutInMillis(60000)#8個(gè)小時(shí)的成果</pre>-
這里的代碼為什么至關(guān)重要?
接口 說明 setConnectionTimeoutInMillis 建立連接的超時(shí)時(shí)間(單位:毫秒) setSocketTimeoutInMillis 通過打開的連接傳輸數(shù)據(jù)的超時(shí)時(shí)間(單位:毫秒) 大家看到了吧?一旦超時(shí),就識(shí)別不到。我采用的方式:
對(duì)識(shí)別內(nèi)容做判斷(識(shí)別后返回一個(gè)字典格式,當(dāng)沒有鍵[words_result]時(shí),停頓0.2秒繼續(xù)識(shí)別
-
-
沒有異常的正常代碼處理:
需求只是識(shí)別,我的處理簡單粗暴,對(duì)結(jié)果進(jìn)行遍歷提取。這里自行打印輸出查看
-
最后,希望我的分享對(duì)大家有用!