My first cv project, from ID card to face recognition
Allearn

My first cv project: Extract ID card number

My CV story starts with a cousrse project when I was in sophomore. The final personal project can be selected from the following choices: 24-point game program, Tetris, ID card number extraction, face recognition by opencv, License plate number extraction. Actually, I have finished the first two in other courses and face recognition(CNN, will be introduced in the following part) was my parallel project directed by another professor, so only two choices: ID card number extraction or License plate number extraction. They were same to me, so I chose ID card number extraction.

Let’s start!

Development Process

Step 1: Development Environment

The project uses:

  • OpenCV: A library for working with images.
  • NumPy: For numerical operations like creating matrices.
  • Pytesseract: A library to perform OCR (Optical Character Recognition) to extract text from images.

Step 2: Loading the Image

image = cv2.imread('test1.jpg')

test1.jpg will be the ID card, which is from the Internet.

image

Step 3: Finding the Area with the ID Number (Region of Interest)

height, width = image.shape[:2] roi = image[int(0.75 * height):height, int(0.1 * width):int(0.9 * width)] The program crops the bottom part of the image, where the ID number is expected. It assumes the ID number is located in a specific area (e.g., the bottom center of the image).

image

Step 4: Cleaning the Image

Before extracting text, the program prepares the image to make it easier for Tesseract to read.

  1. Convert to Grayscale: Removes color, leaving only shades of gray.

gray_image = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

  1. Blur the Image: Reduces noise by smoothing the image.

blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

  1. Thresholding: Converts the image into pure black and white.

_, thresholded_image = cv2.threshold(blurred_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

  1. Morphological Cleaning: Removes small imperfections (like dots or specks) in the image.

kernel = np.ones((3, 3), np.uint8) cleaned_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_CLOSE, kernel)

image

Step 5: Extracting the Text

extracted_text = pytesseract.image_to_string(cleaned_image, config=custom_config)

Tesseract OCR reads the cleaned image and tries to find text. And The config setting tells Tesseract to assume the text is in a single block and to use a specific engine mode.

Step 6: Extracting Only Numbers

id_number = ''.join(filter(str.isdigit, extracted_text))

If the extracted text is too long, it tries to trim it to 18 digits (the typical length of a Chinese ID number).

Step 7: Displaying Results

Finally, the program displays the extracted number. If the result doesn’t look correct, it gives a warning. It also shows the intermediate images (like the cropped region and cleaned image) for debugging purposes.

The result will be “Extracted ID Number: 310109199107141011” which is the same as the ID number.

All done!

If the ID number can be extracted, and the face recognition can be used too, then a KYC(know your customers) system can be realized.