I am trying to extract the complete Aadhar number (12 digits) from the image of an Aadhar card (India)
I am able to identify the region with QR code. To extract the info - I have been looking into python libraries that read and decode Secure QR codes on Indian Aadhaar cards. These 2 libraries seem particularly useful for this use case:
-
Now we create a rectangular kernel and morph close to combine the QR code into one contour
We find contours and filter for the QR code using contour area, contour approximation, and aspect ratio. The detected QR code is highlighted in green
Extracted ROI
Code
import cv2 import numpy as np # Load imgae, grayscale, Gaussian blur, Otsu's threshold image = cv2.imread('1.png') original = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (7,7), 0) thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1] # Morph close kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1) # Find contours and filter for QR code using contour area, approximation, and aspect ratio cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.04 * peri, True) x,y,w,h = cv2.boundingRect(approx) area = cv2.contourArea(c) ar = w / float(h) if len(approx) == 4 and area > 1000 and (ar > .85 and ar < 1.3): cv2.rectangle(image, (x, y), (x w, y h), (36,255,12), 3) ROI = original[y:y h, x:x w] # cv2.imwrite('ROI.png', ROI) # Display cv2.imshow('thresh', thresh) cv2.imshow('close', close) cv2.imshow('image', image) cv2.imshow('ROI', ROI) # Save images # cv2.imwrite('thresh.png', thresh) # cv2.imwrite('close.png', close) # cv2.imwrite('image.png', image) # cv2.imwrite('ROI.png', ROI) cv2.waitKey()