溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python如何實現(xiàn)驗證碼識別功能

發(fā)布時間:2021-04-12 13:37:20 來源:億速云 閱讀:182 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹python如何實現(xiàn)驗證碼識別功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

具體內(nèi)容如下

1.通過二值化處理去掉干擾線

2.對黑白圖片進行降噪,去掉那些單獨的黑色像素點

3.消除邊框上附著的黑色像素點

4.識別圖像中的文字,去掉空格與'.'

python代碼:

from PIL import Image 
from aip import AipOcr 
 
file='1-1-7' 
 
# 二值化處理,轉(zhuǎn)化為黑白圖片 
def two_value(): 
 for i in range(1, 5): 
 # 打開文件夾中的圖片 
 image = Image.open(file+'.jpg') 
 # 灰度圖 
 lim = image.convert('L') 
 # 灰度閾值設(shè)為165,低于這個值的點全部填白色 
 threshold = 165 
 table = [] 
 
 for j in range(256): 
  if j < threshold: 
  table.append(0) 
  else: 
  table.append(1) 
 
 bim = lim.point(table, '1') 
 bim.save(file+'.1.jpg') 
 
two_value() 
 
# 去除干擾線 
im = Image.open(file+'.1.jpg') 
# 圖像二值化 
data = im.getdata() 
w, h = im.size 
black_point = 0 
 
for x in range(1, w - 1): 
 for y in range(1, h - 1): 
 mid_pixel = data[w * y + x] # 中央像素點像素值 
 if mid_pixel < 50: # 找出上下左右四個方向像素點像素值 
  top_pixel = data[w * (y - 1) + x] 
  left_pixel = data[w * y + (x - 1)] 
  down_pixel = data[w * (y + 1) + x] 
  right_pixel = data[w * y + (x + 1)] 
 
  # 判斷上下左右的黑色像素點總個數(shù) 
  if top_pixel < 5: #小于5比小于10更精確 
  black_point += 1 
  if left_pixel < 5: 
  black_point += 1 
  if down_pixel < 5: 
  black_point += 1 
  if right_pixel < 5: 
  black_point += 1 
  if black_point < 1: 
  im.putpixel((x, y), 255) 
  # print(black_point) 
  black_point = 0 
 
im.save(file+'.2.jpg') 
 
# 去除干擾線 
im = Image.open(file+'.2.jpg') 
# 圖像二值化 
data = im.getdata() 
w, h = im.size 
black_point = 0 
 
for x in range(1, w - 1): 
 for y in range(1, h - 1): 
 if x < 2 or y < 2: 
  im.putpixel((x - 1, y - 1), 255) 
 if x > w - 3 or y > h - 3: 
  im.putpixel((x + 1, y + 1), 255) 
 
im.save(file+'.3.jpg') 
 
# 定義常量 
APP_ID = '11352343' 
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE' 
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os' 
 
# 初始化AipFace對象 
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) 
 
# 讀取圖片 
filePath=file+'.3.jpg' 
def get_file_content(filePath): 
 with open(filePath, 'rb') as fp: 
 return fp.read() 
 
# 定義參數(shù)變量 
options = { 
 'detect_direction': 'true', 
 'language_type': 'CHN_ENG', 
} 
 
# 調(diào)用通用文字識別接口 
result = aipOcr.basicGeneral(get_file_content(filePath), options) 
print(result) 
words_result=result['words_result'] 
for i in range(len(words_result)): 
 print(words_result[i]['words'].replace(' ','').replace('.','')) #去掉可能被識別的空格與.

python如何實現(xiàn)驗證碼識別功能

以上是“python如何實現(xiàn)驗證碼識別功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI