python實現(xiàn)驗證碼識別功能
更新時間:2018年06月07日 10:17:58 作者:Li_JiaQian
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)驗證碼識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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('.','')) #去掉可能被識別的空格與.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)讀寫之Python讀寫CSV文件
這篇文章主要介紹了Python數(shù)據(jù)讀寫之Python讀寫CSV文件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Python-GUI?wxPython之自動化數(shù)據(jù)生成器的項目實戰(zhàn)
本文主要介紹了Python-GUI?wxPython之自動化數(shù)據(jù)生成器實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05爬蟲代理池Python3WebSpider源代碼測試過程解析
這篇文章主要介紹了爬蟲代理池Python3WebSpider源代碼測試過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12詳解python OpenCV學(xué)習(xí)筆記之直方圖均衡化
本篇文章主要介紹了詳解python OpenCV學(xué)習(xí)筆記之直方圖均衡化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02