Ejemplo n.º 1
0
    def test_td3_checker(self):
        # optional data hash == '0'
        mrz1 = ("P<CANMARTIN<<SARAH<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
                "ZE000509<9CAN8501019F2301147<<<<<<<<<<<<<<08")
        test1 = bool(TD3CodeChecker(mrz1))

        # optional data hash == '<'
        mrz2 = ("P<CANMARTIN<<SARAH<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
                "ZE000509<9CAN8501019F2301147<<<<<<<<<<<<<<08")
        test2 = bool(TD3CodeChecker(mrz2))

        self.assertEqual(test1, test2)
Ejemplo n.º 2
0
def validate_whole(text):
    td3_check = TD3CodeChecker(text, check_expiry=True)
    if td3_check:
        fields = td3_check.fields()
        if fields.document_type == 'P':
            document_type = 'Passport'
        if fields.sex == 'M':
            sex = 'Male'
        else:
            sex = 'Female'
        json_object = {
            'Document Type': document_type,
            'Issuing Country': fields.country,
            'Surname': fields.surname,
            'Name': fields.name,
            'Passport Number': fields.document_number,
            'Nationality': fields.nationality,
            'Date Of Birth': convert_date(fields.birth_date),
            'Sex': sex,
            'Date Of Expiry': convert_date(fields.expiry_date),
            'Personal Number': fields.optional_data,
        }
        return json_object
    else:
        return False
Ejemplo n.º 3
0
def autoChecker(text):
    text = ''.join(text.split(' '))
    data = None
    t = None
    try:
        data = TD1CodeChecker(text)
        t = 'td1'
    except:
        try:
            data = TD2CodeChecker(text)
            t = 'td2'
        except:
            try:
                data = TD3CodeChecker(text)
                t = 'td3'
            except:
                try:
                    data = MRVACodeChecker(text)
                    t = 'mrva'
                except:
                    try:
                        data = MRVBCodeChecker(text)
                        t = 'mrvb'
                    except:
                        print(" Error : MRZ valide")
    return data, t
Ejemplo n.º 4
0
def create_person():
    document_type = 'P'
    country_code = random_letters(3)
    surname = random_letters(random.random() * 10)
    given_names = random_letters(random.random() * 12)
    document_number = random_letters(random.random() * 9)
    nationality = random_letters(3)
    birth_date = radnom_date()
    sex = random_sex()
    expiry_date = radnom_date()
    code = TD3CodeGenerator(document_type, country_code, surname, given_names,
                            document_number, nationality, birth_date, sex,
                            expiry_date, "", {}, True)
    return str(code), TD3CodeChecker(str(code), False, False).fields()
Ejemplo n.º 5
0
    def test_td3_checker(self):
        mrz_code = ("P<ASUMXHMWD<<EBDALRXHYM<<<<<<<<<<<<<<<<<<<<<\n"
                    "A2222222<0ASU7108215M04120411000146819<<<<44")
        td3_check = TD3CodeChecker(mrz_code)

        fields = td3_check.fields()
        self.assertEqual("MXHMWD", fields.surname)
        self.assertEqual("EBDALRXHYM", fields.name)
        self.assertEqual("ASU", fields.country)
        self.assertEqual("ASU", fields.nationality)
        self.assertEqual("710821", fields.birth_date)
        self.assertEqual("041204", fields.expiry_date)
        self.assertEqual("M", fields.sex)
        self.assertEqual("P", fields.document_type)
        self.assertEqual("A2222222", fields.document_number)
        self.assertEqual("1000146819", fields.optional_data)
        self.assertEqual("5", fields.birth_date_hash)
        self.assertEqual("1", fields.expiry_date_hash)
        self.assertEqual("0", fields.document_number_hash)
        self.assertEqual("4", fields.final_hash)
Ejemplo n.º 6
0
 def test_td3_checker(self):
     mrz_td3 = ("P<UKRTKACHENKO<<MARIANA<<<<<<<<<<<<<<<<<<<<<\n"
                "XX000000<0UKR9108242F23092571234567890<<<<70")
     self.assertTrue(bool(TD3CodeChecker(mrz_td3)))
Ejemplo n.º 7
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from mrz.checker.td3 import TD3CodeChecker

td3_check = TD3CodeChecker("P<INPSPECIMEN<<SAMPLE<<<<<<<<<<<<<<<<<<<<<<<\n"
                           "XX000000<0FRA1901012F16073021234567890<<<<70")

