今天你要學習的驗證碼採用經過第三方AI平臺開放的OCR接口實現,OCR文字識別技術目前已經比較成熟了,並且第三方比較多,今天採用的是百度的。html
官方網址:ai.baidu.com/
接下來申請 python
你須要具有基本的閱讀第三方文檔的能力,打開咱們須要的文檔程序員
cloud.baidu.com/doc/OCR/OCR…web
這個頁面基本上已經把咱們須要作的全部內容都已經標識清楚了json
在目前主流的API開發模式下,都是須要你進行accesstoken的獲取的api
代碼以下 ,重點須要參照文檔進行傳參的設計 網絡
def get_accesstoken(self):
res = requests.post(self.url.format(self.key,self.secret),headers=self.header)
content = res.text
if (content):
return json.loads(content)["access_token"]
複製代碼
獲得accesstoken以後,你能夠繼續下面的操做app
import requests
import json
import base64
import urllib.request, urllib.parse
class GetCode(object):
def __init__(self):
self.url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}"
self.api = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={}"
self.header = {
"Content-Type":'application/json; charset=UTF-8'
}
self.key = "你的KEY"
self.secret = "你的SECRET"
複製代碼
普通沒有干擾的驗證碼,咱們直接識別便可,可是有的驗證碼仍是有干擾的,在識別以前,須要對它進行基本的處理,咱們採用和上篇文章相似的辦法進行,對它進行灰度處理和二值化操做。部分代碼我直接硬編碼了,不過最終識別的效果並無比想象的優化多少。python爬蟲
def init_table(self,threshold=155):
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table
def opt_image(self):
im = Image.open("66.png")
im = im.convert('L')
im = im.point(self.init_table(), '1')
im.save('66_s.png')
return "66_s.png"
複製代碼
調用百度的驗證碼接口,不使用百度給的模塊直接編寫。按照它對應的文檔,書寫便可。 在這個地方尤爲注意官方文檔提示 post
def get_file_content(self,file_path):
with open(file_path, 'rb') as fp:
base64_data = base64.b64encode(fp.read())
s = base64_data.decode()
data = {}
data['image'] = s
decoded_data = urllib.parse.urlencode(data)
return decoded_data
def show_code(self):
image = self.get_file_content(self.opt_image())
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
res = requests.post(self.api.format(self.get_accesstoken()),headers=headers,data=image)
print(res.text)
複製代碼
pip install baidu-aip
安裝以後,就可使用啦
from aip import AipOcr
# 定義常量
APP_ID = '15736693'
API_KEY = '你的KEY'
SECRET_KEY = '你的SECRET'
# 初始化文字識別
aipOcr=AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "1.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定義參數變量
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
# 網絡圖片文字文字識別接口
result = aipOcr.webImage(get_file_content(filePath),options)
print(result)
複製代碼
這種經過第三方OCR技術識別驗證碼的方式,本質上和上篇文章的原理是一致的 在實測過程當中發現,沒有太多幹擾線,搜狗
,騰訊
,有道
基本表現一致
對於這種方式,學會便可~,道理都是一致的,固然你能夠用Python實現一個圖片轉文字的小應用是沒有任何問題的
歡迎關注非本科程序員公衆帳號, 發送 ocr 獲取源碼