Python如何識別銀行卡卡號?
一、現(xiàn)有資源梳理
目前有一張卡號模板圖片
N張測試銀行卡圖片,其一如下
操作環(huán)境 win10-64位
代碼語言 Python 3.6
二、實現(xiàn)方案規(guī)劃
對模板操作,將十個模板和對應(yīng)的數(shù)字一一對應(yīng)起來
圖片中通過查找輪廓,然后繪制輪廓外界矩形的方式,將每一和數(shù)字分割出來,并和對應(yīng)的數(shù)字相對應(yīng)。以字典的形式保存
每一個模板都是這樣的形式存儲。
array([[ 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255], [ 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255], [ 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 0], [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255]], dtype=uint8)
對測試圖片操作,取得我們需要的,每個數(shù)字的像素 .
整個照片的干擾信息很多,很難直接就定位到卡號位置,需要經(jīng)過一系列的變換。
定位到卡號位置后,如何將每個卡號給提取出來,進行模板匹配,識別其數(shù)字。
1.輸入的圖片為RGB格式,需要轉(zhuǎn)換成GRAY格式,然后再將灰度形式的圖片進行二值化處理。
2.對于二值化處理之后的圖片進行Sobel濾波,將數(shù)字模糊,連接起來。
3.經(jīng)過Sobel之后可能數(shù)字沒有連接在一起,所以執(zhí)行閉操作將相鄰的數(shù)字連接起來,因為數(shù)字是橫向的,所以閉操作的核設(shè)置為[1,1,1,1,1,1,1,1,1]
。
4.通過查找輪廓和輪廓外接矩形的方式定位到連續(xù)數(shù)字區(qū)域。
5.通過連續(xù)數(shù)字區(qū)域分割出每一個數(shù)字,然后將每個數(shù)字和模板進行匹配,匹配結(jié)果最高的就是最有可能的數(shù)字。
三、代碼實現(xiàn)
工具包導(dǎo)入
from imutils import contours import numpy as np import argparse import cv2 import myutils
路徑和繪圖函數(shù)及信用卡類型設(shè)定
# 模板圖片 template = 'images/ocr_a_reference.png' # 測試圖片 image = 'images/credit_card_03.png' # 指定信用卡類型 FIRST_NUMBER = { "3": "American Express", "4": "Visa", "5": "MasterCard", "6": "Discover Card" } # 繪圖展示 def cv_show(name,img): cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows()
模板處理
img = cv2.imread(template) cv_show('img', img) # 灰度圖 ref = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv_show('ref', ref) # 二值圖像 ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1] cv_show('ref', ref) # 計算輪廓 #cv2.findContours()函數(shù)接受的參數(shù)為二值圖,即黑白的(不是灰度圖),cv2.RETR_EXTERNAL只檢測外輪廓,cv2.CHAIN_APPROX_SIMPLE只保留終點坐標(biāo) #返回的list中每個元素都是圖像中的一個輪廓 ref_, refCnts, hierarchy = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, refCnts, -1, (0, 0, 255), 3) cv_show('img', img) print(np.array(refCnts).shape) refCnts = myutils.sort_contours(refCnts, method="left-to-right")[0] #排序,從左到右,從上到下 digits = {} # 遍歷每一個輪廓 for (i, c) in enumerate(refCnts): # 計算外接矩形并且resize成合適大小 (x, y, w, h) = cv2.boundingRect(c) roi = ref[y:y + h, x:x + w] roi = cv2.resize(roi, (57, 88)) # 每一個數(shù)字對應(yīng)每一個模板 digits[i] = roi # print(digits)
測試圖片處理
# 初始化卷積核 rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) #讀取輸入圖像,預(yù)處理 image = cv2.imread(image) cv_show('image',image) image = myutils.resize(image, width=300) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv_show('gray',gray) #禮帽操作,突出更明亮的區(qū)域 tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel) cv_show('tophat',tophat) # gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, #ksize=-1相當(dāng)于用3*3的 ksize=-1) gradX = np.absolute(gradX) (minVal, maxVal) = (np.min(gradX), np.max(gradX)) gradX = (255 * ((gradX - minVal) / (maxVal - minVal))) gradX = gradX.astype("uint8") print (np.array(gradX).shape) cv_show('gradX',gradX) #通過閉操作(先膨脹,再腐蝕)將數(shù)字連在一起 gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel) cv_show('gradX',gradX) #THRESH_OTSU會自動尋找合適的閾值,適合雙峰,需把閾值參數(shù)設(shè)置為0 thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv_show('thresh',thresh) #再來一個閉操作 thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel) #再來一個閉操作 cv_show('thresh',thresh) # 計算輪廓 thresh_, threshCnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = threshCnts cur_img = image.copy() cv2.drawContours(cur_img,cnts,-1,(0,0,255),3) cv_show('img',cur_img) locs = [] # 遍歷輪廓 for (i, c) in enumerate(cnts): # 計算矩形 (x, y, w, h) = cv2.boundingRect(c) ar = w / float(h) # 選擇合適的區(qū)域,根據(jù)實際任務(wù)來,這里的基本都是四個數(shù)字一組 if ar > 2.5 and ar < 4.0: if (w > 40 and w < 55) and (h > 10 and h < 20): #符合的留下來 locs.append((x, y, w, h)) # 將符合的輪廓從左到右排序 locs = sorted(locs, key=lambda x:x[0]) output = [] # 遍歷每一個輪廓中的數(shù)字 for (i, (gX, gY, gW, gH)) in enumerate(locs): # initialize the list of group digits groupOutput = [] # 根據(jù)坐標(biāo)提取每一個組 group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5] cv_show('group',group) # 預(yù)處理 group = cv2.threshold(group, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv_show('group',group) # 計算每一組的輪廓 group_,digitCnts,hierarchy = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0] # 計算每一組中的每一個數(shù)值 for c in digitCnts: # 找到當(dāng)前數(shù)值的輪廓,resize成合適的的大小 (x, y, w, h) = cv2.boundingRect(c) roi = group[y:y + h, x:x + w] roi = cv2.resize(roi, (57, 88)) cv_show('roi',roi) # 計算匹配得分 scores = [] # 在模板中計算每一個得分 for (digit, digitROI) in digits.items(): # 模板匹配 result = cv2.matchTemplate(roi, digitROI, cv2.TM_CCOEFF) (_, score, _, _) = cv2.minMaxLoc(result) scores.append(score) # 得到最合適的數(shù)字 groupOutput.append(str(np.argmax(scores))) # 畫出來 cv2.rectangle(image, (gX - 5, gY - 5), (gX + gW + 5, gY + gH + 5), (0, 0, 255), 1) cv2.putText(image, "".join(groupOutput), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) # 得到結(jié)果 output.extend(groupOutput) # 打印結(jié)果 print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]])) print("Credit Card #: {}".format("".join(output))) cv2.imshow("Image", image) cv2.waitKey(0) # (194, 300) # Credit Card Type: MasterCard # Credit Card #: 5412751234567890
所有代碼連在一起就是完整的代碼
到此這篇關(guān)于Python如何識別銀行卡卡號?的文章就介紹到這了,更多相關(guān)Python識別卡號內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。