print("\n".join(td3_check.report_errors))
Ejemplo n.º 8
0
 def test_a_td3_checker(self):
     self.assertTrue(bool(TD3CodeChecker(self.mrz_td3)))
Ejemplo n.º 9
0
 def test_c_td3_checker(self):
     self.assertFalse(
         bool(
             TD3CodeChecker(self.mrz_td3,
                            check_expiry=True,
                            compute_warnings=True)))
Ejemplo n.º 10
0
 def test_b_td3_checker(self):
     self.assertListEqual(
         TD3CodeChecker(self.mrz_td3, check_expiry=True).report_warnings,
         ['document expired'])
Ejemplo n.º 11
0
#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from mrz.checker.td3 import TD3CodeChecker

td3_check = TD3CodeChecker(
    "P<CZESPECIMEN<<VZOR<<<<<<<<<<<<<<<<<<<<<<<<<\n"
    "99003853<1CZE1101018M1207046110101111<<<<<94",
    check_expiry=True)

print(bool(td3_check))
print(td3_check.report_warnings)
def get_passport_data(path):
    # initialize a rectangular and square structuring kernel
    rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
    sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (27, 27))

    image = cv2.imread(path)
    imgHeight = image.shape[0]
    imgWidth = image.shape[1]
    if imgWidth < imgHeight:
        image = cv2.rotate(image, rotateCode=cv2.ROTATE_90_COUNTERCLOCKWISE)

    image = imutils.resize(image, height=600)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # gray = cv2.bitwise_not(gray)

    # smooth the image using a 3x3 Gaussian, then apply the blackhat
    # morphological operator to find dark regions on a light background
    gray = cv2.GaussianBlur(gray, (3, 3), 0)
    # cv2.imshow("Gray", gray)
    # cv2.waitKey()
    blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)
    # cv2.imshow("Blackhat", blackhat)
    # cv2.waitKey()

    # compute the Scharr gradient of the blackhat image and scale the
    # result into the range [0, 255]
    gradX = cv2.Sobel(blackhat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
    gradX = np.absolute(gradX)
    (minVal, maxVal) = (np.min(gradX), np.max(gradX))
    gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")

    # apply a closing operation using the rectangular kernel to close
    # gaps in between letters -- then apply Otsu's thresholding method
    gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
    thresh = cv2.threshold(gradX, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # perform another closing operation, this time using the square
    # kernel to close gaps between lines of the MRZ, then perform a
    # serieso of erosions to break apart connected components
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
    # cv2.imshow("tresh", thresh)
    # cv2.waitKey()
    thresh = cv2.erode(thresh, None, iterations=4)
    # cv2.imshow("Erode", thresh)
    # cv2.waitKey()

    # during thresholding, it's possible that border pixels were
    # included in the thresholding, so let's set 5% of the left and
    # right borders to zero
    p = int(image.shape[1] * 0.05)
    thresh[:, 0:p] = 0
    thresh[:, image.shape[1] - p:] = 0

    # find contours in the thresholded image and sort them by their
    # size
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)[-2]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

    # loop over the contours
    for c in cnts:
        # compute the bounding box of the contour and use the contour to
        # compute the aspect ratio and coverage ratio of the bounding box
        # width to the width of the image
        (x, y, w, h) = cv2.boundingRect(c)
        ar = w / float(h)
        crWidth = w / float(gray.shape[1])

        # cv2.imshow("Image", image[y:y + h, x:x + w].copy())

        # check to see if the aspect ratio and coverage width are within
        # acceptable criteria
        # print(ar, crWidth)
        if ar > 5 and crWidth > 0.6:
            # pad the bounding box since we applied erosions and now need
            # to re-grow it
            pX = int((x + w) * 0.03)
            pY = int((y + h) * 0.03)
            (x, y) = (x - pX, y - pY)
            (w, h) = (w + (pX * 2), h + (pY * 2))

            # extract the ROI from the image and draw a bounding box
            # surrounding the MRZ
            roi = image[y:y + h, x:x + w].copy()
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            break
        else:
            roi = None

    # show the output images
    # cv2.imshow("Image", image)
    if roi is not None:
        config = (
            "--oem 0 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ<1234567890"
        )
        # config = ("--oem 3 --psm 6 --tessdata-dir ./Trained -l eng+ocrb")
        # config = ("--oem 0")
        # cv2.imshow("ROI", roi)
        # cv2.waitKey()
        roiGray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
        roiBlackhat = cv2.morphologyEx(roiGray, cv2.MORPH_BLACKHAT, rectKernel)
        roiThresh = cv2.threshold(roiBlackhat, 0, 255,
                                  cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        # coords = np.column_stack(np.where(roiThresh > 0))
        # angle = cv2.minAreaRect(coords)[-1]
        # # the `cv2.minAreaRect` function returns values in the
        # # range [-90, 0); as the rectangle rotates clockwise the
        # # returned angle trends to 0 -- in this special case we
        # # need to add 90 degrees to the angle
        # if angle < -45:
        #     angle = -(90 + angle)

        # # otherwise, just take the inverse of the angle to make
        # # it positive
        # else:
        #     angle = -angle

        # # rotate the image to deskew it
        # (h, w) = roiBlackhat.shape[:2]
        # center = (w // 2, h // 2)
        # M = cv2.getRotationMatrix2D(center, angle, 1.0)
        # rotated = cv2.warpAffine(
        #     roiBlackhat, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
        # print(angle)

        # roiBitwise = cv2.bitwise_not(roi)
        # roiThresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        # cv2.imshow("ROTATED", roiThresh)
        # cv2.waitKey()
        # pytesseract.image_to_string(Image.open("./imagesStackoverflow/xyz-small-gray.png"),lang="eng",boxes=False,config="--psm 4 --oem 3 -c tessedit_char_whitelist=-01234567890XYZ:"))
        text = pytesseract.image_to_string(roiThresh, config=config)
        # print(text)

        # enhancing text from ocr engine
        text = text.replace(" ", "")
        textList = text.split("\n")
        textEnhanced = []
        for txt in textList:
            l = len(txt) > 44
            if l > 44:
                textEnhanced.append(txt[0:44])
            elif l < 44:
                complement = txt + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                textEnhanced.append(complement[0:44])
            else:
                textEnhanced.append(txt)
        # print(textEnhanced)
        text = "\n".join(textEnhanced)

        td3_check = TD3CodeChecker(text)
        fields = td3_check.fields()
        # print(fields)
        # print(fields.surname.replace("0", "O"))

        # if bd > current then minus 1 century (human age = 0-99)
        now = datetime.datetime.now()
        oneCentury = relativedelta(years=100)

        bd = None
        try:
            bd = datetime.datetime.strptime(fields.birth_date, '%y%m%d')
            if bd > now:
                bd = bd - oneCentury
        except Exception as identifier:
            bd = None

        exp = None
        try:
            exp = datetime.datetime.strptime(fields.expiry_date, '%y%m%d')
        except Exception as identifier:
            exp = None

        # print(bd.strftime('%Y-%m-%d'))
        try:
            resp = {
                'surname':
                "" if fields.surname == "None" else fields.surname.replace(
                    "0", "O"),
                'given_names':
                "" if fields.name == "None" else fields.name.replace("0", "O"),
                'country_code':
                fields.country,
                'passport_number':
                passport_fixer(fields.document_number),
                'nationality':
                fields.nationality,
                'sex':
                'M' if fields.sex == 'H' else fields.sex,
                'birth_date':
                bd.strftime('%Y-%m-%d') if bd is not None else None,
                'expiry_date':
                exp.strftime('%Y-%m-%d') if exp is not None else None
            }
            return resp
        except Exception as identifier:
            return None
    else:
        return None
Ejemplo n.º 13
0
#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from mrz.base.countries_ops import is_code
from mrz.checker.td3 import TD3CodeChecker

mrz_td3 = ("P<ASUMXHMWD<<EBDALRXHYM<<<<<<<<<<<<<<<<<<<<<\n"
           "A2222222<0ASU7108215F04120411000146819<<<<44")

td3_check = TD3CodeChecker(mrz_td3, check_expiry=True)

print(td3_check.mrz_code)

if not td3_check:
    print("Falses:", td3_check.report_falses)
    print("Warnings:", td3_check.report_warnings)

print("'%s' is code: %s" % (td3_check._country, str(is_code("ASU"))))


def print_txt(title, value):
    print(title.ljust(20), value)


fields = td3_check.fields()

print_txt("Document Type:", fields.document_type)
print_txt("Country:", fields.country)
print_txt("Surname:", fields.surname)
print_txt("Name:", fields.name)
print_txt("Doc. Number", fields.document_number)
Ejemplo n.º 14
0
#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from mrz.checker.td3 import TD3CodeChecker


print(TD3CodeChecker("P<CANMARTIN<<SARAH<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
                     "ZE000509<9CAN8501019F2301147<<<<<<<<<<<<<<08",
                     check_expiry=True,
                     compute_warnings=True))