Python OpenCV實現(xiàn)識別信用卡號教程詳解
今天的博文分為三個部分。 在第一部分中,我們將討論 OCR-A 字體,這是一種專為輔助光學(xué)字符識別算法而創(chuàng)建的字體。 然后我們將設(shè)計一種計算機視覺和圖像處理算法,它可以:
- 本地化信用卡上的四組四位數(shù)字。
- 提取這四個分組中的每一個,然后單獨分割 16 個數(shù)字中的每一個。
- 使用模板匹配和 OCR-A 字體識別 16 個信用卡數(shù)字中的每一個。
最后,我們將看一些將信用卡 OCR 算法應(yīng)用于實際圖像的示例。
通過與 OpenCV 模板匹配的 OCR
在本節(jié)中,我們將使用 Python + OpenCV 實現(xiàn)我們的模板匹配算法來自動識別信用卡數(shù)字。
為了實現(xiàn)這一點,我們需要應(yīng)用許多圖像處理操作,包括閾值、計算梯度幅度表示、形態(tài)學(xué)操作和輪廓提取。這些技術(shù)已在其他博客文章中用于檢測圖像中的條形碼并識別護照圖像中的機器可讀區(qū)域。
由于將應(yīng)用許多圖像處理操作來幫助我們檢測和提取信用卡數(shù)字,因此我在輸入圖像通過我們的圖像處理管道時包含了許多中間屏幕截圖。
這些額外的屏幕截圖將讓您更深入地了解我們?nèi)绾文軌驅(qū)⒒緢D像處理技術(shù)鏈接在一起以構(gòu)建計算機視覺項目的解決方案。 讓我們開始吧。
打開一個新文件,命名為 ocr_template_match.py ,我們將開始工作:
# import the necessary packages from imutils import contours import numpy as np import argparse import imutils import cv2
要安裝/升級 imutils ,只需使用 pip :
pip install --upgrade imutils
注意:如果您使用 Python 虛擬環(huán)境(就像我所有的 OpenCV 安裝教程一樣),請確保首先使用 workon 命令訪問您的虛擬環(huán)境,然后安裝/升級 imutils 。
現(xiàn)在我們已經(jīng)安裝并導(dǎo)入了包,我們可以解析我們的命令行參數(shù):
# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image") ap.add_argument("-r", "--reference", required=True, help="path to reference OCR-A image") args = vars(ap.parse_args())
建立了一個參數(shù)解析器,添加兩個參數(shù),然后解析它們,將它們存儲為變量 args 。 兩個必需的命令行參數(shù)是:
–image :要進行 OCR 處理的圖像的路徑。
–reference :參考 OCR-A 圖像的路徑。 該圖像包含 OCR-A 字體中的數(shù)字 0-9,從而允許我們稍后在管道中執(zhí)行模板匹配。
接下來讓我們定義信用卡類型:
# define a dictionary that maps the first digit of a credit card # number to the credit card type FIRST_NUMBER = { "3": "American Express", "4": "Visa", "5": "MasterCard", "6": "Discover Card" }
信用卡類型,例如美國運通、Visa 等,可以通過檢查 16 位信用卡號中的第一位數(shù)字來識別。我們定義了一個字典 FIRST_NUMBER ,它將第一個數(shù)字映射到相應(yīng)的信用卡類型。 讓我們通過加載參考 OCR-A 圖像來啟動我們的圖像處理管道:
# load the reference OCR-A image from disk, convert it to grayscale, # and threshold it, such that the digits appear as *white* on a # *black* background # and invert it, such that the digits appear as *white* on a *black* ref = cv2.imread(args["reference"]) ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
首先,我們加載參考 OCR-A 圖像,然后將其轉(zhuǎn)換為灰度和閾值 + 反轉(zhuǎn)。 在這些操作中的每一個中,我們存儲或覆蓋 ref ,我們的參考圖像。
現(xiàn)在讓我們在 OCR-A 字體圖像上定位輪廓:
# find contours in the OCR-A image (i.e,. the outlines of the digits) # sort them from left to right, and initialize a dictionary to map # digit name to the ROI refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) refCnts = imutils.grab_contours(refCnts) refCnts = contours.sort_contours(refCnts, method="left-to-right")[0] digits = {}
找到了參考圖像中的輪廓。 然后,由于 OpenCV 2.4、3 和 4 版本如何不同地存儲返回的輪廓信息,我們檢查版本并對 refCnts 進行適當(dāng)更改。 接下來,我們從左到右對輪廓進行排序,并初始化一個字典,digits,它將數(shù)字名稱映射到感興趣的區(qū)域。
此時,我們應(yīng)該遍歷輪廓,提取ROI并將其與其對應(yīng)的數(shù)字相關(guān)聯(lián):
# loop over the OCR-A reference contours for (i, c) in enumerate(refCnts): # compute the bounding box for the digit, extract it, and resize # it to a fixed size (x, y, w, h) = cv2.boundingRect(c) roi = ref[y:y + h, x:x + w] roi = cv2.resize(roi, (57, 88)) # update the digits dictionary, mapping the digit name to the ROI digits[i] = roi
遍歷參考圖像輪廓。
在循環(huán)中, i 保存數(shù)字名稱/編號, c 保存輪廓。 我們圍繞每個輪廓 c 計算一個邊界框,用于存儲矩形的 (x, y) 坐標(biāo)和寬度/高度。使用邊界矩形參數(shù)從 ref(參考圖像)中提取 roi。 該 ROI 包含數(shù)字。
我們將每個 ROI 大小調(diào)整為 57×88 像素的固定大小。 我們需要確保每個數(shù)字都調(diào)整為固定大小,以便在本教程后面的數(shù)字識別中應(yīng)用模板匹配。
我們將每個數(shù)字 0-9(字典鍵)與每個 roi 圖像(字典值)相關(guān)聯(lián)。
在這一點上,我們完成了從參考圖像中提取數(shù)字并將它們與相應(yīng)的數(shù)字名稱相關(guān)聯(lián)的工作。
我們的下一個目標(biāo)是隔離輸入 --image 中的 16 位信用卡號。 我們需要先找到并隔離數(shù)字,然后才能啟動模板匹配以識別每個數(shù)字。 這些圖像處理步驟非常有趣且有見地,特別是如果您之前從未開發(fā)過圖像處理管道,請務(wù)必密切關(guān)注。
讓我們繼續(xù)初始化幾個結(jié)構(gòu)化內(nèi)核:
# initialize a rectangular (wider than it is tall) and square # structuring kernel rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3)) sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
您可以將內(nèi)核視為我們在圖像上滑動的小矩陣,以執(zhí)行(卷積)操作,例如模糊、銳化、邊緣檢測或其他圖像處理操作。
構(gòu)造了兩個這樣的內(nèi)核——一個矩形和一個正方形。 我們將使用矩形作為 Top-hat 形態(tài)算子,使用方形作為閉運算。 我們很快就會看到這些。 現(xiàn)在讓我們準(zhǔn)備要進行 OCR 的圖像:
# load the input image, resize it, and convert it to grayscale image = cv2.imread(args["image"]) image = imutils.resize(image, width=300) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
加載了包含信用卡照片的命令行參數(shù)圖像。 然后,我們將其調(diào)整為 width=300 ,保持縱橫比,然后將其轉(zhuǎn)換為灰度。 讓我們看看我們的輸入圖像:
接下來是我們的調(diào)整大小和灰度操作:
現(xiàn)在我們的圖像已經(jīng)灰度化并且大小一致,讓我們進行形態(tài)學(xué)操作:
# apply a tophat (whitehat) morphological operator to find light # regions against a dark background (i.e., the credit card numbers) tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
使用我們的 rectKernel 和我們的灰度圖像,我們執(zhí)行 Top-hat 形態(tài)學(xué)操作,將結(jié)果存儲為 tophat。
Top-hat操作在深色背景(即信用卡號)下顯示淺色區(qū)域,如下圖所示:
給定我們的高帽圖像,讓我們計算沿 x 方向的梯度:
# compute the Scharr gradient of the tophat image, then scale # the rest back into the range [0, 255] gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1) gradX = np.absolute(gradX) (minVal, maxVal) = (np.min(gradX), np.max(gradX)) gradX = (255 * ((gradX - minVal) / (maxVal - minVal))) gradX = gradX.astype("uint8")
我們努力隔離數(shù)字的下一步是計算 x 方向上高帽圖像的 Scharr 梯度。完成計算,將結(jié)果存儲為 gradX 。
在計算 gradX 數(shù)組中每個元素的絕對值后,我們采取一些步驟將值縮放到 [0-255] 范圍內(nèi)(因為圖像當(dāng)前是浮點數(shù)據(jù)類型)。 為此,我們計算 gradX 的 minVal 和 maxVal,然后計算第 73 行所示的縮放方程(即最小/最大歸一化)。 最后一步是將 gradX 轉(zhuǎn)換為范圍為 [0-255] 的 uint8。 結(jié)果如下圖所示:
讓我們繼續(xù)改進信用卡數(shù)字查找算法:
# apply a closing operation using the rectangular kernel to help # cloes gaps in between credit card number digits, then apply # Otsu's thresholding method to binarize the image gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel) thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # apply a second closing operation to the binary image, again # to help close gaps between credit card number regions thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
為了縮小差距,我們執(zhí)行了一個關(guān)閉操作。請注意,我們再次使用了 rectKernel。 隨后我們對 gradX 圖像執(zhí)行 Otsu 和二進制閾值,然后是另一個關(guān)閉操作。 這些步驟的結(jié)果如下所示:
接下來讓我們找到輪廓并初始化數(shù)字分組位置列表。
# find contours in the thresholded image, then initialize the # list of digit locations cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) locs = []
我們找到了輪廓并將它們存儲在一個列表 cnts 中 。 然后,我們初始化一個列表來保存數(shù)字組位置。
現(xiàn)在讓我們遍歷輪廓,同時根據(jù)每個輪廓的縱橫比進行過濾,允許我們從信用卡的其他不相關(guān)區(qū)域中修剪數(shù)字組位置:
# loop over the contours for (i, c) in enumerate(cnts): # compute the bounding box of the contour, then use the # bounding box coordinates to derive the aspect ratio (x, y, w, h) = cv2.boundingRect(c) ar = w / float(h) # since credit cards used a fixed size fonts with 4 groups # of 4 digits, we can prune potential contours based on the # aspect ratio if ar > 2.5 and ar < 4.0: # contours can further be pruned on minimum/maximum width # and height if (w > 40 and w < 55) and (h > 10 and h < 20): # append the bounding box region of the digits group # to our locations list locs.append((x, y, w, h))
我們以與參考圖像相同的方式循環(huán)遍歷輪廓。 在計算每個輪廓的邊界矩形 c之后,我們通過將寬度除以高度來計算縱橫比 ar 。 使用縱橫比,我們分析每個輪廓的形狀。 如果 ar 介于 2.5 和 4.0 之間(寬大于高),以及 40 到 55 像素之間的 w 和 10 到 20 像素之間的 h,我們將一個方便的元組中的邊界矩形參數(shù)附加到 locs。
下圖顯示了我們找到的分組——出于演示目的,我讓 OpenCV 在每個組周圍繪制了一個邊界框:
接下來,我們將從左到右對分組進行排序并初始化信用卡數(shù)字列表:
# sort the digit locations from left-to-right, then initialize the # list of classified digits locs = sorted(locs, key=lambda x:x[0]) output = []
我們根據(jù) x 值對 locs 進行排序,因此它們將從左到右排序。 我們初始化一個列表 output ,它將保存圖像的信用卡號。 現(xiàn)在我們知道每組四位數(shù)字的位置,讓我們循環(huán)遍歷四個排序的組并確定其中的數(shù)字。
這個循環(huán)相當(dāng)長,分為三個代碼塊——這是第一個塊:
# loop over the 4 groupings of 4 digits for (i, (gX, gY, gW, gH)) in enumerate(locs): # initialize the list of group digits groupOutput = [] # extract the group ROI of 4 digits from the grayscale image, # then apply thresholding to segment the digits from the # background of the credit card group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5] group = cv2.threshold(group, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # detect the contours of each individual digit in the group, # then sort the digit contours from left to right digitCnts = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) digitCnts = imutils.grab_contours(digitCnts) digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0]
在此循環(huán)的第一個塊中,我們提取并在每側(cè)填充組 5 個像素,應(yīng)用閾值處理,并查找和排序輪廓。 詳情請務(wù)必參考代碼。 下面顯示的是已提取的單個組:
讓我們用嵌套循環(huán)繼續(xù)循環(huán)以進行模板匹配和相似度得分提?。?/p>
# loop over the digit contours for c in digitCnts: # compute the bounding box of the individual digit, extract # the digit, and resize it to have the same fixed size as # the reference OCR-A images (x, y, w, h) = cv2.boundingRect(c) roi = group[y:y + h, x:x + w] roi = cv2.resize(roi, (57, 88)) # initialize a list of template matching scores scores = [] # loop over the reference digit name and digit ROI for (digit, digitROI) in digits.items(): # apply correlation-based template matching, take the # score, and update the scores list result = cv2.matchTemplate(roi, digitROI, cv2.TM_CCOEFF) (_, score, _, _) = cv2.minMaxLoc(result) scores.append(score) # the classification for the digit ROI will be the reference # digit name with the *largest* template matching score groupOutput.append(str(np.argmax(scores)))
使用 cv2.boundingRect 我們獲得提取包含每個數(shù)字的 ROI 所需的參數(shù)。為了使模板匹配以某種程度的精度工作,我們將 roi 的大小調(diào)整為與我們在第 144 行上的參考 OCR-A 字體數(shù)字圖像(57×88 像素)相同的大小。
我們初始化了一個分數(shù)列表。將其視為我們的置信度分數(shù)——它越高,它就越有可能是正確的模板。
現(xiàn)在,讓我們通過每個參考數(shù)字循環(huán)(第三個嵌套循環(huán))并執(zhí)行模板匹配。這是為這個腳本完成繁重工作的地方。
OpenCV 有一個名為 cv2.matchTemplate 的方便函數(shù),您可以在其中提供兩個圖像:一個是模板,另一個是輸入圖像。將 cv2.matchTemplate 應(yīng)用于這兩個圖像的目的是確定它們的相似程度。
在這種情況下,我們提供參考 digitROI 圖像和包含候選數(shù)字的信用卡的 roi。使用這兩個圖像,我們調(diào)用模板匹配函數(shù)并存儲結(jié)果。 接下來,我們從結(jié)果中提取分數(shù)并將其附加到我們的分數(shù)列表中。這樣就完成了最內(nèi)部的循環(huán)。
使用分數(shù)(每個數(shù)字 0-9 一個),我們?nèi)∽畲蠓謹?shù)——最大分數(shù)應(yīng)該是我們正確識別的數(shù)字。我們找到得分最高的數(shù)字,通過 np.argmax 獲取特定索引。該索引的整數(shù)名稱表示基于與每個模板的比較最可能的數(shù)字(再次記住,索引已經(jīng)預(yù)先排序為 0-9)。
最后,讓我們在每組周圍畫一個矩形,并以紅色文本查看圖像上的信用卡號:
# draw the digit classifications around the group cv2.rectangle(image, (gX - 5, gY - 5), (gX + gW + 5, gY + gH + 5), (0, 0, 255), 2) cv2.putText(image, "".join(groupOutput), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) # update the output digits list output.extend(groupOutput)
對于此循環(huán)的第三個也是最后一個塊,我們在組周圍繪制一個 5 像素的填充矩形,然后在屏幕上繪制文本。
最后一步是將數(shù)字附加到輸出列表中。 Pythonic 方法是使用擴展函數(shù)將可迭代對象(在本例中為列表)的每個元素附加到列表的末尾。
要查看腳本的執(zhí)行情況,讓我們將結(jié)果輸出到終端并在屏幕上顯示我們的圖像。
# display the output credit card information to the screen print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]])) print("Credit Card #: {}".format("".join(output))) cv2.imshow("Image", image) cv2.waitKey(0)
將信用卡類型打印到控制臺,然后在隨后的第 173 行打印信用卡號。
在最后幾行,我們在屏幕上顯示圖像并等待任何鍵被按下,然后退出腳本第 174 和 175 行。
花點時間祝賀自己——你做到了。 回顧一下(在高層次上),這個腳本:
- 將信用卡類型存儲在字典中。
- 獲取參考圖像并提取數(shù)字。
- 將數(shù)字模板存儲在字典中。
- 本地化四個信用卡號碼組,每個組包含四位數(shù)字(總共 16 位數(shù)字)。
- 提取要“匹配”的數(shù)字。
- 對每個數(shù)字執(zhí)行模板匹配,將每個單獨的 ROI 與每個數(shù)字模板 0-9 進行比較,同時存儲每個嘗試匹配的分數(shù)。
- 找到每個候選數(shù)字的最高分,并構(gòu)建一個名為 output 的列表,其中包含信用卡號。
- 將信用卡號和信用卡類型輸出到我們的終端,并將輸出圖像顯示到我們的屏幕上。
現(xiàn)在是時候查看運行中的腳本并檢查我們的結(jié)果了。
信用卡 OCR 結(jié)果
現(xiàn)在我們已經(jīng)對信用卡 OCR 系統(tǒng)進行了編碼,讓我們試一試。
我們顯然不能在這個例子中使用真實的信用卡號,所以我使用谷歌收集了一些信用卡示例圖像。
這些信用卡顯然是假的,僅用于演示目的。 但是,您可以應(yīng)用此博客文章中的相同技術(shù)來識別實際信用卡上的數(shù)字。
要查看我們的信用卡 OCR 系統(tǒng)的運行情況,請打開一個終端并執(zhí)行以下命令:
$ python ocr_template_match.py --reference ocr_a_reference.png \ --image images/credit_card_05.png Credit Card Type: MasterCard Credit Card #: 5476767898765432
我們的第一個結(jié)果圖像,100% 正確:
請注意我們?nèi)绾文軌蛘_地將信用卡標(biāo)記為萬事達卡,只需檢查信用卡號中的第一位數(shù)字即可。 讓我們嘗試第二張圖片,這次是一張簽證:
$ python ocr_template_match.py --reference ocr_a_reference.png \ --image images/credit_card_01.png Credit Card Type: Visa Credit Card #: 4000123456789010
再一次,我們能夠使用模板匹配正確地對信用卡進行 OCR。
$ python ocr_template_match.py --reference ocr_a_reference.png
$ python ocr_template_match.py --reference ocr_a_reference.png \ --image images/credit_card_02.png Credit Card Type: Visa Credit Card #: 4020340002345678
總結(jié)
在本教程中,我們學(xué)習(xí)了如何通過 OpenCV 和 Python 使用模板匹配來執(zhí)行光學(xué)字符識別 (OCR)。
具體來說,我們應(yīng)用我們的模板匹配 OCR 方法來識別信用卡類型以及 16 位信用卡數(shù)字。
為了實現(xiàn)這一點,我們將圖像處理管道分為 4 個步驟:
- 通過各種圖像處理技術(shù)檢測信用卡上的四組四個數(shù)字,包括形態(tài)學(xué)操作、閾值和輪廓提取。
- 從四個分組中提取每個單獨的數(shù)字,導(dǎo)致需要分類的 16 個數(shù)字。
- 通過將每個數(shù)字與 OCR-A 字體進行比較,將模板匹配應(yīng)用于每個數(shù)字以獲得我們的數(shù)字分類。
- 檢查信用卡號的第一位數(shù)字以確定發(fā)行公司。
在評估了我們的信用卡 OCR 系統(tǒng)后,我們發(fā)現(xiàn)它是 100% 準(zhǔn)確的,前提是發(fā)行信用卡公司使用 OCR-A 字體作為數(shù)字。
要擴展此應(yīng)用程序,您可能希望在野外收集信用卡的真實圖像,并可能訓(xùn)練機器學(xué)習(xí)模型(通過標(biāo)準(zhǔn)特征提取或訓(xùn)練或卷積神經(jīng)網(wǎng)絡(luò))以進一步提高該系統(tǒng)的準(zhǔn)確性。
到此這篇關(guān)于Python OpenCV實現(xiàn)識別信用卡號教程詳解的文章就介紹到這了,更多相關(guān)識別卡號內(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處理。