In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Python based on template how to achieve matching credit card number recognition function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python based on the template how to achieve matching credit card number recognition function"!
Environment introduction
Python 3.6 + OpenCV 3.4.1.15
Introduction of principle
First of all, the outline of each number in the template is extracted, and then the credit card image is processed, the digital part is extracted, and the result can be obtained by matching the number with the template.
Complete code
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2020-1-11 1415 Author: Martin# @ File: utils.py# @ Software:PyCharmimport cv2def sort_contours (cnts) Method='left-to-right'): reverse= False I = 0 if method= = 'right-to-left' or method= =' bottom-to-top': reverse= True if method= = 'bottom-to-top': I = 1 boundingboxes = [cv2.boundingRect (c) for c in cnts] (cnts, boundingboxes) = zip (* sorted (cnts, boundingboxes), key=lambda b: B [1] [I], reverse=reverse)) return cnts, boundingboxesdef resize (image, width=None, height=None) Inter=cv2.INTER_AREA): (h, w) = image.shape [: 2] if width is None and height is None: return image if width is None: r = height / float (h) dim = (int (w * r), height) else: r = width / float (w) dim = (width, int (h * r)) resized = cv2.resize (image, dim, interpolation=inter) return resized#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2020-1-11 14Author @ Author: Martin# @ File: template_match.py# @ Software:PyCharm "Credit card digit recognition based on template matching" import cv2import utilsimport numpy as np# specifies the credit card type FIRST_NUMBER = {'3': 'American Express',' 4': 'Visa',' 5': 'MasterCard' " '6':' Discover Card'} # drawing shows def cv_show (name, image): cv2.imshow (name, image) cv2.waitKey (0) cv2.destroyAllWindows () # read template image img = cv2.imread ('. / images/ocr_a_reference.png') cv_show ('img', img) # convert to grayscale image ref = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) cv_show (' ref' Ref) # convert to binary image ref = cv2.threshold (ref, 10,255, cv2.THRESH_BINARY_INV) [1] cv_show ('ref', ref) # calculate contour 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) # sort From left to right, From top to bottom refCnts = utils.sort_contours (refCnts, method='left-to-right') [0] digits = {} # traverse each profile for (I, c) in enumerate (refCnts): (x, y, w, h) = cv2.boundingRect (c) roi = ref [y:y+h, x:x+w] roi = cv2.resize (roi, (57, 88)) digits [I] = roi# initialization convolution kernel rectKernel = cv2.getStructuringElement (cv2.MORPH_RECT, (9) 3) sqKernel = cv2.getStructuringElement (cv2.MORPH_RECT, (5,5)) # read the input image Preprocessing img_path = input ("Input the path and image name:") image_input = cv2.imread (img_path) cv_show ('image', image_input) image_input = utils.resize (image_input, width=300) gray = cv2.cvtColor (image_input, cv2.COLOR_BGR2GRAY) cv_show (' gray', gray) # Top Hat Operation Highlight the brighter area 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) gradX = np.absolute (gradX) (minVal, maxVal) = (np.min (gradX)) Np.max (gradX)) gradX = ((gradX-minVal) / (maxVal-minVal)) gradX = gradX.astype ("uint8") print (np.array (gradX) .shape) cv_show ('gradX', gradX) # closed operation gradX = cv2.morphologyEx (gradX, cv2.MORPH_CLOSE, rectKernel) cv_show (' gradX', gradX) thresh = cv2.threshold (gradX, 0255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) [1] cv_show ('thresh') Thresh) thresh = cv2.morphologyEx (thresh, cv2.MORPH_CLOSE, sqKernel) cv_show ('thresh', thresh) # calculate profile thresh_, threshCnts, hierarchy = cv2.findContours (thresh.copy (), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = threshCntscur_img = image_input.copy () cv2.drawContours (cur_img, cnts,-1, (0,0,255), 3) cv_show (' img', cur_img) locs = [] # iterate through profile for (I C) in enumerate (cnts): (x, y, w, h) = cv2.boundingRect (c) ar = w / float (h) if 2.5 < ar < 4.0and (40 < w < 55) and (10 < h < 20): locs.append ((x, y, w, h)) # sort the matching contours from left to right locs = sorted (locs, key=lambda ix: ix [0]) output = [] # iterate through the numbers for (I, (gX, gY, gW) in each profile GH) in enumerate (locs): groupOutput = [] group = gray [gY-5:gY + gH + 5, gX-5: gX + gW + 5] cv_show ('group', group) # preprocessing group = cv2.threshold (group, 0,255, cv2.THRESH_OTSU) [1] cv_show (' group', group) # calculate each set of contours group_, digitCnts, hierarchy = cv2.findContours (group.copy (), cv2.RETR_EXTERNAL Cv2.CHAIN_APPROX_SIMPLE) digitCnts = utils.sort_contours (digitCnts, method='left-to-right') [0] # calculate each number for each group for c in digitCnts: (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) # get the most appropriate number groupOutput.append (str (np.argmax (scores) cv2.rectangle (image_input, (gX-5, gY-5), (gX + gW + 5, gY + gH + 5), (0,0,255), 1) cv2.putText (image_input, ".join (groupOutput), (gX, gY-15) Cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0,0,255), 2) # get the result output.extend (groupOutput) # print result print ("Credit Card Type: {}" .format (FIRST_ NUMBER [output [0]]) print ("Credit Card #: {}" .format (".join (output) cv2.imshow (" Image ", image_input) cv2.waitKey (0) cv2.destroyAllWindows ()
Result display
Credit Card Type: VisaCredit Card #: 4020340002345678 so far, I believe you have a deeper understanding of "how to achieve matching credit card number recognition function based on Python template". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.