

In each of these operations we store or overwrite ref, our reference image. Ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)įirst, we load the reference OCR-A image ( Line 29) followed by converting it to grayscale ( Line 30) and thresholding + inverting it ( Line 31). Ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) # and invert it, such that the digits appear as *white* on a *black* # and threshold it, such that the digits appear as *white* on a Let’s start our image processing pipeline by loading the reference OCR-A image: # load the reference OCR-A image from disk, convert it to grayscale, On Lines 16-23 we define a dictionary, FIRST_NUMBER, which maps the first digit to the corresponding credit card type. Next let’s define credit card types: # define a dictionary that maps the first digit of a credit cardĬredit card types, such as American Express, Visa, etc., can be identified by examining the first digit in the 16 digit credit card number. This image contains the digits 0-9 in the OCR-A font, thereby allowing us to perform template matching later in the pipeline.

The two required command line arguments are: On Lines 8-14 we establish an argument parser, add two arguments, and parse them, storing as the variable, args. Now that we’ve installed and imported packages, we can parse our command line arguments: # construct the argument parser and parse the argumentsĪp.add_argument("-i", "-image", required=True,Īp.add_argument("-r", "-reference", required=True,
Credit card validator python project install#
Note: If you are using Python virtual environments (as all of my OpenCV install tutorials do), make sure you use the workon command to access your virtual environment first and then install/upgrade imutils.

To install/upgrade imutils, simply use pip : $ pip install -upgrade imutils Template matching has been around awhile in OpenCV, so your version (v2.4, v3.*, etc.) will likely work. You will need to install OpenCV and imutils if you don’t already have them installed on your machine. Lines 1- 6 handle importing packages for this script. Open up a new file, name it ocr_template_match.py, and we’ll get to work: # import the necessary packages These additional screenshots will give you extra insight as to how we are able to chain together basic image processing techniques to build a solution to a computer vision project. Since there will be many image processing operations applied to help us detect and extract the credit card digits, I’ve included numerous intermediate screenshots of the input image as it passes through our image processing pipeline. These techniques have been used in other blog posts to detect barcodes in images and recognize machine-readable zones in passport images. In order to accomplish this, we’ll need to apply a number of image processing operations, including thresholding, computing gradient magnitude representations, morphological operations, and contour extraction. In this section we’ll implement our template matching algorithm with Python + OpenCV to automatically recognize credit card digits. To learn more about using template matching for OCR with OpenCV and Python, just keep reading.įigure 3: The MICR E-13B font commonly found on bank checks ( source).Įach of the above fonts have one thing in common - they are designed for easy OCR.įor this tutorial, we will make a template matching system for the OCR-A font, commonly found on the front of credit/debit cards. In today’s blog post I’ll be demonstrating how we can use template matching as a form of OCR to help us create a solution to automatically recognize credit cards and extract the associated credit card digits from images. Therefore, we need to devise our own custom solution to OCR credit cards. In these cases, the Tesseract library is unable to correctly identify the digits (this is likely due to Tesseract not being trained on credit card example fonts).

Credit card validator python project how to#
We then learned how to cleanup images using basic image processing techniques to improve the output of Tesseract OCR. In a previous blog post, we learned how to install the Tesseract binary and use it for OCR.
