Search code examples
pythonpython-3.xopencvqr-codeaadhaar

How to read & decode Secure QR code on Indian Aadhaar Card image


I am trying to extract the complete Aadhar number (12 digits) from the image of an Aadhar card (India)

enter image description here

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:

  1. pyaadhaar
  2. aadhaar-py

I am unable to decode Secure QR code using them on Aadhaar cards. Information on Secure QR code is available here. Please recommend possible resolutions or some other methods to achieve this task

Here is my code for decoding secure QR code using these libraries. Python version: 3.8

from pyaadhaar.utils import Qr_img_to_text, isSecureQr
from pyaadhaar.deocde import AadhaarSecureQr
from pyaadhaar.deocde import AadhaarOldQr

qrData = Qr_img_to_text(sys.argv[1])
print(qrData)

if len(qrData) == 0:
    print(" No QR Code Detected !!")
else:
    isSecureQR = (isSecureQr(qrData[0]))
    if isSecureQR:
        print("Secure QR code")
        try:
            obj  = AadhaarSecureQr(qrData[0])
        except:
            print("Try aadhaar-py library")
            from aadhaar.qr import AadhaarSecureQR
            integer_scanned_from_qr = 123456
            # secure_qr = AadhaarSecureQR(integer_scanned_from_qr)
            secure_qr = AadhaarSecureQR(int(qrData[0]))
            decoded_secure_qr_data = secure_qr.extract_data()
            print(decoded_secure_qr_data)

Here are the issues I am facing with these libraries:

  1. pyaadhaar: Secure QR code decoding code, tries to convert base10 string to bytes and fails. NOTE: For Old QR Code format of Aadhaar card, pyaadhaar library works well, this issue only occurs for Secure QR code. Stacktrace below:

    File "/home/piyush/libs/py38/lib/python3.8/site-packages/pyaadhaar/deocde.py", line 23, in __init__
    bytes_array = base10encodedstring.to_bytes(5000, 'big').lstrip(b'\x00')
    

    AttributeError: 'str' object has no attribute 'to_bytes'

  2. aadhaar-py: Secure QR decoding fails cause it is unable to validate integer received from QR code. Stacktrace below:

    Traceback (most recent call last): File "/home/piyush/libs/py38/lib/python3.8/site-packages/aadhaar/qr.py", line 55, in init self.decompressed_byte_array = zlib.decompress(self.byte_array, wbits=16+zlib.MAX_WBITS) zlib.error: Error -3 while decompressing data: incorrect header check During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "aadhaarQRCode.py", line 52, in secure_qr = AadhaarSecureQR(integer_scanned_from_qr) File "/home/piyush/libs/py38/lib/python3.8/site-packages/aadhaar/qr.py", line 57, in init raise MalformedIntegerReceived('Decompression failed, please send a valid integer received from QR code') aadhaar.exceptions.MalformedIntegerReceived: Decompression failed, please send a valid integer received from QR code


Solution

  • Thanks for posting the question. I am the author of aadhaar-py, the code raises an exception because the data passed to the lib cannot be parsed. It has to be of a certain type in order for it to be parsable. Please refer the following link for an example: https://uidai.gov.in/te/ecosystem-te/authentication-devices-documents-te/qr-code-reader-te.html

    If you scan the qr code present on the page and pass the data received to the lib, you'll receive the extracted data. P.S.: The Lib has been revamped with a new API. Be sure to check it out :) https://pypi.org/project/aadhaar-py/